0% found this document useful (0 votes)
3 views

Tutorial 7

This document outlines a tutorial for ENGR 1200U, focusing on the use of repetition structures in C++ programming. It describes three types of loops: while, do while, and for, and provides practice examples and programming tasks related to these concepts. The tasks include calculating grades, outputting prime numbers, and converting currency into denominations.

Uploaded by

harshkuddu63
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Tutorial 7

This document outlines a tutorial for ENGR 1200U, focusing on the use of repetition structures in C++ programming. It describes three types of loops: while, do while, and for, and provides practice examples and programming tasks related to these concepts. The tasks include calculating grades, outputting prime numbers, and converting currency into denominations.

Uploaded by

harshkuddu63
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Faculty of Engineering and Applied Science

ENGR 1200U: Introduction to Programming for Engineers

Spring, 2020

Repetition Practice in C++

Due: Wednesday June 3, 2020


Objective
The objective of this tutorial is to understand the use of repetition structure in C++. This allows
for different sections or methods to be repeated systematically over a group of data and
operate on different sizes of data. This will be applied to C++ examples which will allow more
flexibility in your programs.

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.

for(initialization ; condition ; iteration)


{ body }

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.

You might also like