Harith Ikhwan Lab 6 - Repetition Part 1
Harith Ikhwan Lab 6 - Repetition Part 1
Harith Ikhwan Lab 6 - Repetition Part 1
REPETITION STRUCTURE
(Part I)
Learning Outcomes
After completing this lab, you will be able to:
o understand how the loop control variable is used in a loop
o use the while loop structures to executes statements in a program
o understand counter-controlled and sentinel-controlled repetition
A. PRE-LAB ACTIVITIES
Question 1
What is a repetition structure?
Answer
Repetition structure control a block of code to be executed for a fix number of times or
until certain condition is met.
Question 2
Give TWO (2) requirements of a repetitive or loop structure.
Answer
A termination condition controls how many times the loop is executed.
Question 3
Explain THREE (3) operations needed to control the loop structure.
Answer
Statement can be simple or compound Statement is called the body of the loop
Expression acts as a decision maker and is usually a logical expression
Question 4
Differentiate between pre-test and post-test loops.
Answer
Question 5
What is an “infinite loop” and why it is happen?
Answer
continues to execute endlessly
Question 6
What is the definition of counter-controlled loop?
Answer
If ywe know exactly how many pieces of data need to be read
Question 7
What is the definition of sentinel-controlled loop?
Answer
Sentinel variable is tested in the condition and loop ends when sentinel is
encountered
Question 8
Program segment given:
int n = 1;
while(n <= 5)
{
n = n + 1;
cout << n << “ ”;
}
Answer
Initialization : int n=1……………………………………………………………..
Evaluation : n<=5………..…………………………………………………………
Update : n=n+1………………………………………………………………..
Question 9
What is the output for the following while statement. (Use tracing table provided). State
whether this loop is counter-controlled or sentinel-controlled.
int n = 1;
while(n <= 5)
{
n = n + 1;
cout << n << “ ”;
}
Answer
Tracing Table
Output
23456
Counter-controlled
Question 10
What is the output for the following while statement by using the data below as input
for variable item. (Use tracing table provided).
10 3 4 2 5 11
Answer
Tracing Table:
data sum sum <= cin >> data *= sum cout <<
250 item item += sum
data
1 0 true 10 10 10 10
10 10 true 3 30 40 40
30 40 true 4 120 160 160
120 160 true 2 240 400 400
240 400 false false false false
Output:
10 40 160 400
Counter-controlled
B. LAB ACTIVITIES
Exercise 1
int main()
{
int counter;
counter = 1;
while(counter <= 10)
{
cout << counter << “ ”;
counter = counter + 1;
}
return 0;
}
Program 7.1
d. What is the assignment statement used to update the loop control variable?
counter=counter+1
#include <iostream>
using namespace std;
main()
{
int counter;
counter = 10;
while (counter < 0);
{
cout << counter << “ ”;
counter = counter - 1;
}
}
Program 7.2
a. Compile the program. If error messages appear, write down all the error(s).
No error
b. Compile and run the program. Is there any unexpected output? If yes, state the
problem and why it is happened.
Yes. the semicolon at the end of condition loop make a loop end.
c. The purpose of the program is to display all numbers between 10 down to 1. Modify
the program to active the intended purpose of the program.
#include <iostream>
using namespace std;
int main()
{
int counter;
counter = 10;
while (counter >=0)
{
cout << counter << " ";
counter = counter - 1;
}
}
Exercise 3
main()
{
int sum, number;
sum = 0;
cin >> number;
while (number != -1)
{
sum = sum + number;
cin >> number;
}
cout << sum << endl;
Program 7.3
b. What is the sentinel value for this loop? What happen if the user enters the sentinel
value as input?
Sentinel valuen is -1. The loop will stop
b. This program is supposed to display and find the sum of ALL numbers between 15
and 27 (inclusive) that are divisible by three. Then, it displays the numbers and the
sum of these numbers as output. Modify the above program to achieve the
intended purposes.
#include <iostream>
using namespace std;
main()
{
int i = 15, sum = 0;
while(i <= 27)
{
if (i % 3 == 0){
sum = sum + i ;
cout << i << " ";}
i = i + 2;
}
cout << sum << endl;
}
Exercise 5
#include <iostream>
using namespace std;
main()
{
int number, temp;
int digit = 1;
cin >> number;
temp = number;
while ((temp > 0) || (temp < 0))
{
temp = temp / 100;
digit = digit + 2;
}
cout << number << “ has ” << digit << “ digits” << endl;
}
Program 7.5
b. This program segment is supposed to find the total of single digits of an integer
number. For example, integer 123 has three single digits and integer 4576 has four
single digits. Modify the above program to achieve the intended purposes.
#include <iostream>
using namespace std;
main()
{
int number, temp;
int digit = 0;
cin >> number;
temp = number;
while ((temp > 0) || (temp < 0))
{
temp = temp / 10;
digit = digit + 1;
}
cout << number << " has " << digit << " digits" << endl;
}
C. POST-LAB ACTIVITIES
Question 1
Write a program using while loop to find the sum of integers 73 through 415 inclusive.
int main(){
int num1=73,num2=415,sum=0,i=num1;
while(i<=num2){
sum=sum+i;
i++;
}
cout<<sum;
return 0;
}
Name :
Student ID :
Date :
Question 2
Write a program using while loop to determine is a prime number. A prime number is a
number which is divisible by 1 or itself without leaving any remainder. For example: 4, 9,
15 and 16 are NOT a prime numbers while 2, 3, 5 and 7 are prime numbers
#include <iostream>
using namespace std;
int main() {
int i, n;
bool is_prime = true;
if (is_prime)
cout << n << " is a prime number";
else
cout << n << " is not a prime number";
return 0;
Name :
Student ID :
Date :
}
Name :
Student ID :
Date :
Question 3
Write a program using while loop that can read an integer number and determines
whether the sum of the cubes of the digits is equals to the number itself. Display a
message indicating whether the sum of the cubes is equal or not equal to the number.
For example, if the number 563 is entered as input, the sum of the cubes of the digits is
On the other hand, if the number 371 is entered as input, the sum of the cubes of the digits
is
33 + 73 + 13 = 27 + 343 + 1 = 371
The program will be repeated until input number is equal to the sum of the cubes.
Name :
Student ID :
Date :
Name :
Student ID :
Date :