Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Harith Ikhwan Lab 6 - Repetition Part 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Lab 7

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 << “ ”;
}

Write the statement/expression correspond to the loop operation.

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

n n <= 5 n++ cout << n


1 true 2 2
2 True 3 3
3 True 4 4
4 True 5 5
5 True 6 6

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

State whether this loop is counter-controlled or sentinel-controlled.

int item, sum = 0, data = 1;


while (sum <= 250)
{
cin >> item;
data = data * item;
sum = sum + data;
cout << sum << endl;
}

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

Type the following program. Save your program as lab7_prog1.cpp.

//Program Name: lab7_prog1.cpp


//to display number between 10 down to 1
#include <iostream>
using namespace std;

int main()
{
int counter;
counter = 1;
while(counter <= 10)
{
cout << counter << “ ”;
counter = counter + 1;
}
return 0;
}

Program 7.1

a. What is the output?


1 2 3 4 5 6 7 8 9 10

b. What is the name and initial value of loop control variable?


counter= 1

c. What is the expression used to evaluate the loop control variable?


counter<=10

d. What is the assignment statement used to update the loop control variable?
counter=counter+1

e. What is the purpose of this program?


Print a number form 1 to 10
Exercise 2

Type the following program. Save your program as lab7_prog2.cpp.

//Program Name: lab7_prog2.cpp

#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

Type the following program. Save your program as lab7_prog3.cpp.

//Program Name: lab7_prog3.cpp


//This program is to display the sequential number and the total
//of numbers
#include <iostream>
using namespace std;

main()
{
int sum, number;
sum = 0;
cin >> number;
while (number != -1)
{
sum = sum + number;
cin >> number;
}
cout << sum << endl;

Program 7.3

a. What is the program output if the given inputs are 5 10 20 -1?


35

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

c. How many times the while statement will be executed?


Until the enter value is -1
Exercise 4

Type the following program. Save your program as lab7_prog4.cpp.

//Program Name: lab7_prog4.cpp


#include <iostream>
using namespace std;
main()
{
int i = 15, sum = 1;
while(i <= 27)
{
if (i % 3 == 0)
sum = sum + i ;
cout << i << “ ”;
i = i + 2;
}
cout << sum << endl;
}
Program 7.4

a. What is the output?


15 17 19 21 23 25 27 64

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

Type the following program. Save your program as lab7_prog5.cpp.

//Program Name: lab7_prog5.cpp

#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

a. What is the program output if the user enters 1234 as input?


1234 has 5 digits

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.

//question post lab 1


#include<iostream>
using namespace std;

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;

cout << "Enter a positive integer: ";


cin >> n;

// 0 and 1 are not prime numbers


if (n == 0 || n == 1) {
is_prime = false;
}

// loop to check if n is prime


for (i = 2; i <= n/2; ++i) {
if (n % i == 0) {
is_prime = false;
break;
}
}

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

53 + 63 + 33 = 125 + 216 + 27 = 368

which is not equal to 563.

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

which is equal to 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 :

You might also like