Loops

Not sure where to begin with this assignment...

For this lab, create a c++ source file named lab5.cpp. Add your documentation header to the file. The following exercises ask you to create 4 different types of loops – you should add these in order to your source file, until you have 4 functioning loops in your code. Direct all output to the console window. Before each type of loop, add documentation to identify the new loop. Also, send a line of output to your console window identifying which loop is creating the next series of prompts and output.

Count-Controlled While Loop:
1. Add the following code for a count-controlled loop to your source file. Save, compile, and run it, and observe the results:

int count = 1;
while (count <= 10)
{
cout << count << “, “;
count = count + 1;
}

2. Modify this segment of code in the following way. Instead of simply printing the numbers from 1 to 10, this while loop should ask the user to enter 10 integer values. The user should be prompted in a meaningful way for each value, such as “Enter number 1:”, “Enter number 2:”, etc. When the values have been entered, print the sum and average of the values.

3. Now modify this code so that the user can specify how many values are to be entered. You’ll need to ask the user for the number of values prior to starting the loop. Your prompts should stay meaningful, and after the values have been entered, print the count, sum, and average of the values entered.

Count-Controlled For Loop:
4. Create a second loop in your program (leave your first loop in your code!). This loop should do exactly what your loop from problems #1-3 does, except this loop must be implemented with a for loop structure. Note that when you run your code, you’ll be entering two lists of numbers as the user.


Sentinel-Controlled While Loop:
5. Create a third loop in your program. This loop should be a sentinel-controlled while loop; this loop should prompt the user for any number of integers until a value of 0 is entered. Declare and use a global constant for this sentinel value. Recall that the general algorithm for a sentinel-controlled loop is as follows:

get the first value from the user (this is the “priming read”)
while (the value entered by the user is NOT the sentinel value)
{
process the value
get the next value from the user
}

Once again, after all values have been entered, this program should print the count, sum, and average of the numbers.

Flag-Controlled While Loop:
6. Create a fourth loop in your program. This should be a flag-controlled loop that uses a boolean variable to control the loop. This loop should ask the user for numbers, one integer at a time. After entering a value, ask the user if they would like to continue (the user could enter a y or n to indicate whether or not they wish to continue). Print the same results as before – the count, sum, and average of the values entered.

Once you have completed all of these steps, attach your loops.cpp file here in this dropbox.
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/193645/
Topic archived. No new replies allowed.