How can I write this program in c++?

Write a program that reads integer numbers from the user until their amount exceeds 100 or the number of readings is 20.
Then the screen shows how many numbers are read, how much is their amount and how much their arithmetic mean.
Do you know
- how to get one integer from the user?
- how to write loops?
- how to write if-statements?

Build your program one step at at time.

how much is their amount

What does this mean? Should it display each individual item back to the user, or do you want the sum or something?

Anyway, check out the site's tutorial http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/doc/tutorial/basic_io/
http://www.cplusplus.com/doc/tutorial/control/
The program first needs to read from user number. I will do this with for loop, but it says thay when the numbers that are read reach the sum of 100 it need to display how many numbers has been read and how much is their sum and the average. Ex. Lets say that the first 4 numbers will be 25 at the fourth number the program will stop and will dispay that 4 numbers has been read their sum is 100 and the average.
But the program need to work till the sum of numbers reach 100 or the numbers we have read are 20.
I think you're expected to read the numbers into an array of size 20. But you'll need to keep track of how many numbers you've read, because you may read 2 numbers that bring you over the 100 sum limit.

You'll also need to keep track of the sum as you loop, reading values and appending them to the array (so you can tell if you've passed 100).

Once you have read the numbers, you then need to find the mean, and print the required values.
Yes that's what I need to do. Or I can use a function. Can you tell me how to do it?
I see no need for an array.

"work till the sum ..."
can be said:
WHILE count is less than 20 AND sum is less than 100 AND we manage to read a number
DO add the number to the sum and increment the count

show count
show sum
calculate and show average


Read the tutorial pages that Ganado did show.


Are the numbers whole or decimal values?
If they are whole, then you should read integers and the sum should be integer too.
The count must be an integral value; nothing else makes sense.

If you divide an integer value with an integer value, the result is an integer too, even if you would store that value into double variable. Integer is whole; no fraction. For example: 299/100 is 2.

One can shift to floating point division by converting integer to float:
static_cast<double>(299) / 100 is about 2.99.
Im not going to write a second one for you as you will never learn this way. The other one was only 3 lines, but this one uses many of the same principles.

They already said it... loop, read, check for > 100, keep a running total and get the new average … count to 20. So really just glue together 4 simple things (count, average, sum, and 100 test).

Post what you have. You can do this.

I solved it. This made it clear for(i=0; i<20 && sum<100; i++)
Topic archived. No new replies allowed.