Need help with arrays...C-Language.
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.
It totals up all the numbers because you do not set total to 0 when you move onto the next row.
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement