Sum of columns

Asked

Viewed 56 times

0

I need to sum each column of a 3X3 matrix. Questão

What I got so far:

#include <stdio.h>

int main()
{
    int matriz[3][3] = 
    {
        { 5, -8, 10 },
        { 1, 2, 15 },
        { 25, 10, 7}
    };
  
    int array[3] = {0};
    
    for(int linha = 0; linha < 3; linha++){
        for(int coluna = 0; coluna < 3; coluna++){
            printf("%d  ", matriz[linha][coluna]);    
        }
        printf("\n");
    }
    int soma;
    for(int lin = 0; lin < 3; lin++){
        soma = 0;
        for(int col = 0; col < 3; col++){
            soma = soma + matriz[lin][col];   
        }
        array[col] = soma;
    }
    
    printf("\nA soma das colunas da matriz é:\n");
    for(int r = 0; r < 3; r++){
        printf("%d  ", array[r]);   
    }
    
    return 0;
}
  • And the question is what? Say one that is punctual and specific.

  • You said what you want to do, but you didn’t describe the problem you’re having.

3 answers

0

You didn’t specify the problem you’re encountering, but here’s a java solution that looks very similar to C so you can get the idea.

        int[][] matriz = new int[3][3];
        int[] novo = new int[3];
        for (int linha = 0; linha < matriz.length; linha++) {
                for (int coluna = 0; coluna < matriz.length; coluna++) {
                novo[coluna]+=matriz[linha][coluna];
                }
        }

0


You have reversed the loops in the sum. To sum the columns first vary the columns and internally vary the rows of each column.

#include <stdio.h>

int main() {
    int matriz[3][3] = { { 5, -8, 10 }, { 1, 2, 15 }, { 25, 10, 7}  };
      
    int array[3] = {0};

    for(int linha = 0; linha < 3; linha++){
        for(int coluna = 0; coluna < 3; coluna++){
            printf("%d  ", matriz[linha][coluna]);
        }
        printf("\n");
    }
    int soma;
    for(int col = 0; col < 3; col++){
        soma = 0;
        for(int lin = 0; lin < 3; lin++){
            soma = soma + matriz[lin][col];   
        }
        array[col] = soma;
    }

    printf("\nA soma das colunas da matriz é:\n");
    for(int r = 0; r < 3; r++){
        printf("%d  ", array[r]);   
    }

    return 0;
}

See working in https://ideone.com/h8DZCj

  • Thank you for your help.

0

has two problems in its second loop, which sums the columns.

  1. You need to traverse between columns and not between rows. Change the order of the matrix references matriz[lin][col] for matriz[col][lin]
  2. there is an error in the line where you place the sum in your array, you are using the variable col that does not exist outside the loop of it, when it should be the variable lin. Substitute array[col] = soma; for array[lin] = soma;

Stretch with corrections:

    for(int lin = 0; lin < 3; lin++){
        soma = 0;
        for(int col = 0; col < 3; col++){
            soma = soma + matriz[col][lin];   
        }
        array[lin] = soma;
    }

Browser other questions tagged

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