Description
Lab2 continues the warmup to C++, with a focus on POINTERS!
DO NOT USE the class construct.
Follow these steps:
1) Create a file called L2.cpp
2) Write the skeletal main routine, which should look something like:
#include
using namespace std;
int main()
{ cout << "Welcome message" << endl;
…
cout << endl << "End of Lab2 Pgm" << endl;
return 0;
}
3) Write the following code, in a function
int x; // stack allocation of integer variable
int* iPtr; // stack allocation of pointer variable #A
x = 100; // x initialized to hold value 100 #D
iPtr = &x; // iPtr initialized to hold address of x #E
cout << x << &x << endl; // output x’s value and then address #F
cout << *iPtr << iPtr << endl; // same as above #G
cout << ++x << x++ << endl; // outputs 101 101 #H
cout << x++ << ++x << endl; // outputs 102 104 #I
*iPtr = 200; // indirect access #J
cout << x << endl; // is 104 or 200 output? Why? #K
cout << ++(*iPtr) << (*iPtr)++ << endl; // outputs 201 201 #L
cout << (*iPtr)++ << ++(*iPtr) << endl; // outputs 202 204 #M
cout << x << endl; // is 104 or 204 output? Why? #N
cout << *iPtr << endl; // is 104 or 204 output? Why? #P
4) Modify the code in the function crafted in #3 so that parameter ‘x’ is passed by value, and ANOTHER value is passed to replace ‘200’ on line #J. Vary the values passed by invoking this function multiple times. Predict the integer values output in statement #F-I and #K-P.
5) Write a second function, mirroring the code in the function crafted in #4 except that both parameters are passed by reference. Vary the actual arguments passed by invoking this function multiple times. Predict the integer values output in statement #F-I and #K-P.