Pre-Lab - Lab (3)



Before coming to the lab, read Chapter 3 of the text and try to answer the following questions in writing.

1. The following C++ program computes the value of 34.  Modify the program to do the computation for 24, 33, 54, and 44, at once, and to display the results.
// This C++ program computes the value of 34.
#include<iostream>
using namespace std;

int main(void)
{
      int i = 0, p = 1;
      int x = 3, y = 4;

      while(i < 4)
      {
             p = p * x;
             i++;
       }
       cout << x << " to the power of " << y << " is = " << p << endl;

       return 0;
}

Note: Do not write 4 programs, modify the same program to compute all of them in one program.

2. Suppose you have a C++ program, like the one that does your taxes, and you want to display the results both rounded up and down.  How would you round a
real value up?  How would you round a real value down?

3. What does the following C++ program display?
#include<iostream>
using namespace std;

int main(void)
{
      int x,y;
      cout << "Enter 2 values for x and y separated by space, then press <Enter> :";
      cin >> x >> y;
      cout << endl;

      cout << x << "/" << y << " = " << x/y << endl;

      return 0;
}

Does your program produce the correct answer for x = 2 and y = 4?

If not, explain why it does not produce the correct answer and how you would go to fix the problem.

As we mentioned in Lab2, looping helped us simplify our computation significantly.  In Lab3, we will use functions to simplify our computations even more.