1
Error:error: invalid operands to binary % (have ‘float’ and ‘int’)
   14 |         if(arr[i] % 2 == 0) {
      |            ~~~~~~ ^
      |               |
      |               float   

Code:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int size, i, j;
    float max, min, midpoint, sum = 0, avg, median, evenCount = 0, oddCount = 0, divisibleCount = 0;
    printf("Enter the size of the array: ");
    scanf("%d", &size);
    float arr[size];
    for(i = 0; i < size; i++) {
        printf("Enter element %d: ", i+1);
        scanf("%f", &arr[i]);
        sum += arr[i];
        if(arr[i] % 2 == 0) {
            evenCount++;
        } else {
            oddCount++;
        }
        if(i == 0) {
            max = arr[i];
            min = arr[i];
        } else {
            if(arr[i] > max) {
                max = arr[i];
            }
            if(arr[i] < min) {
                min = arr[i];
            }
        }
    }
    midpoint = (max + min) / 2;
    int lessThanMidpoint = 0, greaterThanMidpoint = 0;
    for(i = 0; i < size; i++) {
        if(arr[i] == midpoint) {
            printf("Midpoint found at index %d using linear search.\n", i);
            break;
        }
    }
    for(i = 0; i < size; i++) {
        if(arr[i] < midpoint) {
            lessThanMidpoint++;
        } else if(arr[i] > midpoint) {
            greaterThanMidpoint++;
        }
        if((int)arr[i] % (int)min == 0) {
            divisibleCount++;
        }
    }
    avg = sum / size;
    for(i = 0; i < size; i++) {
        for(j = i+1; j < size; j++) {
            if(arr[i] > arr[j]) {
                float temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    if(size % 2 == 0) {
        median = (arr[size/2] + arr[(size/2)-1]) / 2;
    } else {
        median = arr[size/2];
    }
    printf("Array size: %d\n", size);
    printf("Maximum element: %0.2f\n", max);
    printf("Minimum element: %0.2f\n", min);
    printf("Midpoint: %0.2f\n", midpoint);
    printf("Number of elements less than midpoint: %d\n", lessThanMidpoint);
    printf("Number of elements greater than midpoint: %d\n", greaterThanMidpoint);
    printf("Sum of elements: %0.2f\n", sum);
    printf("Average of elements: %0.2f\n", avg);
    printf("Median of elements: %0.2f\n", median);
    printf("Number of even elements: %0.2f\n", evenCount);
    printf("Number of odd elements: %0.2f\n", oddCount);
    printf("Number of elements divisible by minimum value: %0.2f\n", divisibleCount);
    return 0;
}

Tried: changing the "%" in line 14 to %f, %lf, %0.2f and %.2lf

Results main.c:14:20: error: ‘f’ undeclared (first use in this function) 14 | if(arr[i] %f 2 == 0) { | ^ main.c:14:20: note: each undeclared identifier is reported only once for each function it appears in main.c:14:21: error: expected ‘)’ before numeric constant 14 | if(arr[i] %f 2 == 0) { | ~ ^~ | )

main.c: In function ‘main’:
main.c:14:20: error: invalid suffix "lf" on floating constant
   14 |         if(arr[i] %.2lf 2 == 0) {
      |                    ^~~~
main.c:14:24: error: expected ‘)’ before numeric constant
   14 |         if(arr[i] %.2lf 2 == 0) {
      |           ~            ^~
      |                        )
Morten Jensen
  • 5,818
  • 3
  • 43
  • 55
jlewisadm
  • 11
  • 2
  • 3
    The operator % is not defined for floating numbers in C. You could for example cast the left operand to the type int. Though it is unclear why the array is declared with the type specifier float instead of int. – Vlad from Moscow Feb 20 '23 at 18:11
  • The suffix is L by the way. – Jason Feb 20 '23 at 18:12
  • 1
    `if(fmodf(arr[i], 2.f) == 0.f)` would be syntactically correct but I doubt it'll do what you want. – Ted Lyngmo Feb 20 '23 at 18:13
  • 4
    Is 1.999999 even or odd? – Corvus Feb 20 '23 at 18:14
  • @Corvus For an optimist the number is even. For a pessimist the number is odd. – Vlad from Moscow Feb 20 '23 at 18:17
  • `fmodf` can replace `%` for floats [or `remainder`]. But, you may want to extract the mantissa see: https://stackoverflow.com/questions/15685181/how-to-get-the-sign-mantissa-and-exponent-of-a-floating-point-number Then, you could do (e.g.) `mantissa & 1` to see even/odd. But, it's all a bit dicey because the least significant bit of a float can vary. I'd consider converting to a fixed point number of some sort. – Craig Estey Feb 20 '23 at 18:18
  • Also, using `float` (vs. `int`) for _counters_ will just complicate things. (i.e.) you want: `int evenCount = 0, oddCount = 0;` with adjustments to the corresponding `printf` calls. – Craig Estey Feb 20 '23 at 18:25
  • 2
    You need to post the actual problem statement. I doubt that you are supposed to have an array of `float`. – user3386109 Feb 20 '23 at 18:27

1 Answers1

2

Remainder

The binary operator % yields the remainder of the division of the first operand by the second (after usual arithmetic conversions).

The sign of the remainder is defined in such a way that if the quotient a/b is representable in the result type, then (a/b)*b + a%b == a.

If the second operand is zero, the behavior is undefined.

If the quotient a/b is not representable in the result type, the behavior of both a/b and a%b is undefined (that means INT_MIN%-1 is undefined on 2's complement systems)

Note: the remainder operator does not work on floating-point types, the library function fmod() provides that functionality. — cppreference.com

Mark Dickinson
  • 29,088
  • 9
  • 83
  • 120
Harith
  • 4,663
  • 1
  • 5
  • 20