# C Multidimensional Arrays

Multidimensional arrays are arrays that have more than one dimension. In C, multidimensional arrays can have up to three dimensions. In this article, we will cover the basics of multidimensional arrays in C and provide two examples to illustrate their use.

### Declaring a Multidimensional Array

Multidimensional arrays are declared in the same way as one-dimensional arrays, with the exception that you need to specify the size of each dimension. The following is an example of how to declare a two-dimensional array in C:

``1int array;``

In this example, "array" is a two-dimensional array with 3 rows and 3 columns.

### Value Assignment

Values can be assigned to multidimensional arrays using a nested loop, with the outer loop controlling the rows and the inner loop controlling the columns. The following is an example of how to assign values to a two-dimensional array in C:

``````1int i, j;
2int array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
3
4for (i = 0; i < 3; i++) {
5  for (j = 0; j < 3; j++) {
6    printf("%d ", array[i][j]);
7  }
8  printf("\n");
9}``````

In this example, a nested for loop is used to iterate through the elements of the "array" two-dimensional array and assign values to each element. The values are then printed using the printf() function.

### Accessing Index Values

Index values in a multidimensional array can be accessed by using multiple square brackets. The following is an example of how to access the value of an element in a two-dimensional array in C:

``````1int i, j;
2int array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
3
4i = 1;
5j = 2;
6
7printf("The value of array[%d][%d] is: %d\n", i, j, array[i][j]);``````

In this example, the value of the element in the second row and third column of the "array" two-dimensional array is accessed and printed using the printf() function.

#### Example 1

The following is an example of how to use a two-dimensional array to store the marks of students in a class:

``````1#include <stdio.h>
2
3int main() {
4  int i, j;
5  int marks = {{50, 60, 70, 80}, {75, 65, 55, 45}, {90, 80, 70, 60}};
6  int sum = {0, 0, 0};
7
8  for (i = 0; i < 3; i++) {
9    for (j = 0; j < 4; j++) {
10      sum[i] += marks[i][j];
11    }
12  }
13
14  for (i = 0; i < 3; i++) {
15    printf("The total marks of student %d is: %d\n", i+1, sum[i]);
16  }
17
18  return 0;
19}``````

Output:

``1The total marks of student 1 is: 260``

Example 2:

The following is an example of how to use a three-dimensional array to store the marks of students in multiple subjects in a class:

``````1#include <stdio.h>
2
3int main() {
4  int i, j, k;
5  int marks = {{{50, 60, 70, 80}, {75, 65, 55, 45}, {90, 80, 70, 60}}, {{55, 65, 75, 85}, {95, 85, 75, 65}, {80, 70, 60, 50}}};
6  int sum = {{0, 0, 0}, {0, 0, 0}};
7
8  for (i = 0; i < 2; i++) {
9    for (j = 0; j < 3; j++) {
10      for (k = 0; k < 4; k++) {
11        sum[i][j] += marks[i][j][k];
12      }
13    }
14  }
15
16  for (i = 0; i < 2; i++) {
17    for (j = 0; j < 3; j++) {
18      printf("The total marks of student %d in subject %d is: %d\n", j+1, i+1, sum[i][j]);
19    }
20  }
21
22  return 0;
23}``````

Output:

``````1The total marks of student 1 in subject 1 is: 260
2The total marks of student 2 in subject 1 is: 385
3The total marks of student 3 in subject 1 is: 310
4The total marks of student 1 in subject 2 is: 260
5The total marks of student 2 in subject 2 is: 385
6The total marks of student 3 in subject 2 is: 310``````

In this example, a three-dimensional array "marks" is used to store the marks of students in multiple subjects. A nested for loop is used to calculate the sum of marks for each student in each subject, and the results are printed using the printf() function.