A. Write a function that takes an array of ints, and the size of the array – ano
ID: 3623785 • Letter: A
Question
A. Write a function that takes an array of ints, and the size of the array – another int. It returns a double. Call this one ‘averageArray.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92}
The code below didn't work
#include <stdio.h>
#include <stdlib.h>
double averageArray(int a[7], int len){
int i,sum;
for(int i=0;i<len;i++){
sum=sum+a[i];
}
return((double)sum/len);
}
main(){
int i;
int per[20];
for(i=0; i<7; i++){
printf("enter the numbers");
scanf("%d", &per[i]);}
printf("avg: %f",averageArray( per,7));
system("pause");
}
Explanation / Answer
please rate - thanks
I cleaned it up a bit, the only real problem was to change int i=0 to i=0 in the function
#include <stdio.h>
#include <stdlib.h>
double averageArray(int a[], int len){
int i,sum;
for(i=0;i<len;i++){
sum=sum+a[i];
}
return((double)sum/len);
}
main(){
int i,n;
int per[20];
printf("How many numbers do you have? ");
scanf("%d",&n);
for(i=0; i<n; i++){
printf("enter number %d: ",i+1);
scanf("%d", &per[i]);}
printf("avg: %.3f ",averageArray( per,n));
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.