error in calculating average in my function
when i test my program for the values {78, 90, 56, 99, 88, 68, 92} with an array size of 7, as i enter the 5th value which is 88 it cuts short and completes the program and prints the average as something like 30144423.45 which is totally wrong. i cant seem to figure out what is wrong.
#include <stdio.h>
double averagearray(int values[], int size);
int main(){
int values[] = {0};
double avg;
int i;
int size;
printf("Enter the size of the array\n");
scanf("%d",&size);
printf("Please enter values to fill the array");
for (i = 0; i < size; ++i){
scanf("%d", &values[i]);
}
avg = averagearray(values, size);
printf("Average = %.2lf\n", avg);
system("pause");
return 0;
}
double averagearray(int values[], int size)
{
int i = 0;
double avg, sum = 0.00;
for (i = 0; i < size; ++i) {
sum += values[i];
}
avg = (sum / size);
return avg;
}
Posted Jul 16, 2017, 9:53 PM UTC
825