You are on page 1of 4

(KNL 1353 Structured Programming)

____________________________________________________________________________________________________________
Instruction: -
Answer ALL questions

Question 1

Identify and correct the errors in each of the following:


i. while ( c <= 5 )
{
product *= c;
c++;
ii. while ( z >= 0 )
sum += z;
Solution:
i. int c = 0; //initialize
while ( c <= 5 ) // check
{
product *= c;
c++; // update
}
i. int z = 0; //initialize
while ( z >= 0 ) // check
{
sum += z;
z++; // update
}

____________________________________________________________________________________________________________
Puan Maimun Huja Husin 1
(KNL 1353 Structured Programming)
____________________________________________________________________________________________________________
Question 2

Write the C++ statement to accomplish the following tasks.


i. Declare variables sum and x to be of type int.
ii. Set variable x to 1.
iii. Set variable sum to 0.
iv. Add variable x to variable sum and assign the result to variable sum.
v. Print "The sum is: " followed by the value of variable sum.

Combine the statements into a program that calculates and prints the sum of
the integers from 1 to 10. Use the while statement to loop through the
calculation and increment statements. The loop should terminate when the
value x becomes 11.

Solution:

int sum, x;
x=1;
sum=0;
while (x<=10)
{
sum = sum + x;
x++;
}

cout<<"The sum is: " << sum;

____________________________________________________________________________________________________________
Puan Maimun Huja Husin 2
(KNL 1353 Structured Programming)
____________________________________________________________________________________________________________
Question 3

Write a C++ statement or a set of C++ statements to accomplish each of the


following:
i. Sum the odd integers between 1 and 99 using a for statement. Assume
the integer variables sum and count have been declared.
ii. Print the integers from 1 to 20 using a while loop and the counter
variable x. Assume that the variable x has been declared, but not
initialized. Print only 5 integers per line.

Solution:
i. for(count=1;count<100;count+=2)
sum+=count;

ii. x = 1;
while (x<=20)
{
for (int y=1; y<6; y++)
{
cout<<x;
x++;
}
cout<<endl;
}

____________________________________________________________________________________________________________
Puan Maimun Huja Husin 3
(KNL 1353 Structured Programming)
____________________________________________________________________________________________________________
Question 4

Find the error(s) in each of the following code segments and explain how to
correct it (them).
i. x = 1;
while ( x <= 10 );
x++;
}
ii. for ( y = .1; y != 1.0; y += .1 )
cout << y << endl;
iii. The following code should output the even integers from 2 to 100:
counter = 2;

do
{
cout << counter << endl;
counter += 2;
} while ( counter < 100);

Solution:
i. x = 1;
while ( x <= 10 ) // remove the semicolon
{ // insert the opening braces
x++;
}
ii. for ( y = .1; y != 1.0; y+= .1 ) // will loop forever
cout << y << endl;
iii. counter = 2;
do
{
cout << counter << endl;
counter += 2;
} while ( counter <= 100); // add = so that 100 will be included

End of Section

____________________________________________________________________________________________________________
Puan Maimun Huja Husin 4

You might also like