Array VS matrix

Hello,
I know that I have been asking about arrays 2D arrays and matrixes for a quite a while but feel that so far I haven't been able to come up with the right questions. Now I think I have the right questions..

Case scenario:

I have a 2D grid stored in a 1D array (e.g. int grid[ 100 ];)
If I want to access that grid by index, all I have to do is put the index in (e.g. grid[ 4 ] = 5;) and I can change the value of the grid at that point. retrieve it etc etc
If I want to access that grid by coordinates, I can't, so I have to write functions (e.g. int indexToX( int ) and int indexToY( int ) ) that let you do that.

Question:

If I had that same 2D grid stored in a matrix instead (e.g. int grid[ 10 ][ 10 ]; ) the coordinate access would be handled for me, but would the access speed be EXACTLY as fast as with a 1D array? Also, would the index accessing be there for me or would I have the exact same problem in reverse? (it being, not being able to access the matrix by index but by coordinates only)

Thanks a lot,
Clodi
One definition for "exactly" is identical binary. Write both versions, compile, and study the assembly.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// a single block of sizeof(int)*15 bytes
int foo [15];

// a single block of sizeof(int)*15 bytes
// compiler memorizes number "3"
int bar [5][3];

// Lets pretend the foo is 2D with 3 elements per row:
cout << foo[ row*3 + col ];
index = row*3 + col;
cout << foo[ index ];
// Compiler did calculate memory address:
// addressof(foo) + (index)*sizeof(int)


cout << bar[ row ][ col ];
// Compiler did calculate memory address:
// addressof(bar) + (row*3 + col)*sizeof(int)

// by index:
R = index / 3;
C = index % 3;
cout << bar[ R ][ C ];
// or
cout << *( &bar[0][0] + index );
If I want to access that grid by coordinates, I can't, so I have to write functions (e.g. int indexToX( int ) and int indexToY( int ) ) that let you do that.

You cannot access a 1D array with a cordinate.

would the access speed be EXACTLY as fast as with a 1D array?

For dense matrices the 1D approach will be faster since it offers better memory locality and less allocation and deallocation overhead.

would the index accessing be there for me or would I have the exact same problem in reverse?

the indexing is there for you. (if at all i understand what you mean). e.g
1
2
int array[10][10];
array[3][2] = 5; //its there for you. You dont need int indexToX( int ) and int indexToY( int ) ) 
You cannot access a 1D array with a cordinate.


This is what I meant:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
float Lattice::getSiteDistance( int a,int b )
{
    //find coordinates
    int x1,y1,x2,y2;
    x1 = indextoX( a );
    y1 = indextoY( a );
    x2 = indextoX( b );
    y2 = indextoY( b );

    //pythagoras to get distance
    return (float)sqrt( pow( (float)(abs(x1-x2)),2 ) + pow( (float)(abs(y1-y2)),2 ) );
}
float Lattice::getSiteDistance( int a,int b,int c,int d )
{
    //pythagoras to get distance
    return (float)sqrt( pow( (float)(abs(a-c)),2 ) + pow( (float)(abs(b-d)),2 ) );
}


For dense matrices the 1D approach will be faster since it offers better memory locality and less allocation and deallocation overhead.


Thanks a lot for the info

One definition for "exactly" is identical binary. Write both versions, compile, and study the assembly.


I don't get your post right now 'cause I am attending a lec ture and there is too much noise, Let me reprocess it and get back to you. Thank you SO much for thelp
You have to explain more, because your code fragment merely gives an eerie feeling.
Topic archived. No new replies allowed.