## multidimensional array

5 posts / 0 new
Author
Message

I need help, I am trying to understand how multi dimensional array store values.

```#define MAX_YEARS   2
#define MAX_COURSE  3
#define MAX_STUDENT 5

int main ()
{
int marks[YEARS][COURSES][STUDENTS];

return 0;
}```

There will be  30  value of marks,  Will any one help me how to initialized array  ?

This is just an example  I had created to understand the multidimensional array

This topic has a solution.
Last Edited: Sat. Feb 15, 2020 - 12:20 PM
This reply has been marked as the solution.

Again, this will be covered in any 'C' textbook; eg, in the one you have already been pointed to:

https://publications.gbdirect.co.uk//c_book/chapter5/arrays.html

So which part(s), specifically, are you stuck on ?

Some notes on multi-dimension arrays here:

https://www.avrfreaks.net/commen...

EDIT

And, again, the easiest way to experiment with this to see what's happening would be to do it on a PC

EDIT 2

Being standard 'C' stuff - not specific to AVR - this is widely covered by tutorials on the interwebs

which gives this example:

```int x[2][3][4] =
{
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }
};```

to see how it works, try just commenting each element; eg,

```int x[2][3][4] =
{
/* x[0] */  {
/* x[0][0] */   {
/* x[0][0][0] */ 0,
/* x[0][0][1] */ 1,
/* x[0][0][2] */ 2,
/* x[0][0][3] */ 3
},
/* x[0][1] */   {
/* x[0][1][0] */ 4,
/* x[0][1][1] */ 5,
/* x[0][1][2] */ 6,
/* x[0][1][3] */ 7
},
/* x[0][2] */   {
/* x[0][2][0] */ 8,
/* x[0][2][1] */ 9,
/* x[0][2][2] */ 10,
/* x[0][2][3] */ 11
}
},
/* x[1] */  {
/* x[1][0] */   {
/* x[0][1][0] */ {12,
:
:
etc```

EDIT 3

One thing to note is that only the first dimension can be omitted in the definition:

```int one_dim[] = { 1,2,3 };

int two_dim[][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} };

int three_dim[][3][4] =
{
{ { 0, 1, 2, 3}, { 4, 5, 6, 7}, { 8, 9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }
};```

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Feb 15, 2020 - 12:08 PM

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Expeimenting on a PC I agree is by far the best way to learn.

In terms of initialising, you end up with lots of curly braces :)

Here's another example to run on a PC and experiment with.

```#define MAX_YEARS   2
#define MAX_COURSE  3
#define MAX_STUDENT 5

int marks[MAX_YEARS][MAX_COURSE][MAX_STUDENT] =
{
{
{ 10, 11, 12, 13, 14 },
{ 20, 21, 22, 23, 24 },
{ 30, 31, 32, 33, 34 }
},

{
{ 50, 51, 52, 53, 54 },
{ 60, 61, 62, 63, 64 },
{ 70, 71, 72, 73, 74 }
}
};

int main(void)
{
for (int y = 0; y < MAX_YEARS; y++)
{
for (int c = 0; c < MAX_COURSE; c++)
{
for (int s = 0; s < MAX_STUDENT; s++)
{
printf("marks[%d][%d][%d]=%d\n", y, c, s, marks[y][c][s]);
}
}
}
return 0;
}
```

sky33 wrote:

```#define MAX_YEARS   2
#define MAX_COURSE  3
#define MAX_STUDENT 5

int marks[YEARS][COURSES][STUDENTS];
```

Did you mean (as  MrKendo showed):

```#define MAX_YEARS   2
#define MAX_COURSE  3
#define MAX_STUDENT 5

int marks[MAX_YEARS][MAX_COURSES][MAX_STUDENTS];```

If so, those #define names are poorly chosen!

In 'C', the value you put in the brackets in an array declaration is the number of elements.

Because 'C' array indexes start at zero, the maximum index value will be one less than the number of elements.

So better names would be:

```#define NUM_YEARS   2
#define NUM_COURSE  3
#define NUM_STUDENT 5

int marks[NUM_YEARS][NUM_COURSES][NUM_STUDENTS];```

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Feb 15, 2020 - 12:24 PM