Exercise # 4 Solution

advertisement
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:
What is the output of following code? Explain your answer?
a) for(double sample =2; sample>0; sample = sample-0.5)
cout<<sample<< “ “;
Answer: 2.00 1.50 1.00 0.50
b) int n=5;
while( --n>0 )
{
if( n==2)
break;
cout<<n<<” ”;
}
cout<<” End of loop”;
Answer: 4 3 End of loop
c) int n, m;
for(n=1; n<=10;n++)
for(m=10;m>=1;m-)// Error: should be m-cout<<n<<”times”<<m<<”=”<<n*m<<endl;
Answer:
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<=10;i=i+3)
cout<<’X’;
Answer:
int i=1;
while(i<=10)
{
cout<<’X’;
i=i+3;
}
b) for( int i=1;i<=10;i++)
if( i<5 && i!=2)
cout<<’X’;
Answer:
int i=1;
while( i<=10 )
{
if( i<5 && i!=2)
cout<<’X’;
i++;
}
c)
cout<<’X’;
for( long m=200;m<1000;m=m+100)
cout<<’X’;
Answer:
long m=100;
do {
cout<<’X’;
m=m+100;
} while (m<1000);
Exercise 4:
What is the output of this nested loop structure?
i = 4;
while (i >= 1)
{
j = 2;
while (j >= 1)
{
cout << j << ‘ ‘;
j--;
}
cout << i << endl;
i--;
}
Answer:
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 < 10);
cout << prod << ‘ ‘;
a) What is the output of the code as written?
b) Correct the code so that it works as intended.
Answer:
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 < =10); // missing equal sign
cout << prod << ‘ ‘;
Exercise 6:
i) What is printed by the following program fragment?
a) for(int i=1; i<5;i++) {
int j=0;
while(j<i) {
cout<<j<<” ”;
j++;
}
}
Answer: 0 0 1 0 1 2 0 1 2 3
b) int i=0;
while( i<5){
for(int j=i; j>1;j--)
cout<<j<<” ”;
cout<<” ****”<<endl;
i++;
}
Answer:
****
****
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>0; i--)
sum=sum+i;
Answer:
While loop:
int i=0, sum=1000;
while(i>0) {
sum=sum+i;
i--;
}
Do-while loop:
int i=0, sum=1000;
do{
sum = sum + i;
i--;
} while(i>0);
Note: There will be a practical work after solving the above questions. The task will
be announced during the session.
Download