The program is not reading the numbers in the array

-1

I'm doing exercises at uri online judge, this one asks:

In this problem you should read a number, indicating an array line in which an operation must be performed, a capital character, indicating the operation to be performed, and all elements of a M[12][12] array.

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

The following are the 144 floating-point values that make up the array, which is filled line by line, from line 0 to line 11, always from left to right.

Output

Print the requested result (the sum or average), with 1 house after the 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 array and gives an error. Thank you for helping me, thank you:)

    
asked by anonymous 20.02.2016 / 00:14

1 answer

2

When it exits from scanf("%d",&l) , the code is buffering the line break buffer and skips the scanf("%c",T) , that is, it skips directly to the scanf("%f", &M[][]) within the for, so when you enter an error character. try reading the character like this

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

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

    
20.02.2016 / 06:22