The purpose of this code is to know if a grade is below or above the average mark of a set of marks. However, there are some points to consider:
-
There is no operator div
in C. Use the /
division operator.
-
It does not make sense for the average to be whole.
-
Your way of averaging is wrong. There is no point dividing an array by a number. What would make sense would be to add all the elements of the array and divide the result by a number or divide the value of all elements of the array by the same number.
-
If you are assigning your_points = 80;
to the start of the function, then the last parameter would be useless.
-
It's one thing to average and say whether a note is above or below it, and another thing is to put a new value into an array. If the function name says that she does something, she should do nothing more than what she says she does. This function should not change the given array.
-
This for
is wrong, it is not calculating anything. You are only comparing note 80 to each of the notes given in the array and producing a sequence of True
and False
without even looking at the calculated average (whatever that is).
-
If the function returns a int
, then it should have a return
somewhere.
What you need to fix this is simple:
Add all elements of the array.
int sum(int nums[], int size) {
int total = 0;
for (int i = 0; i < size; i++) {
total += nums[i];
}
return total;
}
Calculate the mean by dividing the sum of the array elements by the size of the array.
double average(int nums[], int size) {
return sum(nums, size) / (double) size;
}
Make sure your grade is above or below average:
int better_than_average(int nums[], int size, int your_points) {
return your_points > average(nums, size);
}