Functions average

I know this is a simple problem but I know i'm missing something in the array but not sure what.

The assignment:
Write a function that takes an array of ints, and the size of the array (an 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}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<stdlib.h>


double averageArray(int array[7])    {
	double result;

	result = (double)array[7] / 7;

	return result;
}

main()  {
	int array[] = { 78, 90, 56, 99, 88, 68, 92 };

    averageArray(array);

	system("pause");
}
result = (double)array[7] / 7;You are accessing element with index 7 which does not exist (your array contains 7 elements with indexes of [0,6]) and it is illegal and can lead to problems.

Write a function that takes an array of ints, and the size of the array (an int)
You wrote a function that can take only arrays of 7 ints, which is not what assigment requires

average of the values in the array
Average is a sum of all values divided on values count, not some arbitrary value divided on count
Topic archived. No new replies allowed.