Lab Practice 5: Repetition Structures: Part A: Tutorial
Lab Practice 5: Repetition Structures: Part A: Tutorial
Learning Objectives: At the end of this lab practice, you should be able to:
To describe the repetition structures while, for and do-
while used in a program.
To write C++ programs, that uses while, for and do-
while structures and how it can be convert to switch-
case statement.
To apply the counter-controlled and sentinel-controlled in a
program.
Total Week 2 weeks
PART A: TUTORIAL
1
a) What is a repetition?
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
c) What is the most important difference between a while statement and a do-
while statement?
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
2
Practice 3: What output will be produced by the following codes?
a) int a = 5;
while( a <= 60)
{ a *= 2;
cout << a << endl;
}
cout << “a is now ” << a << endl;
Answer:
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
b) int x = 10;
while (x >= 10)
{
cout << x << endl;
x = x – 3;
}
Answer:
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
c) int x =4, y = 5, z = y + 6;
do
{
cout << z << "–";
z = z + 7;
} while (((z - x) % 4) != 0);
Answer:
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
3
d) int x = –21;
do
{
cout << x << endl;
x = x – 3;
} while ( x > 0);
Answer:
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
Answer:
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
int n, m;
for (n = 1; n <= 3; n++)
for (m = 4; m >= 1; m– –)
cout<< n << “times”<< m << “ = “ << n*m << endl;
Answer:
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
4
Practice 5: Trace the following segment of a program and get the result.
int i,j,k;
for (i=1; i<=5;i++)
{for (j=1;j<=3;j++)
{for (k=1; k<=4;k++)
cout<<'*';
cout<<endl;
}
cout<<endl;
}
Answer:
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………..
………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………………………….
Practice 6: What does the following program segment prints? Assume that the input x=5
and y=10.
int x,y;
cout<<"Enter two integers in the range of 1-20: ";
cin>> x >> y;
for (int i=1; i<=y; i++)
{
for (int j=i; j<=x; j++)
cout<<'&';
cout<<endl;
}
Answer:
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………..
………………………………………………………………………………………………
………………………………………………………………………………………………
5
Practice 7: Write a block of code that prints out the odd numbers 1 through 999 using
while and for structures. You do not need to write a full program.
Answer:
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………..
………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………………………….
………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………………………….
a) int d = 28;
while (d%2==0 || d > 1)
{ d = d / 2;
cout << d << "\n";
}
Answer:
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………..
b) int x = 19;
while (x > 2)
{ x /= 2;
cout << x;
if (x%2 == 0)
cout << "\n";
}
6
Answer:
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………..
c) int c = 1;
while (c < 20)
{ if (c%4 == 0 && c%3 != 0)
cout << c << "x";
c++;
}
Answer:
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………..
d) int d = 31;
do
{ d = d/3;
if (d%2 == 1)
d += 4;
cout << d << "+";
} while (d>2);
Answer:
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………..
7
Practice 9: Trace the following program and get the result.
#include <iostream.h>
int main(void)
{
for (int i = 0; i < 5; i+=3)
cout << " i = " << i << endl;
double xx = 32;
while (xx > 1.0)
xx /= 2;
cout << " xx = " << xx << endl;
int i = 10;
int j = 4;
int k = i / j;
double x = i / j;
double y = ( (double)i / (double)j );
cout << " k=" << k << " , x=" << x << " , y=" << y <<
endl;
return 0;
}
Answer:
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………..
………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………………………….
………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………………………….
…………………………………………………………………………………………….
…………………………………………………………………………………………….
8
Practice 10: Write a portion of code (not a full program) that computes the sum of the
even numbers 2 through 100.
2 + 4 + 6 + 8 + ... + 98 + 100
Store the value in a variable called sum. You do not have to print out anything.
Answer:
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………..
………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………………………….
………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………………………….
9
PART B: LAB PROGRAMMING
Program 1: Write a program using while loop to find the sum of integers 65 through 120
inclusive.
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
Program 2: Write a program that reads in a size of triangles shapes, then prints them
similar to the following diagrams, for example, if your program reads a size of 5, it
should print:-
a)
&&&&&
&&&&
&&&
&&
&
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
10
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
b) @
@@
@@@
@@@@
@@@@@
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
11
Program 4: Write a program (starting from #include) that repeatedly collects positive
integers from the user, stopping when the user enters a negative number or zero. After
that, output the largest positive number entered. A sample/example run should appear on
the screen like the text below.
Enter a number: 3
Enter a number: 10
Enter a number: 2
Enter a number: -213
The largest positive number you entered was 10.
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
12
Program 4: Write a program using do-while loop to determine whether the number is a
prime number. (A prime number is a number which is divisible by 1 or itself without
leaving any remainder. Example: 4,915 and 16 are not prime numbers while 2, 3, 5 and 7
are prime numbers).
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
13
Program 5: A bowling team consists of three players. Each player bowls four games.
The table below shows the information of last night's game scores.
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
14
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
15