Loops

Not sure where to start 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.

This is not a homework site. We won't do your homework for you. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again. As it is impossible to find derivative of function without knowledge in arithmetic, you cannot do more complex tasks in programming without clear understanding of basics
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/193644/
AbstractionAnon if your not gonna comment anything useful then dont comment on my shit and waste my time
Hmm... well I would begin where the assignment says. Put the first loop in the code as asked and observe its behavior. What do I learn from it? How does each line of code work together to produce the results. What is the purpose of each line of code? Then proceed to step 2 using the things I have learned in class about asking the user for input. How would I add the number entered to the current sum to get the final result?

Each of these parts of the assignment then adds more the the loop.

AbstractionAnon didn't waste your time. He was explaining the best way to get help from the forum on homework. Most (if not all) people here feel as he does... we aren't going to do your work for you (some may if you pay them lol). We will help you with specific questions or problems along the way. The only question so far that you have posed which he might have answered is "where do I start?" Well... hopefully I have answered that question.
Last edited on
As this is an assignment I'm assuming you have an instructor. I would begin with asking them. If you are still confused after that I would advise consulting the wiki or your text book. Then if you are still confused on a specific problem that the other resources could not answer then the forum might be useful rather than just copy pasting the entire assignment in here and hoping someone will do it for you.
closed account (E0p9LyTq)
@jtm013,

From the "Welcome -- read before posting!" sticky announcement
( http://www.cplusplus.com/forum/beginner/1/#msg6680 )

Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.


With that said, do you have any code you worked on you could post for us to look at?
Last edited on
Topic archived. No new replies allowed.