# Thread: Cannot understand this multi dimensional array concept

1. ## Cannot understand this multi dimensional array concept

Hello everyone,

Am learning C programming from "Programming In C" by "Stephen Kochan". The following para has been copied from the book straightaway coz I want you guys to explain me the part in bold.

As with one-dimensional arrays, it is not required that the entire array be initialized. A statement such as
int M[4][5] = {
{ 10, 5, -3 },
{ 9, 0, 0 },
{ 32, 20, 1 },
{ 0, 0, 8 }
};
only initializes the first three elements of each row of the matrix to the indicated values. The remaining values are set to 0 . Note that, in this case, the inner pairs of braces are required to force the correct initialization.Without them, the first two rows and the first two elements of the 3rd row would have been initialized instead. (Verify to yourself that this is the case.)

2. One of the great things about programming is the ease with which you can test these things out, for yourself. Try it and print it up. See what you get. Don't be afraid to go explore these things.

3. The result that am getting on executing the code(with or without the inner braces) is 2293440!! Now it has confused me even more. I tried it the following way:
Code:
```#include <stdio.h>

int main ()

{

int M[4][5] = { 10, 5, -3, 9, 0, 0, 32, 20, 1, 0, 0, 8 } ;

printf ("%i", M) ;

return 0 ;

}```
AND

Code:
```#include <stdio.h>

int main ()

{

int M[4][5] = {
{ 10, 5, -3 },
{ 9,  0,  0 },
{ 32, 20, 1 },
{  0,  0, 8 }
};

printf ("%i", M) ;

return 0 ;

}```

4. > printf ("%i", M) ;
This does NOT print the whole array!
It's just (badly) taking the pointer to the start of the array - &M[0][0], and printing it in some integer representation.

Try for loops to print each M[row][col] instead.

5. Thanks Salem. Now am getting the result 10000100021473157120 after making
the following changes to the code. I dont think this is right.

Code:
```#include <stdio.h>

int main ()

{

int M[4][5] = {
{ 10, 5, -3 },
{ 9,  0,  0 },
{ 32, 20, 1 },
{  0,  0, 8 }
};

int i, k ;

for (i = 0, k = 0; i <= 3, k <= 4; i++, k++)
printf ("%i%i", M[i][k]) ;

return 0 ;

}```

6. The easiest and clearest way to work with a 2D array print out, is to use an outer for loop to handle the row logic, and an inner for loop to handle the column logic, within each row.

Use variable names that are immediately meaningful to you, such as row and col, or r and c, instead of i and k, etc.

The easiest and clearest way to work with a 2D array print out, is to use an outer for loop to handle the row logic, and an inner for loop to handle the column logic, within each row.

It'll be great if you or anyone can give me an example of the same with respect to the above program.

8. Originally Posted by exus69

It'll be great if you or anyone can give me an example of the same with respect to the above program.
You may find these tutorial useful:

Basic matrix know-how
Dystopian Code: How to Create, Assign Values to and Output a Matrix in C

Other matrix tutorials (sum, product, etc) that operate with more advanced concepts:
Dystopian Code: C

9. Ok lets consider a very simple code of a multi dimensional array.

Code:
```#include <stdio.h>

int main ()

{
int sun[2][3] = {
{0, 1, 2},
{3, 4, 5}

} ;

int r, c ;

for (r = 0, c = 0; r <= 1, c <= 2; r++, c++)
printf ("%i", sun[r][c]) ;

return 0 ;

}```
Instead of the output 012345 am getting a weird number 044200192.
What is the mistake here? The program is supposed to display the digit in this order right? [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] which means it should display 012345 right?

10. Let's consider that you need to consider the replies you have been given on this matter:

Use nested for loops (that's two for loops one for each dimension of a 2D array), and work from that. You didn't look through the tutorial given in the link, did you?

I remain quite unclear on this whole concept of asking for help, getting exactly the help you asked for, and then returning with unchanged types of code.

Honestly, if you hold your breath long enough, you will be complaining next about the lack of air, I just know it.

The easiest and clearest way to work with a 2D array print out, is to use an outer for loop to handle the row logic, and an inner for loop to handle the column logic, within each row.
Originally Posted by exus69

It'll be great if you or anyone can give me an example of the same with respect to the above program.

I did ask you(or anyone) who could show me practical code what you actually mean.
Am a real noob only dependant on this forum to get help on C but instead of showing
the code ur criticizing me of not following what you said.

If you notice my code in post#9, I did follow what you suggested in post#6 of using
meaningful variable names like r for rows and c for columns. Am sorry if I offended you
in any way...

12. The link in post #8 of your thread has ALL the info you were asking for, including examples. How to get values to print from the 2D array, how to enter values into a 2D array - it's ALL right there, with code examples.

Using more meaningful names for 2D array indices is helpful, but so is studying the tutorial links that answer your questions, in greater detail than we can, in a post or two.

Go! Study! Learn! USE the resources that you have been given.

Oh don't think for one moment that I won't criticize a student who won't use the study aids that they have been given! Wasting your brain is a sin in my book, and I'll nail up your hide on my barn door -- friend!

13. exus69, Try to write with words how would you print array's elements in the order you want. Something like....

Code:
```Go to the first row (i = 0) , check if that row exists (i < max rows).
Go to the first element of that row, (k = 0), check if the element exist (k < max cols)
Print that element.
Move to the next element in that row. (++k)
Move to the next row. (++i)```
Always read any tutorial recommended in here.

I agree its my mistake. I should have gone through that link thoroughly instead of asking here again. But I wanna know whats the technical difference in my approach
Code:
``` for (r = 0, c = 0; r <= 1, c <= 2; r++, c++)
printf ("%i", sun[r][c]) ;```
Code:
``` for (r = 0; r <= 1; r++)
{
for (c = 0; c <= 2; c++)
printf ("%i", sun[r][c]) ;

}```

15. Originally Posted by ardavirus
Code:
```Go to the first row (i = 0) , check if that row exists (i < max rows).
Go to the first element of that row, (k = 0), check if the element exist (k < max cols)
Print that element.
Move to the next element in that row. (++k)
Move to the next row. (++i)```
Hello ardavirus,

You answered my question even before I was writing my question!!!
But dont you think, technically, the following code does the same thing what u've mentioned:

Code:
```for (r = 0, c = 0; r <= 1, c <= 2; r++, c++)
printf ("%i", sun[r][c]) ;```