Average with unknown amount of input

I have an assignment for class that I can't get started on. I can't find anything similar on the web.

"In this part you will write a program that reads a specified number of floating point numbers from the user and prints their average. The first number input will be an integer that determines how many numbers are to be averaged. The rest of the input will be floating point numbers to be averaged. Output should be accurate to three decimal places."

I am really hung up on how to make the first integer determine how much input for the second set of numbers. Can someone help?

Sample interaction:

Please enter the size of the data set: 8

Please enter your data: 1.39 5.5 9.6885 3.198 23.58684 17 -6.58 16.35

average = 8.767


Thanks!
I don't think I understand your hangup. The user will input an integer. You will store this value in a variable which represents the number of floating point number to average.

I don't now where you need help. Do you not understand user input? Do you not understand variables? Do you not understand how to create the variable(s) needed for the floating point values?

Why don't you write some code showing us what you know have so far. Then we can give you more specific help.
The hangup is only on making the input for the numbers to be averaged the number that the user wants.

I just need a little help on that. What is it called or a sample code? Just a small hangup
I think you are looking for an array of values. This will give you a number of elements of the same type. See this tutorial:

http://cplusplus.com/doc/tutorial/arrays/

Also this one, with an emphasis on the Pointers and arrays section

http://cplusplus.com/doc/tutorial/pointers/

Because you don't know the number you will need, you will need to dynamically allocate the array at run time. see this tutorial:

http://cplusplus.com/doc/tutorial/dynamic/

If you want to use the C++ Standard Template Library (STL) classes, you could use a vector (http://cplusplus.com/reference/stl/vector/), a list (http://cplusplus.com/reference/stl/list/) or a deque (http://cplusplus.com/reference/stl/deque/) instead. These container classes can be used instead of an array, and give lots of flexibility and added functionality, but as a beginner, you should probably learn how arrays work first.
Last edited on
Thanks!
There is no reason to use an array, dynamic or otherwise, to solve this problem. Just keep a running total and divide it by the number of inputs at the end to get the average.
An array is overkill, doing more work than is necessary.

All that is needed is an accumulator. Set it to zero.

Then get from the user the number of values to input.

Having done that, the key point is to use a loop. Since we want to loop a known number of times (the user just told us, so it's not unknown) a for loop is ideal.

Within the loop, read each value from the user. Add it to the accumulator.

When the loop finishes, we have input and accumulated all the values. All that remains is to calculate the average (a simple division) and output the result.

If you take what I've described above and turn it into C++ program code, you should have a solution. (I missed out the part where a message or two are output to prompt the user, you need to do that before attempting to get the input).
Last edited on
You're both right. What was I thinking? No array is needed.

(Back to my real job)
Topic archived. No new replies allowed.