Invalid operators in a binary expression C

0

I have here a problem I am trying to calculate the average in my vector class_points , but it gives me an error and I do not know how to solve. Could someone give me some ideas?

int better_than_average(int class_points[], int class_size, int your_points) {
  // Your code here :)
  // Note: class_size is the length of class_points.

  your_points = 80;

  class_points[class_size++] = your_points;

  int class_average = class_points div class_size;



  for (int i; i < class_size; i++)
  {
    if (your_points > class_average)
    {
      printf("True");
    } else {
      printf("False");
    }
  }
 }

Output of Clang 3.6 / C11

solution.c:9:35: error: expected ';' at end of declaration
  int class_average = class_points div class_size;
                                  ^
                                  ;
1 error generated.
    
asked by anonymous 28.05.2018 / 21:48

1 answer

1

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);
    }
    
  • 28.05.2018 / 22:12