×

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C

The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant order.

Syntax

The general form of a multidimensional array can be written as;

 datatype array_name [size 1] [size 2] [size 3] … [size N];
 Or
 datatype array_name [number of tables] [size_of_row][size of column]; 

Where;

  • Data type:It is the data type present in the C standard in which the array elements will be stores. Any data type in the C standard is applicable such as, int, float, double, and so on.
  • array_name: It is the name of the array which is user specified.
  • size 1, size 2, size 3,... size N: These are the size of the dimension which will represent an array.
  • Number of tables: It is the total number of tables an array can accept such as, a 2D array can consist of up to single table while other multidimensional might have more of it.
  • size_of_row: It represents the elements a row can store.
  • size_of_column: It represents the elements a column can store.

Consider a three-dimensional array, and it can be represented as;

int threeD [5][6][3];

A two-dimensional array is represented as;

int twoD [5][6];

The total elements that will be stored in a multidimensional array can be calculated by just multiplying the size of all the dimensions that the user will declare.

E.g.:

int threeD [5][6][3];

The above-mentioned array can store a total of (5 * 6 * 3) elements, i.e., 90 elements.

In the same manner, an array int a[10][20] can store a total of (10 * 20) elements, i.e., 200 elements.

Two-dimensional array

It is one of the simplest forms of the multidimensional array. A two-dimensional array or a 2D array is a list of a one-dimensional array. Hence to declare a 2D array integer array of size [m][n], the declaration would be something like; int twoDimensional [m][n]; .

A two-dimensional array can also be called as a matrix.There are two ways of initializing a two-dimensional array during the time of declaration.

 int twoDim [3][3] = {
             {2, 5, 6},
             {4, 1, 9},
             {3, 4, 8}
 }; 

Or

int twoDim[3][3] = { 2, 5, 6, 4, 1, 9, 3, 4, 8};

The above two declarations are the ones where the values are decided while writing the program itself. In order to make that user input, for loops can be used and then can be displayed and more operations on it can be performed.

E.g.:

 #include<stdio.h>
 int main()
 {
 /* 2D array declaration*/
 int disp[2][3];
 /*Counter variables for the loop*/
 int i, j;
 for(i=0; i<2; i++)
 {
 for(j=0;j<3;j++)
 {
 printf("Enter value for disp[%d][%d]:", i, j);
 scanf("%d", &disp[i][j]);
 }
 }
 //Displaying the array elements
 printf("Two Dimensional array elements:\n");
 for(i=0; i<2; i++)
 {
 for(j=0;j<3;j++)
 {
 printf("%d ", disp[i][j]);
 if(j==2)
 {
 printf("\n");
 }
 }
 }
 return 0;
 } 

Output

Multi-dimensional Arrays in C

Three-dimensional array

Initializing a three-dimensional array is as same as that of the two-dimensional array. Hence, the difference is that the number of dimensions increases so that the number of nested braces will also increase.

Syntax

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};

While accessing the elements in the 3D(three dimensional) array, which is mostly similar to the 2D array, one has to make use of the three loops instead of the two loops for one additional dimensional in the 3D array.

E.g.:

 // C Program to store and print 12 values entered by the user
 #include <stdio.h>
 int main()
 {
 int test[2][3][2];
 printf("Enter 12 values: \n");
 for (int i = 0; i< 2; ++i)
 {
 for (int j = 0; j < 3; ++j)
 {
 for (int k = 0; k < 2; ++k)
 {
 scanf("%d", &test[i][j][k]);
 }
 }
 }
   // Printing values with proper index.
 printf("\nDisplaying values:\n");
 for (int i = 0; i< 2; ++i)
 {
 for (int j = 0; j < 3; ++j)
 {
 for (int k = 0; k < 2; ++k)
 {
 printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
 }
 }
 }
 return 0;
 } 

Related Topics

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

What is algorithm in C?

What is an algorithm? In computer programming languages, an algorithm is set of statement that solves a particular problem. In C, first step of every algorithm is Start and last step of...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.