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
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.
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).