The program is not reading the matrix numbers

Asked

Viewed 900 times

-1

I’m doing exercises on Uri online Judge, this asks:

In this problem you should read a number, indicating a row of the matrix on which an operation must be performed, a capital character, indicating the operation that will be performed, and all the elements of a matrix M[12][12].

Then calculate and show the sum or the mean of the elements that are in the green area of the matrix, as the case may be. The first input line contains an L number indicating the line that will be considered for operation. The second input line contains a single Uppercase character T (’S' or’M'), indicating the operation (Sum or Average) to be performed with the matrix elements.

Follow the 144 floating point values that make up the matrix, and it is filled line by line, from line 0 to line 11, always from left to right.

Exit

Print the requested result (sum or average) with 1 box after decimal point.

#include <stdio.h>

main() {

    float M[12][12];
    int linha, l, coluna;
    char T;
    float calculo;

    scanf("%d", &l);
    scanf("%c", &T);

    for(linha = 0; linha < 12; linha ++){
        for(coluna = 0; coluna < 12; coluna ++){
            scanf("%f", &M[linha][coluna]);
        }
    }

    if (T == 'M'){
        for(coluna = 0; coluna < 12; coluna ++){
            calculo = calculo + M[l][coluna];
        }
        calculo = calculo/12;
    }
    else {
        for (coluna = 0; coluna < 12; coluna ++){
            calculo = calculo + M[l][coluna];
        }
    }
    printf ("%0.1f", calculo);


}

The program does not read the matrix and gives error. I thank anyone who wants to help me, thank you :)

  • Is that language? Look whenever possible to identify which language is the question.

  • Looks like C. But that’s not enough

  • Sorry guys, it’s in C language

1 answer

2

When you leave the scanf("%d",&l) the code is storing line break buffer and skips the scanf("%c",T), I mean, it jumps right into the scanf("%f", &M[][]) inside the is, then when you enter an error character. try to read the character like this

scanf("\n%c",&T);

With that, when you type <n>[enter], will identify you have a line break before reading a value for T.

Browser other questions tagged

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