0% found this document useful (0 votes)
115 views15 pages

Lab Practice 5: Repetition Structures: Part A: Tutorial

This document outlines a lab practice on repetition structures in C++. It contains examples of while, for, and do-while loops and asks students to determine output, differentiate between loop types, and write short code snippets using repetition structures. The objectives are to describe repetition structures, write programs using while, for, and do-while loops, and apply counter-controlled and sentinel-controlled loops. The lab practice contains multiple choice questions, short answer questions, and code tracing exercises.

Uploaded by

kamiyoukai
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views15 pages

Lab Practice 5: Repetition Structures: Part A: Tutorial

This document outlines a lab practice on repetition structures in C++. It contains examples of while, for, and do-while loops and asks students to determine output, differentiate between loop types, and write short code snippets using repetition structures. The objectives are to describe repetition structures, write programs using while, for, and do-while loops, and apply counter-controlled and sentinel-controlled loops. The lab practice contains multiple choice questions, short answer questions, and code tracing exercises.

Uploaded by

kamiyoukai
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

LAB PRACTICE 5: REPETITION STRUCTURES

Name: ______________________________________ Group: __________________


SID: _______________________ Date: ____________

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

Practice 1: Circle the correct answer for true/false questions below.


a) A section of repeating code is referred to as a loop. [T/F]
b) A do-while loop is referred to as a post-test loop because the loop condition is
tested after the loop body of instructions. [T/F]
c) It is possible that the body of a while loop may not execute at all. [T/F]
d) A sentinel-controlled while loop is a loop whose termination depends on a special
value. [T/F]
e) A repetition is a control structure that causes certain statements to execute over
and over. [T/F]

Practice 2: Answers all of the questions below:

1
a) What is a repetition?
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

b) How many structures does repetition have?


…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

c) What is the most important difference between a while statement and a do-
while statement?
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

d) Differentiate between pre-test and post-test loops.


…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

e) What does a break statement do?


…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

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:
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

e) for (int count = 1; count < 5; count++)


cout << (2 * count) << “ “;

Answer:
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

Practice 4: Get the output of the following nested loop:

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:
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………..
………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………………………….
………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………………………….

Practice 8: Get the output for the following repetitions:

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;

for (int i = 1; i <= 2; i++)


for (int j = 1; j < 5; j+=2)
cout << " i * j = " << i*j << 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.

Player Game 1 Game 2 Game 3 Game 4


1 100 120 150 100
2 45 90 150 145
3 70 90 125 140

Write a program to do the following:


a) Declare a 3-by-4 array named bowlers and store the above information in the
array.
b) Compute and display the total score for each player.
c) Find and display the lowest score in each game.
d) Find and display the average team score

………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………

14
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………

15

You might also like