Render and display the Matrix in C (dev c++)

Asked

Viewed 736 times

-2

I have to do this:

Make a program that loads a 12x4 matrix with the values of sales of a store, where each row represents one month of the year and each column represents a week of the month. Calculate and show;

  • the total sold in each month of the year, showing the name of the month in full;

  • the total sold in each week throughout the year;

  • the total sold by the store in the year.

The total of the year worked, but the other two results I can not, the numbers do not appear right. I really do not know how to proceed :(

This is my code:

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include <time.h>
#include <locale.h>

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

int mat[12][4], l, c, totalsemana, totalano, jan, fev, mar, abr, mai, jun, jul, ago, set, out, nov, dez; char mes; //PEDIR VALORES for(l=0; l<12; l++) { for(c=0; c<4; c++) { printf("\n________________________________________________________________"); printf("\n\n Insira o valor das vendas do mês %i - semana %i - posição [%i][%i]: ", l+1, c+1, l+1, c+1); scanf("%i", &mat[l][c]); totalano = totalano + mat[l][c]; } } for(l=0; l<12; l++) { for(c=0; c<4; c++) { jan = jan + mat[1][c]; fev = fev + mat[2][c]; mar = mar + mat[3][c]; abr = abr + mat[4][c]; mai = mai + mat[5][c]; jun = jun + mat[6][c]; jul = jul + mat[7][c]; ago = ago + mat[8][c]; set = set + mat[9][c]; out = out + mat[10][c]; nov = nov + mat[11][c]; dez = dez + mat[12][c]; } } printf("\n\n ################### R E S U L T A D O S #####################"); printf("\n\n ****************************************************"); printf("\n\n O total vendido no ANO é: %i", totalano); //MOSTRAR EM CADA SEMANA printf("\n\n ****************************************************"); printf("\n\n O total vendido em cada SEMANA é: "); for(c=0; c<4; c++) { for(l=0; l<12; l++) { printf("\n------------------------------------"); printf("\n Mês %i - semana %i: ", l+1, c+1); printf("%i", mat[l][4]); } } printf("\n\n ****************************************************"); //MOSTRAR CADA MES printf("\n\n O total de venda em cada MÊS no ano é: "); printf("\n Janeiro: %i", jan); printf("\n Fevereiro: %i", fev); printf("\n Março: %i", mar); printf("\n Abril: %i", abr); printf("\n Maio: %i", mai); printf("\n Junho: %i", jun); printf("\n Julho: %i", jul); printf("\n Agosto: %i", ago); printf("\n Setembro: %i", set); printf("\n Outubro: %i", out); printf("\n Novembro: %i", nov); printf("\n Dezembro: %i", dez); printf("\n\n ****************************************************"); }

1 answer

0


I found several errors in your code, here they are: First, the variables where you are holding the amount collected each month need to be initialized, thus:

int mat[12][4], l, c, totalsemana, totalano, jan = 0, fev = 0, mar = 0, abr = 0, mai = 0, jun = 0, jul = 0, ago = 0, set = 0, out = 0, nov = 0, dez = 0;

Second, your second noose is wrong, here it is:

//for(l=0; l<12; l++){
    for(c=0; c<4; c++){   
        jan += mat[1][c]; 
        fev += mat[2][c];
        mar += mat[3][c];
        abr += mat[4][c];
        mai += mat[5][c];
        jun += mat[6][c];
        jul += mat[7][c];
        ago += mat[8][c];
        set += mat[9][c];
        out += mat[10][c];
        nov += mat[11][c];
        dez += mat[12][c];
    }
//}

and finally, its indexing in the loop that shows each week is incorrect, here’s how it should look:

printf("\n------------------------------------");
printf("\n Mês %i - semana %i: ", l+1, c+1);
printf("%i", mat[l][c]);

A tip: identify your code well, it helps maintenance and reading by third parties.

Browser other questions tagged

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