Archived

This topic is now archived and is closed to further replies.

Arrays

This topic is 5671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.]

Share this post


Link to post
Share on other sites
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.]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
The following works for me:

int number[10][7] =
{1,1,1,0,1,1,1
,0,0,1,0,0,1,0
,1,0,1,1,1,0,1
,1,0,1,1,0,1,1
,0,1,1,1,0,1,0
,1,1,0,1,0,1,1
,1,1,0,1,1,1,1
,1,0,1,0,0,1,0
,1,1,1,1,1,1,1
,1,1,1,1,0,1,0};

So, it looks like you can initialize 2-dimension arrays as if it were a 1-dimensional array. I am using MSVC++ 6.0. Maybe this is only available in C++ and not in C? I have no idea. All I know is that it works.

Jason Doucette
http://www.jasondoucette.com/

Share this post


Link to post
Share on other sites