Problem with float and matrix

Asked

Viewed 543 times

0

I have to add the values that have two or more decimals but when I put by Exp: "1.0", it fills this whole row automatically and jumps to the next, I reviewed and I don’t know why you are giving error so I appreciate if I can help.

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

int main () { setlocale(LC_ALL,"Portuguese");

    float matriz[7][4], soma = 0;

    for ( int i = 0; i < 7; i++){

        for ( int h = 0; h < 4; h++){
            printf("\nInforme o valor da linha %i e coluna %i: ",i,h);
            scanf("%f",&matriz[i][h]);
        }

        system ("cls");

    }

    for (int c = 0; c < 7; c++){
        for ( int k = 0; k < 4; k++){
            soma += matriz[c][k];
        }
    }

    printf("\nO valor é: %.0f\n",soma);

    return 0;
}

1 answer

2

You specified the locale of the program as Portuguese (called setlocale in the first line). You need to enter the number in this format - if you enter with 1,2 instead of 1.2 your program should do what you want.

  • It would be better, in my opinion, to leave the locale as predefined for input and change it only to output.

  • Cool, but how do I know it just for output ? ( e vlw Carlos, silly error of attention )

Browser other questions tagged

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