How to find the Average Values of all the Array Elements in C#?
Answers
Answered by
0
Minimum change solution:
float grades_average(float grades[7]){
int i;
float sum = 0;
float average = 0.0;
/* calculate the sum of grades using for loop*/
for(i = 0; i < 7; i++){
sum = sum + grades[i];
}
average = sum/7.f;
return average;
}
Change for(i = 0; i <= 7; i++){ to for(i = 0; i < 7; i++){. Valid indicies for grades are only 0-6. 7 is out of bounds.
Change sum = sum + grades[7]; to sum = sum + grades[i]; You need to check each element, not the (beyond) last one over and over.
Change average = sum/7; to average = sum/7.f; The .f ensures no integer division. That preserves the decimal during division.
I hope that helps!
Similar questions