Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Web Hosting

Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


getResponse autofunnels

Liquid Web Cloud VPS Hosting

11,159 questions

15,235 answers

573 users

How to sum 2D array (matrix) cols in C

1 Answer

0 votes
#include <stdio.h>
         
void sum_matrix_cols(int matrix[][5], int cols_sum[3], int rows, int cols)
{
    for (int j = 0; j < cols; j++) {
        for (int i = 0; i < rows; i++)
            cols_sum[j] += matrix[i][j];
    }
}
  
int main()
{
    int matrix[3][5] = { { 1, 2, 3, 4, 5 },
                         { 1, 2, 3, 4, 5 },
                         { 1, 2, 3, 4, 5 } };
  
    int cols_sum[5] = { 0 };
    int rows = sizeof matrix / sizeof matrix[0];
    int cols = sizeof matrix[0] / sizeof(int);
  
    sum_matrix_cols(matrix, cols_sum, rows, cols);
  
    for (int i = 0; i < 5; i++)
        printf("%d\n", cols_sum[i]);
  
    return 0;
}
 
/*
run:
 
3
6
9
12
15
 
*/

 




answered Apr 13, 2018 by avibootz
edited Apr 13, 2018 by avibootz
...