Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab: Repetition Control Structures Goal To re-enforce problem solving skill by Solving problems that require various control structures. To practice coding of various C++ looping

Lab: Repetition Control Structures

Goal

To re-enforce problem solving skill by Solving problems that require various control structures. To practice coding of various C++ looping statements in a program. Learning Outcomes

Upon completion of the Exercises, students should be able to Know the differences between pre-test and post-test loops, and determine which one is appropriate according to problem description.

Know what C++ statement should be used to implement a particular kind of loop. to implement repetition control structure using different C++ statement.

Background C++ has three looping structures:

while, for, and do..while. The syntax for each is given below:

C++ Syntax for

while statement C++ Syntax for for statement C++ Syntax for do while statementInitialization of LCV while (repeating condition)

{

Actions required repeating.

Modification of LCV

}

for (LCV=initial_value; Repeating condition; Modification of LCV)

{

Actions required repeating

}

Initialization of LCV do

{

Actions required repeating

Modification of LCV

}

while (repeating condition)

Different Types of Loops

Not All algorithms that require the implementation of loops (repetition control structure) are

created equal. An End-of-File Loop is needed when the algorithm requiresreading and processing all data in a file without knowing how many pieces of data are there in the file at the time

of algorithm development. For example, to process all student data in a file of student records. Normally, C++ while or do while statement is used for this purpose. A Sentinel-Controlled Loop is used when the number of repetitions is depended on the detection of a special value. For instance, a process is to continue until an input of zero is received. C++ while or do while statement is usually used for this purpose. A Counter-Controlled Loop is usually implemented if the number of repetitions is known at the time of algorithm development, or can be found out before execution of the loop structure. Most likely, C++ for statement is used for this purpose Besides the differences among the three different C++ looping statements described above, there are other major differences in terms of when the loops repeating condition is tested. When executing the while and for statements: the repeating condition of the loophas been test once by the computer BEFORE the loop body is executed for the first time. This is referred to as a Pre-Test Loop. It is different with the logic in a C++ do while statement, because

before the repeating condition is tested for the first time, the loop body will have been executed oncealready. That is why a do while loop is considered a Post-Test Loop. Consequently, the minimum number of repetitions of a pre-test loop (C++ while loop or C++ for loop) is zero, and the minimum number of repetitions ofa post-test loop (C++ do while loop) is one.

Exercise III.

Solving a Looping Problemwith different C++ Looping Statements

1.Write a program that

a. gets one integer parameter say N which tells the number of values that a user will enter.

b. The program will loop N times to find the largest number.

c.Output the largest number.

2.Enter the program using your chosen program IDE.

3.Save the source code (the week10_lastname_firstname.cpp file).

This is the code I have tried to work on but I think I might have done it all wrong.

#include

using namespace std;

int main() { //Declare variables int score1, score2; int x; int y; int num; int myNum; int count = 0; int sum = 0; double average; char again; do { //Prompt user for inputs cout << "before the loop, score1 is " << score1 << endl; cout << "before the loop, score2 is " << score2 << endl << endl; for (int i = 1; i < 10; i++) cout << "Enter your number Now: " << myNum << endl; cin >> myNum; while (socre2 <=10) cout << " After the loop, socre1 is" << score1 << endl; cout << " After the loop, score2 is" << score2 << endl; If (sum > myNum) { sum = myNum; } else { count++; sum += number; average = sum / (count + count); } return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Brief the importance of span of control and its concepts.

Answered: 1 week ago

Question

4. Who should be invited to attend?

Answered: 1 week ago