English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Online Tools

O)

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Flow Control

C Language Structure

C Language File

C Others

C Language Multi-dimensional Arrays

C Language Reference Manual

In this tutorial, you will learn to use multidimensional arrays (two-dimensional and three-dimensional arrays) with examples.

In C programming, you can create an array of arrays. These arrays are called multidimensional arrays. For example,3][4]);

float x[2where x is a two-dimensional (12d) array. This array can hold3elements. You can view the array as having4columns. Each row has

Similarly, you can declare a three-dimensional (3d) array. For example,

float y[2][4][3]);

In this case, the array y can hold24elements.

Initialization of multidimensional array

Below are the methods of initializing two-dimensional and three-dimensional arrays:

Initialization of two-dimensional array

//Different methods of initializing a two-dimensional array
int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
         
int c[][3] = {{1, 3, 0}, {-1, 5, 9}};
                
int c[2][3] = {1, 3, 0, -1, 5, 9};

Initialization of three-dimensional array

You can initialize a three-dimensional array in a similar way to a two-dimensional array. Here is an example

int test[2][3][4] = {
    {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
    {{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}};

Example1: used to store and print a two-dimensional array of values

//C program stores and displays the temperature of two cities within a week.
#include <stdio.h>
const int CITY = 2;
const int WEEK = 7;
int main()
{
  int temperature[CITY][WEEK];
  //Use nested loops to store values in a two-dimensional array
  for (int i = 0; ++i)
  {
    for (int j = 0; ++j)
    {
      printf("City %d, Day %d: ", i + 1, j + 1);
      scanf("%d", &temperature[i][j]);
    }
  }
  printf("\nDisplay values: \n\n");
  //Use nested loops to display the values of a two-dimensional array
  for (int i = 0; ++i)
  {
    for (int j = 0; ++j)
    {
      printf("City %d, Day %d = %d\n", i + 1, j + 1, temperature[i][j]);
    }
  }
  return 0;
}

Output Result

City 1, Day 1: 33
City 1, Day 2: 34
City 1, Day 3: 35
City 1, Day 4: 33
City 1, Day 5: 32
City 1, Day 6: 31
City 1, Day 7: 30
City 2, Day 1: 23
City 2, Day 2: 22
City 2, Day 3: 21
City 2, Day 4: 24
City 2, Day 5: 22
City 2, Day 6: 25
City 2, Day 7: 26
Display Value: 
City 1, Day 1 = 33
City 1, Day 2 = 34
City 1, Day 3 = 35
City 1, Day 4 = 33
City 1, Day 5 = 32
City 1, Day 6 = 31
City 1, Day 7 = 30
City 2, Day 1 = 23
City 2, Day 2 = 22
City 2, Day 3 = 21
City 2, Day 4 = 24
City 2, Day 5 = 22
City 2, Day 6 = 25
City 2, Day 7 = 26

Example2: sum of two matrices

//C program to find2*2Order matrix sum
#include <stdio.h>
int main()
{
  float a[2][2], b[2][2], result[2][2]);
  //Use nested for loops to get input
  printf("Input elements of the first matrix\n");
  for (int i = 0; 2; ++i)
    for (int j = 0; 2; ++j)
    {
      printf("Input a%d%d: ", i + 1, j + 1);
      scanf("%f", &a[i][j]);
    }
  // Use nested for loops to get input
  printf("Input elements of the second matrix\n");
  for (int i = 0; 2; ++i)
    for (int j = 0; 2; ++j)
    {
      printf("Input b%d%d: ", i + 1, j + 1);
      scanf("%f", &b[i][j]);
    }
  // Add corresponding elements of two arrays
  for (int i = 0; 2; ++i)
    for (int j = 0; 2; ++j)
    {
      result[i][j] = a[i][j] + b[i][j];
    }
  //Display sum
  printf("\nSum of matrix:\n");
  for (int i = 0; 2; ++i)
    for (int j = 0; 2; ++j)
    {
      printf("%.1f\t", result[i][j]);
      if (j == 1)
        printf("\n");
    }
  return 0;
}

Output Result

Input elements of the first matrix
Input a11: 2;
Input a12: 0.5;
Input a21: -1.1;
Input a22: 2;
Input elements of the second matrix
Input b11: 0.2;
Input b12: 0;
Input b21: 0.23;
Input b22: 23;
Sum of matrix:
2.2     0.5
-0.9    25.0

Example3: three-dimensional array

//C program to store and print user input of12values
#include <stdio.h>
int main()
{
  int test[2][3][2]);
  printf("Input ",12values: \n");
  for (int i = 0; 2; ++i)
  {
    for (int j = 0; 3; ++j)
    {
      for (int k = 0; 2; ++k)
      {
        scanf("%d", &test[i][j][k]);
      }
    }
  }
  //Print values using appropriate indices.
  printf("\nDisplay values:\n");
  for (int i = 0; 2; ++i)
  {
    for (int j = 0; 3; ++j)
    {
      for (int k = 0; 2; ++k)
      {
        printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
      }
    }
  }
  return 0;
}

Output Result

Input12Values: 
1
2
3
4
5
6
7
8
9
10
11
12
Display Value:
test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12