Hi i have two doubts in the following code,the doubts are marked..PLs note that the following code is correct .This is a program to read 2d array using pointer ()i.e Dynamic array ,to calculate its rowsum and column sum and display this array along row sum and column sum.
Line 17: Rsum[i] = 0; sets the value in Rsum[i] to 0. Prior to that Rsum[i] contains some junk value that would not be conducive to generating an accurate sum.
Rsum[i]+=Val[i*MaxC+j];(WHY WE USE i*MaxC+j INSIDE Val)
If we have a one-dimensional array, and wish to treat it as a two dimensional array, we must calculate the one dimensional index from our two dimensional indexes. We do this by multiplying the row index times the number of columns in the two dimensional array and adding the column index.
For instance in this one dimensional array (in which an element is equal to the index for an element:)
[0][1][2][3][4][5][6][7][8]
which we would like to treat as a two dimensional array:
0 1 2
0 [0][1][2]
1 [3][4][5]
2 [6][7][8]
To calculate the one dimensional index of 1, 2 (row, column), we multiply the row index by 3 and then add the column index which gives us 5: 1 * 3 + 2 = 5
1 2 3 4 5 6 7 8 9 10 11
// http://ideone.com/So484g
#include <iostream>
int main()
{
constunsigned rows = 3 ;
constunsigned cols = 5 ;
for ( unsigned row=0; row < rows; ++row )
for ( unsigned col =0; col < cols; ++col )
std::cout << '(' << row << ',' << col << ") = " << row * cols + col << '\n' ;
}