Tutorial 7
Tutorial 7
Spring, 2020
Repetition
There are three different kinds of loops available in most programming languages. Each loop
can be used to meet the specification of the program even with their differences. The three
different loops you will be using in C++ are the while, do while, and for loops.
while(condition)
{ body }
do
{ body }
while(condition);
Practice examples:
What is the output of the following C++ code?
a) int i = 0;
int temp = 1;
while (i < 5){
i = i + 1;
temp = temp * i;
}
cout << "i =" <<i<< "and temp ="<<temp<<endl;
b) int num = 0;
int count;
int y = 0;
for (count = 1; count <= 5; ++ count){
num = 3 * (count ‐ 1) + (y ‐ count);
cout << num << " ";
}
cout << count << " " << endl;
Tutorial 7 – due: June 3, 2020
c) double x=10.5;
int num;
do {
num = x * 2 + 3;
x = x + 1;
cout <<num <<" ";
}while(x < 15);
Programming Practice
a) Write a program which allows a teacher to determine the min, max, and average grade from
grades entered. The teacher will continuously enter grades until the sentinel is specified. For
this example, a grade of -1 will be used to denote grading is completed. The min, max, and
average grade should then be displayed to the teacher. This program should not accept invalid
grades to be used [0, 100].
b) Write a program which outputs prime numbers up to a specified number. For example, if a user
entered the number 25, the program would output the prime numbers between 1 and 25.
c) Write a program which converts a sum of currency into the highest available denominations in
terms of coins. For example, the user inputs the number 4.25. The program would output that
it takes 2 Toonies and 1 Quarter. Ensure that the result uses the least amount of coins possible.
This can be solved mathematically or using loops.