How to calculate the sum of each row of a matrix in C?

Asked

Viewed 14,836 times

1

I need to create an algorithm that reads the dimensions of a matrix, read the values to be placed in the matrix, with a vector calculate the sum of the values of each row and print the matrix and the vector.

This float soma computes the total sum of the entire matrix, but need to calculate the sum of each line.

int main() {

    int L , C , linha = 0 , coluna = 0;
    float soma = 0;
    printf("\nDigite o numero de linhas da matriz: ");
    scanf("%d",&L);
    printf("\nDigite o numero de colunas da matriz: ");
    scanf("%d",&C);
    float matriz[L][C];


    for(linha = 0; linha < L; linha++){
        for(coluna = 0; coluna < C; coluna++){
            printf("\nDigite o %d valor para a %d linha: ",coluna + 1, linha + 1);
            scanf("%f",&matriz[linha][coluna]);
            soma = soma + matriz[linha][coluna];

        }
    }

    for (linha = 0; linha < L; linha++){
        for(coluna = 0; coluna < C; coluna++){
            printf("%5.2f ",matriz[linha][coluna]);
        }
            printf("\n\n");
    }



    printf("\n\nA soma total eh  %5.2f\n\n\n",soma);

    system("pause");
    return 0;
}
  • Create a sum vector and place the sum of the lines in each position, that’s it?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

It takes a array to store the lines. It should be the same size as the number of input lines. Don’t forget to reset the data. I did this with memset(). And then sum equal to the total, only on each element of the line. Then print all the elements.

#include <stdio.h>
#include <string.h>

int main() {
    int linhas , colunas;
    printf("\nDigite o numero de linhas da matriz: ");
    scanf("%d", &linhas);
    printf("\nDigite o numero de colunas da matriz: ");
    scanf("%d", &colunas);
    float matriz[linhas][colunas];
    float soma = 0;
    float somaLinhas[linhas];
    memset(somaLinhas, 0, sizeof(somaLinhas));
    for (int linha = 0; linha < linhas; linha++) {
        for (int coluna = 0; coluna < colunas; coluna++) {
            printf("\nDigite o %d valor para a %d linha: ", coluna + 1, linha + 1);
            scanf("%f", &matriz[linha][coluna]);
            soma += matriz[linha][coluna];
            somaLinhas[linha] += matriz[linha][coluna];
        }
    }
    printf("\n");
    for (int linha = 0; linha < linhas; linha++) {
        for (int coluna = 0; coluna < colunas; coluna++) printf("%5.2f ",matriz[linha][coluna]);
        printf("\n");
    }
    for (int linha = 0; linha < linhas; linha++) printf("\nA soma da linha %d eh %5.2f", linha, somaLinhas[linha]);
    printf("\nA soma total eh  %5.2f", soma);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

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