Please explain answer 18. (8 pts) For the following code segment, write out what
ID: 3604816 • Letter: P
Question
Please explain answer
18. (8 pts) For the following code segment, write out what is printed to the screen. Show the displayed output precisely by using the following rules o Write one character per box o Skip a box to indicate the presence of a blank space in the output. o Skip a row to indicate the presence of a blank line in the output. #include using namespace std void Sum (int&); int main () int add4; Sum (add); Sum (add); Sum (add); Sum (add); return 0; void Sum (int& add) int sum- static int j1; sum sum + add; coutExplanation / Answer
1
-
4
2
-
7
3
-
9
4
-
10
First Call : Sum(add)
sum and j are static, so it wont reinitialize after intialization.
Sum = 0, j = 1, add=4;
sum = sum + add (0+4 = 4)
cout << j << “-” << sum << endl; //prints 1-4
j++; //j = 2
add--; //add = 3
Second Call : Sum(add)
sum and j are static, so it wont reinitialize after intialization.
Sum = 4, j = 2, add=3; //as per last function call
sum = sum + add (4+3 = 7)
cout << j << “-” << sum << endl; //prints 2-7
j++; //j = 3
add--; //add = 2
Third Call : Sum(add)
sum and j are static, so it wont reinitialize after intialization.
Sum = 7, j = 3, add=2; //as per last function call
sum = sum + add (7+2 = 9)
cout << j << “-” << sum << endl; //prints 3-9
j++; //j = 4
add--; //add = 1
Second Call : Sum(add)
sum and j are static, so it wont reinitialize after intialization.
Sum = 9, j = 4, add=1; //as per last function call
sum = sum + add (9+1 = 10)
cout << j << “-” << sum << endl; //prints 4-10
j++; //j = 5
add--; //add = 0
1
-
4
2
-
7
3
-
9
4
-
10