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

Asked

Viewed 10,163 times

2

I am beginner in programming in c, I have here a problem the exercise and calculate the average of the odd in a matrix that receives real numbers. However, I’m having the following mistake:

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?

  • tried this too, has similar error. error: invalid operands to Binary % (have 'float' and 'double')

  • First explain to me what is a remnant of float. In mathematics this does not exist.

2 answers

1

The remaining operator of the division, the %, also called a module, can only be used between integers. In the code is being used between floats, here:

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

For the matrix a was created with the type float.

To do the same operation between floats it is necessary to use the function fmod library math.h:

#include <math.h>

...

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

Note that variables of type float or double, may present some inaccuracies in the stored values, and therefore a if of this kind may not always work.

Job documentation fmod if you want to consult

0

According to the website http://www.matematica.pt/faq/numero-par.php: "First the definition of PAR number can only be applied to integers. That being said, a number is even if when divided by the number two, the result is an integer number. Using mathematical language, "even is every number that can be written in the form 2n", with n belonging to the set of integers" So 8 is even because 2 x 4 = 8.

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

See the code below. I made small modifications, study the code and try to do it yourself. 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

See the program output when running: Saída do código acima

Browser other questions tagged

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