Need help with arrays...C-Language.

Started by
2 comments, last by madhatter256 20 years ago
Ok. I''m starting to get teach myself arrays for C-language. What I''m trying to do is creat a proram that reads a 5 x 5 array of integers and then prints the row sums and the column sums. It should look something like this when coded properly: Row1: 1 1 1 1 1 Row2: 2 2 2 2 2 Row3: 3 3 3 3 3 Row4: 4 4 4 4 4 Row5: 5 5 5 5 5 Row Totals: 5 10 15 20 25 Colum Totals: 15 15 15 15 15 I got the part where it shows the numbers that were inputted to the array. I think that to total up each row and column is to use a for loop like this: for(row1=0; row1<=ROWS; row1++){ for(row2=0; row2<=COLS; row2++){ total += a[row1][row2]; } printf("Total for Row %d is: %d", row1+1, total); printf("\n"); } However, that adds up all of the numbers!! For now, I want to tackle it in adding the rows. If I get that working right then the columns will be done faster.
Advertisement
It totals up all the numbers because you do not set total to 0 when you move onto the next row.

Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud
I was just about to say that I figured it out. And turns out someone beat me to it. Thanks for your help. Now I gotta figure out how to read each colum now and by the looks of it, I''ll have to do the same but maybe use another type of loop.
ok i got the whole thing working now.

This topic is closed to new replies.

Advertisement