#include <iostream>
usingnamespace std;
int func(int p, int q);
int main()
{
int a = 1, b = 6, c = 4;
c = func(a,b);
cout << "in main: " << a << " "
<< b << " " << c << endl;
c = func(2,a+b) + 100;
cout << "in main again: " << c << endl;
cout << "over";
return 0;
}
int func(int p, int q)
{
int r, s;
r = p + q;
if (r == 7)
s = r + 3;
else
s = r - 5;
cout << "in the func: " << p << " " << q
<< " " << r << " " << s << endl;
return r + s;
}
My output is
in the func: 1 6 7 10
in main: 1 6 17
in function 2 7 9 4
in main again: 113
over
I have the correct output but I don't understand how does it go from 1 6 7 10 to 1 6 17 then to 2 7 9 4.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main()
{
int p = 10, r = 6, s = 2;
for (p = 1; p < 4; p++) {
cout << "type in a number";
if (p < 3)
cin >> r;
else
cin >> s;
cout << "in: " << p << " "
<< r << " " << s << endl;
}
cout << "out";
return 0;
}
For this program, I am suppose to enter 10, 20, 30.
My output is:
1 10 2
2 20 2
3 20 30
How did it go from 2 20 2 to 3 20 30 when the formula in the program doesn't even have calculations.
Line Notes Values
-----------------------------------------
5: initialization | p=10 r=6 s=2
7: p is set to 1 | p=1 r=6 s=2
7: p < 4 is true | ""
9: p < 3 is true | ""
10: user sets r to 10 | p=1 r=10 s=2
13: output 1, 10 and 2 | ""
7: p is incremented | p=2 r=10 s=2
7: p < 4 is true | ""
9: p < 3 is true | ""
10: user sets r to 20 | p=2 r=20 s=2
13: output 2, 20 and 2 | ""
7: p is incremented | p=3 r=20 s=2
7: p < 4 is true | ""
9: p < 3 is false | ""
12: user sets s to 30 | p=3 r=20 s=30
13: output 3, 20 and 30 | ""
7: p is incremented | p=4 r=20 s=2
7: p < 4 is false | ""
16: output "out" | ""
The first looks more complicated, but I may take a look at it later.