Advertisement

Arrays

Started by June 02, 2002 01:50 PM
10 comments, last by Ragadast 22 years, 5 months ago
I've seen some examples making something like this with arrays:

int array[50][50] = {1, 0, 1, 1, 1, 0, 0,
                     0, 1, 1, 0, 0, 0, 1,
                     0, 0, etc...}
 
I've never seen a tutorial explaining arrays used in that way, but I think it might be useful for something I'm doing. So, could someone explain me how it works please? [edited by - Ragadast on June 2, 2002 2:54:00 PM]
Nuh-uh. Not with a 2D array. To do that you''d have to say
int array[50][50] =
{ { 0, 0, 0, 0, 0, ...}, {0, 0, 0, 0, 0, 0, ...}, ...};

In other words, each row is an array, a subset of the matrix. You have to set each element in each row as shown above if you want to do that.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

[twitter]warrenm[/twitter]

Advertisement
As another example:
int arr[5][5] = {{1, 0, 0, 0, 1}, {1, 0, 0, 0, 1},  {0, 1, 0, 1, 0},  {0, 0, 1, 0, 0},  {0, 0, 0, 0, 0}}; 


Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

[twitter]warrenm[/twitter]

Ok. But what''s the mean of
{0, 1, 1, 1, 0, 1}? 

Is it a number? Each line of numbers correspond to one of the elements of the array, right?
Each number corrosponds to a single element of the array (a single memory location); Each line is a ROW in the array. However, each row is also an array in and of itself. That''s why the initialization is expressed that way.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

[twitter]warrenm[/twitter]

Cone3D''s arrays and strings tutorial

Advertisement
If you have this array:

int array [3][3] = { {2, 4, 6}, {8, 10, 12}, {14, 16, 18} };

then if you want to assign the number 12 in this array to another variable, you would write this:

int some_value;

some_value = array[2][3];

...or if you want to assign the number 16 to another variable you write this:

int another value;

another_value = array [3][2];

did this help?
If you have this array:

int array [3][3] = { {2, 4, 6}, {8, 10, 12}, {14, 16, 18} };

then if you want to assign the number 12 in this array to another variable, you would write this:

int some_value;

some_value = array[2][3];

...or if you want to assign the number 16 to another variable you write this:

int another value;

another_value = array [3][2];

did this help?
Thanks a lot, i got it
wait a sec, isn''t the first element of an array supposed to be labled 0?

so in this case, if you want to assign the number 12 in the array to another variable, wouldn''t you write this:

int some_value;

some_value = array[1][2];

correct me if i''m wrong...still a bit sketchy on arrays.
unkn.Enigma1625

This topic is closed to new replies.

Advertisement