Question 1: Modify a program (loops) [6]
Modify the program below to use a while loop instead of a for loop, keeping the behaviour and logic as close as possible to the original.
#include <cstdio>
const float StartVal = 12.3;
const float Scalar = 87.654;
const int StepSize = 3;
int main()
{
float j = 4.5;
for (int i = 10; i < (Scalar * j); i = i + StepSize)
{
printf("Step %d, limiter %g\n", i, j);
j = j - 1;
}
return 0;
}
|
Sample solution:
// just showing the revised loop
int i = 10;
while (i < (Scalar * j)) {
printf("Step %d, limiter %g\n", i, j);
j = j - 1;
i = i + StepSize;
}
|