Find max and average of multiple values

This is an assignment I have to do. Please help, I tried, but have no idea how to do it:

Write a program in C++ to find the maximum of a set of values and its average. The user should be able to feed the total number of values in the set, and then user will be prompted to enter the values. After feeding the whole set of values, the program will find the maximum value, number of values, the average value and display them on the monitor.


First, what type of variable do I use to input multiple values?
Second, how do I store multiple values?
Third, how do I output multiple values?



Last edited on
Although arrays can be used, they are not necessary given your problem. You do not actually need to store all the values that the user enters, you only need to process each one as it is entered.

The number of values is simple to find: it is entered by the user at the start as specified in the problem.

To find the maximum and average, you will need to keep track of a few things (current maximum and total sum) as you get input from the user. Let's take an example, say the user wants to enter 4 values: 7, 8, 2, 11. Assuming the user will always enter at least 1 value, you can treat the first value as a special input. In this case '7' is your first value, and you can make it your current maximum as well as initialize your total to 7. Then you can loop for the remaining values. The user enters 8, so you can check this against your current maximum and update it to 8. Also, update your total to 15. The user enters 2, which is less than your current maximum, so you don't need to update it, but you add 2 to the total giving you 17. Finally the user enters 11, you can update your maximum to 11 and add it to your total giving you 28.

Once the loop is over you will have your maximum and total. You can then divide the total by the number of inputs to get your average. Voila!
First, what type of variable do I use to input multiple values?
You can turn string/template for help.

Second, how do I store multiple values?
If you use template in previous,you will suppose have to use your own class and reload < for comp.Furthermore,static_cast<type> will help you in type conversion.
If you use string intead, try to use vector<string>,and stringsteam will help you convert string to int,float,etc.

Third, how do I output multiple values?
You have to know,if you clearly know how you input your info,you can also simply get and output them by yourself.

Hope this will be useful.
Do you understand what mahlerfive has said? Its really just a simple for loop which keeps progressive track of desired output. Post your code if you are still having trouble.
Topic archived. No new replies allowed.