please read the code and help me out
while allocating space for 2D arrays using new to read the elements
int * val=new int[r*c] // r and c are no of rows and columns resp.
for(int i=0; i%26lt;r; i++)
{
for(int j=0; j%26lt;c; j++)
{
cin%26gt;%26gt;val[i*c+j]; // what does this statement do?
}
}
my doubt is why not A[i] [j]?
what does this statement do?
C++ pointers and 2D arrays?
You haven't actually allocated a 2d array: you've allocated a 1d array of size r*c.
Consequently, you need to do some math to convert your 2d coordinate into a 1d index. i*c+j converts your (r,c) coordinate into a unique index. You can see how this works simply by trying various numbers:
(0, 0) = 0*c+0 = 0
(1, 0) = 0*c +1 = 1
(0, 1) = 1*c + 0 = c
(1, 1) = 1*c+1 = c + 1
In this way, every possible 2d coordinate gets mapped to an index between 0 and r*c.
Reply:The statement assigns the user input to the array in the position indicated by i, c+j.
tarot cards
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment