How to make the remainder of the division of a float by a number in c?

1

I am a beginner in programming in c, I have here an exercise problem and calculate the mean of the odd ones in a matrix that receives real numbers. However, I'm having the following error:

  

error: invalid operands to binary% (have 'float' and 'int')

Here is the code:

float mediaImpar(float a[4][4]){
    int count=0;
    float soma=0;
    for(int l=0;l<2;l++)
        for(int c=0;c<2;c++){
            if(a[l][c]%2!=0){
                soma=a[l][c]+soma;
                count++;
            }}
     float media=soma/count;
     return media;
    //return countPrimo;
}
int main(int argc, char** argv) {
    float numero;
    float a[2][2];
    for(int l=0;l<2;l++){
        for(int c=0;c<2;c++){
            printf("Digite na linha %d e coluna %d \t", l, c);
            scanf("%f",&numero);
            a[l][c]=numero;
        }
    }
    float m=mediaImpar(a);
    printf("%f",m);



    return (EXIT_SUCCESS);
}

How to solve this problem, and what is the cause?

    
asked by anonymous 17.07.2017 / 17:11

2 answers

1

The rest of the division operator, % , also called a module, can only be used between integers. Node code is to be used between floats , here:

if (a[l][c] % 2 != 0){
//    ^-float ^-int

Because the a array was created with type float . To use the same operator in floats you must use the fmod function of the math.h library:

#include <math.h>

...

if (fmod(a[l][c], 2) != 0){

Note that values of type float or double may have some inaccuracies in stored values, so a if of that type may not always work depending on the value that is there.

Function documentation fmod if you want to consult

    
17.07.2017 / 17:39
0

According to the site link : "First, the definition of PAR number can only be applied to integers, so a number is even if being divided by number two, the result is an integer. written in the form 2n ", with n belonging to the set of integers" Thus 8 is even since 2 x 4 = 8.

One way to analyze this in programming is through the much-used MOD function or the remainder of the entire division. If the remainder of a number divided by two is zero (0) it will be even! Otherwise, if the remainder of the entire division by 2 is different! = From zero the number will be odd.

See the code below. I made small modifications, study the code and try to do it myself. Good Study

/*Calcula a media dos ímpares numa matriz que recebe números INTEIROS*/
#include <stdio.h>
#include <stdlib.h>

float mediaImpar(int a[4][4]); //protótipo da função

int main(int argc, char** argv) {
    int numero, linha, coluna;
    int matriz[4][4]; //declara uma matriz 4 x 4 com linhas e colunas 0,1,2 e 3
    float media = 0.0;
    for(linha = 0; linha < 4; linha++){
        for(coluna = 0; coluna < 4; coluna++){
            system("CLS"); //limpa a tela
            printf("Entre com o elemento matriz[%d][%d]: ", linha+1, coluna+1);
            scanf("%d", &matriz[linha][coluna]);

        }//fim for
    }//fim for
    media = mediaImpar(matriz); //chama a função
    printf("====A matriz digitada====\n\n");
    for(linha = 0; linha < 4; linha++){
         for (coluna = 0; coluna < 4; coluna++){
             printf("%2d ", matriz[linha][coluna] );
             if (coluna == 3)
               printf ("\n");
         } //fim for
    } //fim for
    printf("\nMedia dos números impares da matriz: %.2f",media);

    return (EXIT_SUCCESS);
} //fim main

float mediaImpar(int a[4][4]){
    int count = 0, i, j;
    int soma = 0;
    for(i = 0; i < 4; i++){
        for(j = 0; j < 4; j++){
            if(a[i][j] % 2 != 0){
                soma += a[i][j]; // o mesmo que soma = soma + a[i][j];
                count++;
            }//fim if
         }//fim for
     }//fim for
     float media = (float)soma/count; //cast para que o resultado da media seja float
     return media;

} //fim mediaImpar

View the output of the program when it is run:

    
18.07.2017 / 15:16