Question
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.
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 statement Initialization 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 requires reading 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 loop has 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 once already. 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 of a post-test loop (C++ do..while loop) is one. CSC170L Lab Exercise Week 10 Lab: Repetition Control Structures 2 Student Name: Exercise I. Problem Analysis All problems described below require the implementation of a repetition control structure. Analyze each problem then, tell which kind of loop is most appropriate to solve the problem. Give you answer by placing an in the proper box (check only one on each row). Also, give your reason for choosing the particular Loop Structure. The Best Looping Structure for the Problem Problem Description: a program is needed to Pre-Test while Loop Pre-Test for Loop Post-Test do..while Loop Compute the number of even numbers in a file. The file may be empty. Find the largest and the smallest numbers in a list of 500 integers. Find and display the smallest in a list of non-zero numbers entered by an operator. An input of zero (0) is an indication of no additional input will be given. At least one non-zero input will be given. Display a menu of choices for the user to choose from. A selection of option 6 will end the program. The user may enter 6 at any time. CSC170L Lab Exercise Week 10 Lab: Repetition Control Structures 3 Exercise II. Repetition Control Structure and C++ do..while Statement 1. Identify each of the five elements of a loop in the following C++ program segment : int num1; int num2; num1 = 1; num2 = 2; cout << "Before the loop, num1 is " << num1 << endl; cout << "Before the loop, num2 is " << num2 << endl << endl; do { cout << num1 << " + " << num2 << " = " << num1 + num2 << endl ; num2 = num2 + 3; } while (num2 <= 10); cout << " After the loop, num1 is " << num1 << endl; cout << "After the loop, num2 is " << num2 << endl; 2. What type of loop the above code segment has? Circle one: (A pre-test , A post-test loop) 3. Hand-trace the code segment in Exercise II.1). Then, write down the final outputs produced by it: Elements of the loop Your Answer Loop Control Variable (LCV) Initialization of LCV. Please give statement(s) Repeating Condition Loop Body Please give statement(s) LCV Iteration Please give statement(s) CSC170L Lab Exercise Week 10 Lab: Repetition Control Structures 4 4. Nested Repletion Control Structure Example: Rewrite the following C++ nested for loops into nested while loops. A C++ Code Segment Trace the For Loops for (int I = 5; I > 0; I = I - 2) { cout << "I is " << I << endl; for (int J = 1; J < 5; J = J + 2) cout << " J is " << J << endl; } CSC170L Lab Exercise Week 10 Lab: Repetition Control Structures 5
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started