SOLUTIONS
OCTOBER 2022
QUESTION 1
1.1 Value of newval: 2
1.2 Value of a: 17
1.3 Output of findValue(3): 23
, QUESTION 2
2.1 Purpose of the Code:
The purpose of this segment of code is to calculate the sum of the integers from
0 to 5 (inclusive) and store the result in the variable s. The variable n starts at 0
and is incremented by 1 in each iteration of the while loop until it becomes
greater than 5, with each value of n being added to s.
2.2 Purpose of the Code:
The purpose of this segment of code is to count how many non-zero elements
are present in the array numbers. The loop iterates through all the elements of
the array, and for each non-zero element, the counter c is incremented by 1.
After the loop completes, c contains the total number of non-zero elements in the
array.
QUESTION 3
Variable Diagram
Ste a count Condition Calculation Update Update
p (initiall (initially (count < (a += count da d count
y 2) 0) valueP) + a / 2)
1 2 0 True a += 0 + 3 2
=> a += 0 +
1
2 3 2 True a += 2 + 6 4
=> a += 2 +
1
3 6 4 True a += 4 + 13 6
=> a += 4 +
3
4 13 6 False (loop - - -
ends)
QUESTION 4
COMPLETE CODE
#include <iostream>
using namespace std;
int main()
{