Invalid operators in a binary expression C

Asked

Viewed 127 times

0

I have a problem here I’m trying to average my vector class_points, but it gives me a mistake and I do not know how to solve. Someone could enlighten me a little?

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");
    }
  }
 }

Clang output 3.6 / C11

solution.c:9:35: error: expected ';' at end of declaration
  int class_average = class_points div class_size;
                                  ^
                                  ;
1 error generated.
  • Where did you get this div? I think what you wanted was to use / in his stead. In C, the division of integers is an entire division.

1 answer

1


The purpose of this code is to know if a note is below or above the average note of a set of notes. However, there are some points to consider:

  • There is no operator div in C. Use the operator / division.

  • No sense the average being whole.

  • Your way of calculating the mean is wrong. It makes no sense to divide 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 else divide the value of all the elements of the array by the same number.

  • If you are assigning your_points = 80; right at the start of the function, then the last parameter would not be serving for anything.

  • It is one thing to average and tell whether a note is above or below it and quite another to put a new value in an array. If the name of the job says she does something, she shouldn’t do anything more than what she says she does. This function should not change the given array.

  • That one for is just comparing the 80 note with each of the notes given in the array and producing a sequence of True and False without even looking at the calculated average (whatever).

  • If the function returns a int, then she should have one return somewhere.

What you need to fix this is simple:

  1. Add all array elements.

    int sum(int nums[], int size) {
      int total = 0;
      for (int i = 0; i < size; i++) {
        total += nums[i];
      }
      return total;
    }
    
  2. Calculate the mean by dividing the sum of the array elements by the array size.

    double average(int nums[], int size) {
      return sum(nums, size) / (double) size;
    }
    
  3. Check if your grade is above or below average:

    int better_than_average(int nums[], int size, int your_points) {
      return your_points > average(nums, size);
    }
    
  • Thanks for the explanation!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.