Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello All, I have a code below that is completely operational for summing the ro

ID: 3551071 • Letter: H

Question

Hello All,


I have a code below that is completely operational for summing the rows and columns of a two dimensional array, but just wondering if there was a way to simplify it a bit, in particular the 10 lines of "int sumrow" and int sumcol?" I've been working on this for four hours now in difffernt ways but can't seem to simplify it.




#include<stdio.h>


#define NUMROWS 5

#define NUMCOLS 5


int main( )

{


int val[NUMROWS][NUMCOLS] = { 1, 6, 15, 7, 1, 1, 5, 17, 9, 10, 12, 7, 2, 3, 6, 6, 4, 12, 13, 20, 21, 5, 4, 12, 5 };


int i, j;


int sumrow0 = val[0][0] + val[0][1] + val[0][2] + val[0][3] + val[0][4];

int sumrow1 = val[1][0] + val[1][1] + val[1][2] + val[1][3] + val[1][4];

int sumrow2 = val[2][0] + val[2][1] + val[2][2] + val[2][3] + val[2][4];

int sumrow3 = val[3][0] + val[3][1] + val[3][2] + val[3][3] + val[3][4];

int sumrow4 = val[4][0] + val[4][1] + val[4][2] + val[4][3] + val[4][4];


int sumcol0 = val[0][0] + val[1][0] + val[2][0] + val[3][0] + val[4][0];

int sumcol1 = val[0][1] + val[1][1] + val[2][1] + val[3][1] + val[4][1];

int sumcol2 = val[0][2] + val[1][2] + val[2][2] + val[3][2] + val[4][2];

int sumcol3 = val[0][3] + val[1][3] + val[2][3] + val[3][3] + val[4][3];

int sumcol4 = val[0][4] + val[1][4] + val[2][4] + val[3][4] + val[4][4];


int Rtotal= (sumrow0 + sumrow1 + sumrow2 + sumrow3 + sumrow4);


int Ctotal = ( sumcol0 + sumcol1 + sumcol2 + sumcol3+ sumcol4);


printf(" ROW totals = %d", Rtotal);

printf(" COLUMN totals = %d", Ctotal);


return 0;

}

Explanation / Answer

#include<stdio.h>


#define NUMROWS 5

#define NUMCOLS 5


int main( )

{


int val[NUMROWS][NUMCOLS] = {{ 1, 6, 15, 7, 1}, {1, 5, 17, 9, 10}, {12, 7, 2, 3, 6}, {6, 4, 12, 13, 20}, {21, 5, 4, 12, 5} };


int i, j;

int rtotal = 0,ctotal = 0;


for(i=0;i<NUMROWS;i++){

for(j=0;j<NUMCOLS;j++){

rtotal = rtotal + val[j][i];

ctotal = ctotal + val[i][j];

}

}


printf(" ROW totals = %d", rtotal);

printf(" COLUMN totals = %d", ctotal);


return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote