Velocity Reviews > Newbie Multi-Dimensional Array Prob

# Newbie Multi-Dimensional Array Prob

Henry
Guest
Posts: n/a

 08-29-2003
Hopefully you guys can give me some help on Multi-dimensional arrays....
I just read the section on it and here's why I am confused

Main()
int matrix [2][4];
{
{1, 2, 3, 4};
{10, 20, 30, 40};
}

^ That was Pratical C's explaination of how multidimensional arrays break
down. "a declaration of an array of dimension 2 whose elements are arrays
of dimension 4."
Ok this is why I am confused , I guess they mean by dimension 2 that it is
1's, and then 10's. So if it was dimension 4 as the first number it would
be 1's, 10's, 100's, 1000's ?? And with the second number being 4 that means
it continues for 4 numbers. If I am right (???) .. so if I wanted to put
something in that array I could put it at just the number 10 , but not 11 or
12?

Also another thing that confuses me is when they have an array say ... int
arr[4] and then they set a variable like this int [x] inside the array
later... obviously x would have to have a value between 0 and 4 correct?
So when you set x inside the array like that it just replaces one of the
sections of the array equal to its value.. let me try to explain what I
mean:

x = 3
arr[4] = arr[0], arr[1], arr[2], arr[3], arr[4]
arr[x] = arr[0], arr[1], arr[2], arr[x], arr[4]

Am I right? Or have I completly confused myself... either way I am still
very confused.

Thanks,
Henry

Paul Richards
Guest
Posts: n/a

 08-29-2003
I think you are getting confused as to what arrays actually are. You
seem to be talking about using them to store sequences of numbers, or
something..

They are infact a bit different. They are basically a way of having
many seperate variables which you can index using a number.

So say I wanted to have 5 ints representing the weights of 5 objects.
Instead of declaring my variables like:

int weight1, weight2, weight3, weight4, weight5;

I could infact declare all 5 in one go like this:

int weight[5];

Then I can access all the weights using notation like weight[0],
weight[1].. weight[4], and I can even access them using another variable
as the index.

for (int i = 0; i < 5; i++) {
printf( "%i\n", weight[i] );
}

for example..

When they go to multiple dimensions it sounds like your book has a very
confusing description, they are infact just like having a 2D or 3D board
of values, instead of a long line of them like in my example. Of course
the dimensions can go beyond just 3.

Henry wrote:
> Hopefully you guys can give me some help on Multi-dimensional arrays....
> I just read the section on it and here's why I am confused
>
> Main()
> int matrix [2][4];
> {
> {1, 2, 3, 4};
> {10, 20, 30, 40};
> }
>
> ^ That was Pratical C's explaination of how multidimensional arrays break
> down. "a declaration of an array of dimension 2 whose elements are arrays
> of dimension 4."
> Ok this is why I am confused , I guess they mean by dimension 2 that it is
> 1's, and then 10's. So if it was dimension 4 as the first number it would
> be 1's, 10's, 100's, 1000's ?? And with the second number being 4 that means
> it continues for 4 numbers. If I am right (???) .. so if I wanted to put
> something in that array I could put it at just the number 10 , but not 11 or
> 12?
>
> Also another thing that confuses me is when they have an array say ... int
> arr[4] and then they set a variable like this int [x] inside the array
> later... obviously x would have to have a value between 0 and 4 correct?
> So when you set x inside the array like that it just replaces one of the
> sections of the array equal to its value.. let me try to explain what I
> mean:
>
> x = 3
> arr[4] = arr[0], arr[1], arr[2], arr[3], arr[4]
> arr[x] = arr[0], arr[1], arr[2], arr[x], arr[4]
>
> Am I right? Or have I completly confused myself... either way I am still
> very confused.
>
> Thanks,
> Henry
>
>

--
Paul Richards

New Technology Researcher

Fred L. Kleinschmidt
Guest
Posts: n/a

 08-29-2003

Henry wrote:
>
> Hopefully you guys can give me some help on Multi-dimensional arrays....
> I just read the section on it and here's why I am confused
>
> Main()
> int matrix [2][4];
> {
> {1, 2, 3, 4};
> {10, 20, 30, 40};
> }
>
> ^ That was Pratical C's explaination of how multidimensional arrays break
> down. "a declaration of an array of dimension 2 whose elements are arrays
> of dimension 4."
> Ok this is why I am confused , I guess they mean by dimension 2 that it is
> 1's, and then 10's. So if it was dimension 4 as the first number it would
> be 1's, 10's, 100's, 1000's ?? And with the second number being 4 that means
> it continues for 4 numbers. If I am right (???) .. so if I wanted to put
> something in that array I could put it at just the number 10 , but not 11 or
> 12?
>
> Also another thing that confuses me is when they have an array say ... int
> arr[4] and then they set a variable like this int [x] inside the array
> later... obviously x would have to have a value between 0 and 4 correct?
> So when you set x inside the array like that it just replaces one of the
> sections of the array equal to its value.. let me try to explain what I
> mean:
>
> x = 3
> arr[4] = arr[0], arr[1], arr[2], arr[3], arr[4]
> arr[x] = arr[0], arr[1], arr[2], arr[x], arr[4]
>
> Am I right? Or have I completly confused myself... either way I am still
> very confused.
>
> Thanks,
> Henry

You do not seem to understand what an array is.
An array dimensioned 4 can contain any four values. So, for example,
int x[4] = {23, 4567, 1098876, -444 };
so here x[0] has the value 23,
x[1] has the value 4567,
x[2] has the value 1098876,
x[3] has the value -444

So the two dimensional array
int matrix[2][4] = { {1,2,3,4}, {10, 20, 30, 40} };

matrix[1][3] contains the value 30.

You could set any values here;
int matrix[2][4] = { {123, -888, 98765, 1}, {0, 0, 0, 0} };
--
Fred L. Kleinschmidt
Associate Technical Fellow
Boeing Common User Interface Services

Eric Sosman
Guest
Posts: n/a

 08-29-2003
"Fred L. Kleinschmidt" wrote:
> [...]
> So the two dimensional array
> int matrix[2][4] = { {1,2,3,4}, {10, 20, 30, 40} };
>
> matrix[1][3] contains the value 30.

ITYM 40.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

Henry
Guest
Posts: n/a

 08-29-2003
> So the two dimensional array
> int matrix[2][4] = { {1,2,3,4}, {10, 20, 30, 40} };
>
> matrix[1][3] contains the value 30.
>
> You could set any values here;
> int matrix[2][4] = { {123, -888, 98765, 1}, {0, 0, 0, 0} };

Ok it seems I did have my understanding of arrays a little skewed.. BUT

I'm still very confused on multidimensional arrays... how does
matrix[2][4] initialize to {{1, 2, 3, 4}, {10, 20, 30, 40}} like my book
says it does. (??)

how does matrix[1][3] contain the value 30 in that array .. still very
confused <=(>

Henry

Alex
Guest
Posts: n/a

 08-29-2003
Henry <(E-Mail Removed)> wrote:
>> So the two dimensional array
>> int matrix[2][4] = { {1,2,3,4}, {10, 20, 30, 40} };
>>
>> matrix[1][3] contains the value 30.
>>
>> You could set any values here;
>> int matrix[2][4] = { {123, -888, 98765, 1}, {0, 0, 0, 0} };

> Ok it seems I did have my understanding of arrays a little skewed.. BUT

> I'm still very confused on multidimensional arrays... how does
> matrix[2][4] initialize to {{1, 2, 3, 4}, {10, 20, 30, 40}} like my book
> says it does. (??)

matrix[2][4] means "two rows, four columns each", as in:

[ ][ ][ ][ ]
[ ][ ][ ][ ]

The initialization fills the first row with 4 values, and then
fills the second row with 4 values.

[ 1][ 2][ 3][ 4]
[10][20][30][40]

> how does matrix[1][3] contain the value 30 in that array .. still very
> confused <=(>

Since array numbering is in the 0..N-1 format, matrix[1][3]
means "second row, fourth column". This, as you can see,
contains 40 and not 30.

Alex

Mike Wahler
Guest
Posts: n/a

 08-29-2003

Henry <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> > So the two dimensional array
> > int matrix[2][4] = { {1,2,3,4}, {10, 20, 30, 40} };
> >
> > matrix[1][3] contains the value 30.
> >
> > You could set any values here;
> > int matrix[2][4] = { {123, -888, 98765, 1}, {0, 0, 0, 0} };

>
> Ok it seems I did have my understanding of arrays a little skewed.. BUT
>
> I'm still very confused on multidimensional arrays... how does
> matrix[2][4] initialize to {{1, 2, 3, 4}, {10, 20, 30, 40}} like my book
> says it does. (??)
>
> how does matrix[1][3] contain the value 30 in that array .. still very
> confused <=(>

It contains 40. Fred made a typo.

Perhaps if you compile and run the following it might help:

#include <stdio.h>
#include <stdlib.h>

int main()
{

int matrix[2][4] =
{
{1, 2, 3, 4}, /* matrix[0] */
{5, 6, 7, 8} /* matrix[1] */
};

size_t rows = sizeof matrix / sizeof *matrix;
size_t cols = sizeof *matrix / sizeof **matrix;
int row = 0;
int col = 0;

puts("row col");

for(row = 0; row < rows; ++row)

for(col = 0; col < cols; ++col)
printf("[%2d][%2d] == %d\n",
row, col, matrix[row][col]);

putchar('\n');
printf(" %3s ", "");

for(col = 0; col < cols; ++col)
printf("[%3d]", col);

puts("col\nrow ---------------------");

for(row = 0; row < rows; ++row)
{
printf("[%3d]", row);

for(col = 0; col < cols; ++col)
printf(" %3d ", matrix[row][col]);

putchar('\n');
}

return 0;
}

-Mike

Alex
Guest
Posts: n/a

 08-30-2003
Henry <(E-Mail Removed)> wrote:
>> matrix[2][4] means "two rows, four columns each", as in:
>>
>> [ ][ ][ ][ ]
>> [ ][ ][ ][ ]
>>
>> The initialization fills the first row with 4 values, and then
>> fills the second row with 4 values.
>>
>> [ 1][ 2][ 3][ 4]
>> [10][20][30][40]
>>
>>
>> > how does matrix[1][3] contain the value 30 in that array .. still very
>> > confused <=(>

>>
>> Since array numbering is in the 0..N-1 format, matrix[1][3]
>> means "second row, fourth column". This, as you can see,
>> contains 40 and not 30.

> So the values in the second rows can hold up to 10, 20 , 30 , 40 chars each?
> Still a little confused, but you have made it very clear on what a multi
> dimensional array looks like.

Not quite.

Again, you have a 2x4 matrix. It can hold 8 integers in total.
First four integers go in the first row, the second four integers
go in the second row.

10, 20, 30, and 40 are the actual integer values in the four
columns of the second row.

Alex

Barry Schwarz
Guest
Posts: n/a

 08-30-2003
On Fri, 29 Aug 2003 18:23:27 -0400, "Henry" <(E-Mail Removed)>
wrote:

>> So the two dimensional array
>> int matrix[2][4] = { {1,2,3,4}, {10, 20, 30, 40} };
>>
>> matrix[1][3] contains the value 30.
>>
>> You could set any values here;
>> int matrix[2][4] = { {123, -888, 98765, 1}, {0, 0, 0, 0} };

>
>Ok it seems I did have my understanding of arrays a little skewed.. BUT
>
>I'm still very confused on multidimensional arrays... how does
>matrix[2][4] initialize to {{1, 2, 3, 4}, {10, 20, 30, 40}} like my book
>says it does. (??)
>
>how does matrix[1][3] contain the value 30 in that array .. still very
>confused <=(>
>

Does it help to go back to basics? C does not have multidimensional
arrays. We use the phrase colloquially because if flows easier off
the tongue (or quill) than array of arrays.

int matrix[2][4]; defines matrix to be an array of 2 arrays of 4 int.
Among other things, this means that matrix has only two elements,
denoted by matrix[0] and matrix[1]. That each of these elements is
itself an array is "just a detail".

The subscript operator ([ and ]) associates left to right. Therefore,
matrix[1][3] is interpreted as (matrix[1])[3]. (While this is also
syntactically correct, I have never seen anyone write it this way.)
We already know that matrix[1] evaluates to the second array of 4 int.
Applying the [3] operator to this array evaluates to the value of the
fourth int in that array (which would 40 based on your
initialization).

As far is the initialization goes, consider a one dimensional array
int a1[2];
This is obviously an array of 2 int. If we wanted to add
initialization to the definition, we would code
int a1[2] = {-5,17};
The syntax is straight forward. To initialize an array, include the
list of initialization values within a pair of braces.

Now we look at matrix. It is an array of 2 elements. Therefore the
initialization is { -- , -- }. So what do the dashes represent.
Apply the rule recursively. Each element of matrix is itself an array
of 4 int. The initialization of an array of 4 int looks like
{1,2,3,4). So we replace each set of dashes with the initialization
of an array of 4 int, like
{ {1,2,3,4} , {10,20,30,40} }

Putting it all together we get
int matrix[2][4] = { {1,2,3,4} , {10,20,30,40} };

Looks kind of familiar, doesn't it?

Now consider int test[3][4][2]. Its initialization starts out as
{--,--,--}. Since each element of test is itself an array of 4
elements, each -- gets replace by {--,--,--,--}. So now we have
{ {--,--,--,--} , {--,--,--,--} , {--,--,--,--} }. For ease of
visualization, we will write it as
{
{--,--,--,--} ,
{--,--,--,--} ,
{--,--,--,--}
}

Since each sub element of these arrays of 4 elements is itself an
array of two elements, we would write that initialization as {1,2}.
So each line of the above initialization gets transformed to
{ {1,2} , {2,3} , {4,5} ,{5,6} }

Putting it all together and again splitting the lines to make things
easier to see, we get
int test[3][4][2] =
{ /*start of array initialization*/
{ /*start of initialization for test[0]*/
{1,2}, /*data for test[0][0]*/
{2,3}, /*data for test[0][1]*/
{4,5}, /*data for test[0][2]*/
{5,6}, /*data for test[0][3]*/
} /*end of test[0]
{ /*start of test[1]*/
{9,8}, /*data for test[1][0]*/
{7,6}, /*data for test[1][1]*/
{5,4}, /*data for test[1][2]*/
{3,2}, /*data for test[1][3]*/
} /*end of test[1]
{ /*start of test[2]*/
{10,11}, /*data for test[2][0]*/
{12,13}, /*data for test[2][1]*/
{14,15}, /*data for test[2][2]*/
{16,17}, /*data for test[2][3]*/
} /*end of test[2]
}; /*end of array initialization*/

<<Remove the del for email>>

Henry
Guest
Posts: n/a

 08-30-2003

> Does it help to go back to basics? C does not have multidimensional
> arrays. We use the phrase colloquially because if flows easier off
> the tongue (or quill) than array of arrays.
>
> int matrix[2][4]; defines matrix to be an array of 2 arrays of 4 int.
> Among other things, this means that matrix has only two elements,
> denoted by matrix[0] and matrix[1]. That each of these elements is
> itself an array is "just a detail".

Thanks your description helped alot ( i did not include the entire thing
because it was very long) . I have another question:
I thought array[3] would actually be an array of 4 integers. Doesnt the
If I am right then the multidimensional arrays dont seem to follow that rule
when they initialize, or are you just not including the '/0' ?

Also another thing I am having trouble with is something like this:
main()
{
int [5];
x = 3;
for (i = 0; i < x; ++i);
int[i] = ++x;

I know thats a really crappy example because I did it off the top of my head
but the whole int[i] thing confuses me very much.
where in the 5 part array is int[i] stored??? Does the value of [i]
coincide with its place in the array ? So is it changing every time the loop
executes?

Also in my book they did an example very similar to the one I just showed
but it was multidimensional with the variables x and y. They did a test for
each variable which would add 1 to it if it was true and then at the end
they set it as matrix[x][y]....
the actual value was set as matrix[3][5] .. I was just wondering where in
the array that x and y would be stored as their values increased. I wish I
had something who was a programmer who could explain it a little better for
me in person,

Henry