+ 1
Given a list of N integers, find its mean (as a double), maximum value, minimum value, and range.
Yeah, some help here guys
4 Réponses
+ 3
Please show us your attempt.
+ 3
You first write down the definition. Then from the definition, translate it into code.
+ 2
Kindly show us your attempt first. This way you can learn more about things and improve. If you are stuck or have a doubt, feel free to ask about it here.
The problem require you to simply iterate over the array to find the maximum and minimum element respectively. The range is defined by [min,max], where [ and ] signify inclusion. Also, during the iteration, keep adding values of every element to a variable sum to get the average (average=sum/size).
These problems can also be solved using standard library functions. Assuming that your data is in an array a[] of size n:
0) For finding the maximum and minimum element of a range/container:
auto data = std::minmax_element(a,a+n);
The variable data is a pair, where the first element is the minimum value and the second is the maximum value.
1) For finding average of the range of elements:
double average = (std::accumulate(a,a+n,0)/static_cast<double>(n));
Accumulate calculates the sum of the elements in the range. This is then used to get the average.
0
Show us your attempt