The matrix is printing the same values for all indexes, why?

Asked

Viewed 125 times

1

Create an algorithm that loads a 12 x 4 matrix with the sales values of a store, in which each row represents one month of the year, and each column, one week of the month.

For simplification purposes consider that each month has only 4 weeks. Calculate and print:
- Total sold in each month of the year;
- Total sold each week throughout the year;
- Total sold in the year.

#include <stdio.h>

int main(){
    int matriz[12][4];
    int linha, coluna;
    int totalMes[12];
    int totalSemana[48];
    int total = 0;

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

    for(linha=0; linha<12; linha++){
        for(coluna=0; coluna<4; coluna++){          
            totalMes[linha] += matriz[linha][coluna];

            totalSemana[coluna] += matriz[linha][coluna];

            total += totalMes[linha] + totalSemana[coluna];
        }
    }

    for(linha=0; linha<12; linha++){
        printf("Total vendido no mês %d = %d:\n", linha+1, totalMes[linha]);

        for(coluna=0; coluna<4; coluna++){
            printf("LINHA = %d e COLUNA = %d\n", linha, coluna);
            printf("Total vendido na semana %d = %d:\n", coluna+1, totalSemana[coluna]);        
        }
        printf("\n\n");
    }

    printf("Total vendido no ano = %d:\n", total);

    return 0;
}

1 answer

1

face I don’t see any attribution to your variables that’s correct?

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

        for(linha=0; linha<12; linha++){
            for(coluna=0; coluna<4; coluna++){

                totalMes[linha] += matriz[linha][coluna];

                totalSemana[coluna] += matriz[linha][coluna];

                total += totalMes[linha] + totalSemana[coluna];
            }

        }

where you assigned a value in the matrix variable[row][column]?

me explains this pq so far your matrix came null and is empty . then empty sum with empty result = empty

=D

Browser other questions tagged

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