Question: Exercise: Write code (on paper) for a program that performs the following steps (in main). Take a picture of your code on paper and save
Exercise: Write code (on paper) for a program that performs the following steps (in main). Take a picture of your code on paper and save it to your computer. Convert the image to a pdf and save it as paper_code.pdf.
Then, draw a memory diagram (on paper) that describes the behavior of the program, and use that diagram to predict the output--make sure to circle the output on your diagram page. This just needs to be the diagram (youre not using the tool, so no need to write the description). Then take a picture of your paper memory diagram, convert it to a pdf, and save it as paper_diagram.pdf.
Finally, implement your code in a file called steps.cpp and check your predictions.
Note: Start your file steps.cpp as follows, then complete main to implement the steps below.
#include
using namespace std;
int func(int arg) {
arg = arg + 2;
return arg;
}
int funcp(int *argp) {
*argp = (*argp) + 2;
return *argp;
}
int main () {
...
}
1. Define an integer variable i with initial value 17 and a double variable f with initial value -4.2.
2. Define a pointer variable ip initialized to hold the address of the variable i (the type of ip will be int *). Also define a pointer variable fp initialized to hold the address of the variable f (what is the correct type for fp?).
3. Print the four values i, *ip, f, and *fp (here, * is the dereference operator). Please label your output, e.g.,
Initial values: i is 17, *ip is 17, f is -4.2, *fp is -4.2
4. Multiply i by 3 (e.g., i = i * 3;). Multiply *fp by 1.5. Then print the four values i, *ip, f, and *fp, labelling your output as before, e.g.,
After multiplying: i is 51, *ip is
5. Define a second double variable f2 with initial value 2.5. Assign the address of f2 to the pointer variable fp. Also, multiply i and *ip together and store the result in i, using an assignment expression statement. Now print the five values i, *ip, f, *fp, and f2. Label your output (be sure to predict this and all output values by hand first in your memory diagram drawn on paper).
After adding f2 and multiplying i: i is
6. Print the return value from the call func(i), then print the two values i and *ip after that call in a separate output expression statement, but still on the same line. For example:
cout << "func(i) returns " << func(i);
cout << ", i is " << i << ", *ip is " << *ip << endl;
7. Print the return value from the call func(*ip), then print the two values i and *ip after that call in a separate output expression statement, following the example in step 6.
8. Print the return value from the call funcp(ip), then print the two values i and *ip after that call in a separate output expression statement, following the example in step 6.
9. Print the return value from the call funcp(&i), then print the two values i and *ip after that call in a separate output expression statement, following the example in step 6
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
