# Exercise # 4 Solution ```Tampere University of Technology
Department of Software Systems
OHJ-1106 Programming I
Exercise # 4 Solution
(26-10-2012)
(Note: Solutions should be submitted as a printed copy at the beginning of session)
Exercise 1:
Define the following terms:
a. Loop Entry: The point at which the flow of control reaches the first statement
inside a loop.
b. Iteration: An individual pass through, or repetition of, the body of a loop.
c. Termination Condition: The condition that causes a loop to be exited.
d. Tracing a variable means watching a program variable change value while the
program is running. This can be done with special debugging facilities or by
inserting temporary output statements in the program.
Exercise 2:
a) for(double sample =2; sample&gt;0; sample = sample-0.5)
cout&lt;&lt;sample&lt;&lt; “ “;
b) int n=5;
while( --n&gt;0 )
{
if( n==2)
break;
cout&lt;&lt;n&lt;&lt;” ”;
}
cout&lt;&lt;” End of loop”;
Answer: 4 3 End of loop
c) int n, m;
for(n=1; n&lt;=10;n++)
for(m=10;m&gt;=1;m-)// Error: should be m-cout&lt;&lt;n&lt;&lt;”times”&lt;&lt;m&lt;&lt;”=”&lt;&lt;n*m&lt;&lt;endl;
1 times 10 =10
1 times 9= 9
….
1 times 1 =1
2 times 10 =20
2 times 9= 18
….
Exercise 3:
Rewrite the following loops as for loops
a) for( int i=1;i&lt;=10;i=i+3)
cout&lt;&lt;’X’;
int i=1;
while(i&lt;=10)
{
cout&lt;&lt;’X’;
i=i+3;
}
b) for( int i=1;i&lt;=10;i++)
if( i&lt;5 &amp;&amp; i!=2)
cout&lt;&lt;’X’;
int i=1;
while( i&lt;=10 )
{
if( i&lt;5 &amp;&amp; i!=2)
cout&lt;&lt;’X’;
i++;
}
c)
cout&lt;&lt;’X’;
for( long m=200;m&lt;1000;m=m+100)
cout&lt;&lt;’X’;
long m=100;
do {
cout&lt;&lt;’X’;
m=m+100;
} while (m&lt;1000);
Exercise 4:
What is the output of this nested loop structure?
i = 4;
while (i &gt;= 1)
{
j = 2;
while (j &gt;= 1)
{
cout &lt;&lt; j &lt;&lt; ‘ ‘;
j--;
}
cout &lt;&lt; i &lt;&lt; endl;
i--;
}
214
213
212
211
Exercise 5:
The following code segment is supposed to calculate product of 10 natural numbers
It has four flaws in it.
int n = 1;
int prod = 0;
do {
prod = prod *n;
} while (n &lt; 10);
cout &lt;&lt; prod &lt;&lt; ‘ ‘;
a) What is the output of the code as written?
b) Correct the code so that it works as intended.
a) do-while loop will not terminate.
b) int n = 1;
int prod = 1; // prod should start from 1
do
{
prod = prod *n;
n++; // n value should be incremented by one to get factorial of 10
} while (n &lt; =10); // missing equal sign
cout &lt;&lt; prod &lt;&lt; ‘ ‘;
Exercise 6:
i) What is printed by the following program fragment?
a) for(int i=1; i&lt;5;i++) {
int j=0;
while(j&lt;i) {
cout&lt;&lt;j&lt;&lt;” ”;
j++;
}
}
Answer: 0 0 1 0 1 2 0 1 2 3
b) int i=0;
while( i&lt;5){
for(int j=i; j&gt;1;j--)
cout&lt;&lt;j&lt;&lt;” ”;
cout&lt;&lt;” ****”&lt;&lt;endl;
i++;
}
****
****
2 ****
3 2 ****
4 3 2 ****
ii) Convert the following for loop to a while loop and a do-while loop
long sum=1000;
for(int i=sum; i&gt;0; i--)
sum=sum+i;
While loop:
int i=0, sum=1000;
while(i&gt;0) {
sum=sum+i;
i--;
}
Do-while loop:
int i=0, sum=1000;
do{
sum = sum + i;
i--;
} while(i&gt;0);
Note: There will be a practical work after solving the above questions. The task will
be announced during the session.
```