mercredi 6 novembre 2019

Finding Average In An Array Of Numbers Using Function

I want to prompt the user to enter the values in the array and the size. But something went wrong.

 #include <stdio.h>  

 double calAvg(int arr[], int size); 

 int main(void) {   
    int n = 5;   
    int arr[n];   
    int num;   
    double numAverage;   

    printf("Enter the numbers of average: ");   
    scanf("%lf", &numAverage); 

    printf("Enter the numbers: ");   
    scanf("%d", &num);   

    double average = calAvg(arr, num);   
    printf("Average of entered numbers are %.2lf\n", average);

    return 0; 
 } 

 double calAvg(int arr[], int size){   
   double total = 0;  
   double average;  

   for(int i = 0; i < size ; i++){
       total += arr[i];
       average = total/size; 
   } 
   return average; 
}

Aucun commentaire:

Enregistrer un commentaire