Need to change from max value to average

Hi!!
i did a program with array and with funcion to find a max value ( got help with it a little) now i need to do the same one only to calcuate the average using float insted. can i get a little help with it??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  #include <iostream>

using namespace std;

int max(int v[] , int n)//here its suposed to be "float average (int v[], int n)

{
   int current_max = v[0];
   for(int i=0; i<n; i++) {
      if (current_max < v[i])
         current_max = v[i];
   }

   return current_max;
}
int main(void)
{
   int arr[10] = {1,2,3,4,5,6,7,8,9,10};
   int maxValueInArr;

   maxValueInArr=max(arr, 10);
   cout << maxValueInArr;

   return 0;
}
Well an average is in some ways simpler than finding the maximum value. There are no if-statements required (with one possible exception).

Create a variable of type float to store the total value. Set its initial value to zero.

Loop through each value in the array, and add it to the total.

After the end of the loop, calculate the average which is simply total divided by number of elements. Return that value.

Beware of possible pitfalls. Even though each element is of type int, and you might use an integer for the total, you'd then have integer division, which would give an inaccurate result. Other possible problem - if the number of elements is zero, you might get a divide-by-zero error.
Last edited on
i did this but when i try it to change it to float or in my function i cant get it to work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;
 
int main()
{
    int sum = 0;
    int average = 0;
    int array[10] = {1,2,3,4,5,6,7,8,9,10};
    for (int i = 0; i < 10; ++i)
        sum+=array[i];
    average = sum/10;
    cout<<"Average:"<<average;
}
Well, I was hoping you would modify the function, rather than main().
But still, your latest example doesn't work because of two problems.
line 12
sum is type int
10 is type int
calculation is done using integer division, which means the decimal part of the result is lost.

Second problem, also line 12
result is assigned to variable average which is type int
That means even if floating-point division was done, the result would still be truncated to an integer, losing the decimal part.

Also, there's no need to declare the variable average at the start, wait until you are ready to assign a proper value to it.

Here I used a constant named size instead of the literal 10. This makes the code easier to maintain, if the array size needed to change, you'd only have to change one place instead of three.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;
 
int main()
{
    const int size = 10;
    
    int array[size] = {1,2,3,4,5,6,7,8,9,10};

    float sum = 0;
    
    for (int i = 0; i < size; ++i)
        sum += array[i];
    
    float average = sum/size;
    
    cout << "Average:" << average;
}



Topic archived. No new replies allowed.