English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

this is c++ code to print the sum of rows and column.
// Printing of the sum of each row.
cout<<"Printing of the sum of rows "< for (i=0;i<4;i++)
{
for (j=0;j<4;j++)
{
r = r + num[i][j];
}
cout<<"The sum of row "< r = 0;
}
// Printing of the sum of each column.
cout<<"Printing of the sum of columns "< for (i=0;i<4;i++)
{
for (j=0;j<4;j++)
{
c = c + num[j][i];
}
cout<<"The sum of column "< c = 0;
}

WHAT I DON'T UNDERSTAND IS that the codes for adding up both the rows and colum is almost same then how is the answer coming different.
there is differnece of 2 lines-->1) r = r + num[i][j];
2) c = c + num[j][i];
but thier meaning is also same- both locating to the same number, THEN HOW IS THE ANSWER COMING DIFFERNET
plzzzz help, and plz give the explaination in detail

2007-01-05 23:10:01 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

Just look at the following 4 X 4 Matrix

........ C0....C1....C2....C3
R0.... 10....20.....15....30
R1.... 12......2.....90....10
R2.... 45......5.....11....40
R3.... 70....60.....50....10


R0,R1,R2,R3 refer to 4 rows
C0,C1,C2,C3 refer to 4 columns
Any element will be designated in the form "num[r][c]" where "r" is the row number and "c" is the column number

The numbers num[i][j] and num[j][i] are not always referring to the same position..
Just Input the above matrix from the keyboard and execute the follwing code:

void main()
{
int num[4][4];
int i,j;

/ *
write code to scan your matrix here....
*/

cout<<"Printing elements row-wvise "<
for (i=0;i<4;i++)
{
cout< for (j=0;j<4;j++)
{
cout<<"\t"<<"Value of num["< }
}


cout<
for (i=0;i<4;i++)
{
cout< for (j=0;j<4;j++)
{
cout<<"\t"<<"Value of num["< }
}

This code will print the values of num[i][j] and num[j][i]
Now you will realize that num[i][j] and num[j][i] are not referring to the same position in the above two for loops....

2007-01-05 23:32:48 · answer #1 · answered by Anonymous · 0 0

lets do some dry run to show you what's going on. Consider the table below. It represents your num array such as num[0][0], num[2][3], etc....

x---------x---------x--------x---------x
| [0][0] | [0][1] | [0][2] | [0][3] |
x---------x---------x--------x---------x
| [1][0] | [1][1] | [1][2] | [1][3] |
x---------x---------x--------x---------x
| [2][0] | [2][1] | [2][2] | [2][3] |
x---------x---------x--------x---------x
| [3][0] | [3][1] | [3][2] | [3][3] |
x---------x---------x--------x---------x

in the rows calculation, the num array is accessed as follows:
num[0][0]
num[0][1]
num[0][2]
num[0][3]
num[1][0]
num[1][1]
num[1][2]
num[1][3]
.........
..........
and so on until num[3][3]

Now lets move to colums calculation:
num[0][0]
num[1][0]
num[2][0]
num[3][0]
num[0][1]
num[1][1]
num[2][1]
num[3][1]
.......
.......
and so on until num[3][3]

Now compare the above 2 sets of results. When rows are calculated, the first index (row) stays the same until the second index (column) finishes traversing the loop. Then the first index increments by one (which means next row) and calculates positions in that row.

In the colums calculation, the second index (row) stays the same until the first index (column) finishes traversing the loop. Then the second index increments by one.

2007-01-06 07:38:54 · answer #2 · answered by Anonymous · 1 0

the difference is what part of the array you are incrementing WHEN you do the addition.
in the first, you are incrementing the second parameter and adding, then you increment the first and do the addition on the second again.
in the second part you are incrementing the first parameter and adding, then incrementing the second, then doing the addition on the first again.

2007-01-06 08:38:56 · answer #3 · answered by justme 7 · 0 0

fedest.com, questions and answers