0% found this document useful (0 votes)
9 views3 pages

C++ Repetition Structures Tutorial

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

C++ Repetition Structures Tutorial

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

Common questions

Powered by AI

Systematic iteration allows structured traversal and processing of data sets, fundamental in engineering applications for tasks such as simulations, data analysis, and resource management. It ensures uniform application of operations across elements, supporting scalability, enhancing performance, and allowing complex functionality to be implemented efficiently .

The 'for' loop initializes 'count' to 1, evaluates 'num' using the formula 'num = 3 * (count − 1) + (y − count)', and increments 'count' until it exceeds 5. The output sequence is calculated by plugging 'count' values (1 through 5) into the formula, resulting in outputs: 0, 2, 4, 6, 8, followed by 'count', which is 6 .

To output prime numbers up to a given number, a nested loop structure is efficient. An outer loop iterates through each number, while an inner loop checks divisibility for each number up to its square root. If a number is only divisible by itself and one, it is prime. 'For' loops efficiently manage both the range iteration and divisibility checks, ensuring a systematic prime number generation .

Sentinel values signal termination of input collection in loops without requiring a fixed iteration count. In the grading program, the sentinel value of -1 ends grade entry, allowing the loop to exit gracefully after computing min, max, and averages from valid inputs only, ensuring meaningful output despite unknown input sizes .

Repetition structures allow sections of code to process data systematically, repeating operations across varying data sizes. This flexibility enables the program to handle dynamic inputs efficiently, perform iterative calculations, and manage control flow in complex scenarios without manually rewriting code for each data instance .

The 'do...while' loop executes its body at least once before checking the condition, thus ensuring one iteration even if the condition is initially false. In the given code, 'num' starts with 'x' as 10.5, producing values 24, 26, 28, 30, and 32 as 'x' increments from 10.5 to 14.5 before the loop exits when 'x' is no longer less than 15 .

Using loops to break down sums into denominations ensures minimal coins by iteratively subtracting the largest possible denomination before moving to smaller ones. A 'while' loop can reiterate the subtraction until the quotient is zero, efficiently minimizing coin usage through integral division and modulus operations, adapting dynamically to input amounts .

Key factors include whether initial checks are necessary ('while' and 'for'), guaranteeing at least one execution ('do while'), known iteration counts upfront ('for'), or conditional looping where condition checking is separated from looping body execution ('while'). Each structure's characteristics should align with specific program logic to ensure efficiency and readability .

In the program, input validation is achieved by setting conditions that only accept grades between 0 and 100. Invalid inputs are rejected by the loop, continuing to prompt for new input until a valid grade is entered or the sentinel value of -1 is detected, at which point the loop concludes, calculating and displaying the min, max, and average of the valid inputs .

The output of the code is 'i =5 and temp =120'. This is because the loop increments 'i' from 1 to 5, and 'temp' is repeatedly multiplied by the current value of 'i'. The final calculation of 'temp' is 1*2*3*4*5=120 .

You might also like