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

BlueHost Web Hosting

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

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

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,780 questions

10,864 answers

573 users

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

1 Answer

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

 




answered Apr 13 by avibootz
edited Apr 13 by avibootz
...