[size=2][b][color=Blue]Can some one give one c++ programming coding of two one-dimensional sample? I have task on it but have no idea how it goes..[/color][/b][/size]
: [size=2][b][color=Blue]Can some one give one c++ programming coding : of two one-dimensional sample? I have task on it but have no idea : how it goes..[/color][/b][/size] : It's a lot more complicated than it seems at first sight.
Ypu can declare a 2 dimensional array like this
int array[10][10];
it works as you would expect. array[0][0] = 1; sets the top left-hand corner to 1.
However generally we don't want arrays of fixed size, we want to build them dynamically. This is where the complication comes in. You need a array of pointers to pointers.
In practise it is usally easier to say
array = malloc(width * height * sizeof(int));
array[y * width + x] = 1; /* set element x, y to one */
That isn't an array of pointer to pointer though, that's a "mangled" 2D array... which is better than pointer-to-pointer arrays in one way, you can memcpy() and strcpy() to and fro common (statically allocated) 2D arrays.
Comments
one dimensional example. Declares an array of 10 elements and clears each element:
[code]int array[10];
for (int i=0; i<10; i++)
array[i]=0;[/code]
two dimensional example. Same thing as above but 2 dimensional:
[code]int array[5][5];
for (int y=0; y<5; y++)
for (int x=0; x<5; x++)
array[y][x]=0;[/code]
int arr[3][4];
accessing the integer in 0th row and 1st column: (array indices are 0 based)
arr[0][1] = 10;
printing all values of the array:
[code]
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++)
cout << arr[i][j] << ' ';
cout << endl;
}
[/code]
--
~Donotalo()
: of two one-dimensional sample? I have task on it but have no idea
: how it goes..[/color][/b][/size]
:
It's a lot more complicated than it seems at first sight.
Ypu can declare a 2 dimensional array like this
int array[10][10];
it works as you would expect. array[0][0] = 1; sets the top left-hand corner to 1.
However generally we don't want arrays of fixed size, we want to build them dynamically. This is where the complication comes in. You need a array of pointers to pointers.
In practise it is usally easier to say
array = malloc(width * height * sizeof(int));
array[y * width + x] = 1; /* set element x, y to one */
handling the array as a one-dimensional object.