How to get the average in an array

it a an C++ and it need to accept an array as a parameter and loop though the array adding the array elements and return their sum divided by the number of elements used. Am confused on changed it to find the average added on with the highest and lowest number.

1
2
3
4
5
6
7
8
9
10
{  
   int i;   
   
   max = min = v[0];
   
   for (i = 1; i < n; i++) ]
   {  
      if (v[i] > max) max = v[i];
      if (v[i] < min) min = v[i];
   }
The average need to be adding all the numbers in v[] buffer and dividing by total number of elements in v[] buffer.
can u give me an example please
//for example
v[0] =11;
v[1] =12;
v[2] =13;
v[3] =14;
v[4] =15;

//five element in v[] buffer .
for(int i =0 ; i < 5; i++)
{
int sum += v[i];
}
int average = sum / 5; // As there are 5 element in the array .

didn“t you have mathmatics in school O?o...
bluecoder:
1
2
3
4
for(int i =0 ; i < 5; i++)
{
int sum += v[i];
}


We better think about this again *SCRN* (hint: what is the scope of sum?)

Ciao, Imi.
Topic archived. No new replies allowed.