Finding the average of an array using c [closed]

不羁岁月 提交于 2019-12-04 04:29:42

问题


I am new to coding in C and I am trying to get the average of the array but for some reason it is not calculating, it is giving me a result of 0.0.

How can I fix this. Thanks

#include <stdio.h>
float grades_average(float grades[7]);
int main() {

 float grades[7]={98.8, 97.9, 99.3, 99.8, 99.6, 99.4, 99.9};
 float average;
 average = grades_average(grades);
 printf("Average is: %.2f",average);


 return 1;
}
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[7];
    }
 average = sum/7;

 return average;
}

The output is:

Average is: 0.00

回答1:


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;
}
  1. 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.
  2. Change sum = sum + grades[7]; to sum = sum + grades[i]; You need to check each element, not the (beyond) last one over and over.
  3. 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!




回答2:


Here is full code. with 2 changes, Line 20 and Line 23

#include <stdio.h>
float grades_average(float grades[7]);
int main() {

 float grades[7]={98.8, 97.9, 99.3, 99.8, 99.6, 99.4, 99.9};
 float average;
 average = grades_average(grades);
 printf("Average is: %.2f",average);


 return 1;
}
float grades_average(float grades[7]){
 int i;
 float sum = 0;
 float average = 0.0;
 /* calculate the sum of grades using for loop*/

   /*Changed here to <7 because i takes 0,1,2,3,4,5,6 which are 7 elements, your code is <=7 which takes 0.......7 which are 8 elements */
    for(i = 0; i < 7; i++){

       /*Changed here to i from 7, your code everytime sums out of bound element, Garbage value since grades[7] does not exist*/
       sum = sum + grades[i];
    }
 average = sum/7;

 return average;
}



回答3:


  1. Change for(i = 0; i <= 7; i++){} to for(i = 0; i < 7; i++){}, because totally you have 7 elements, starting from 0to6.
  2. Change sum = sum + grades[7]; to sum = sum + grades[i];, as grades[7] will only have the last value of the array, but you want to add all the values present in the array.

    include

    float grades_average(float grades[7]);
    int main() {
    
     float grades[7]={98.8, 97.9, 99.3, 99.8, 99.6, 99.4, 99.9};
     float average;
     average = grades_average(grades);
     printf("Average is: %.2f",average);
    
    
     return 1;
    }
    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;
    }
    


Hope that helps..




回答4:


Try by putting i in place of 7,

 sum = sum + grades[7];

like

sum = sum + grades[i];

and your loop from 0 to 6




回答5:


In place of using this

 sum = sum + grades[7];

Use

sum = sum + grades[i];

Then it will work.



来源:https://stackoverflow.com/questions/45973691/finding-the-average-of-an-array-using-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!