Learn C Programming Language

Introduction to C
Variables and Data Types
Operators
Conditional Statements
Loops
Arrays
Strings
Pointers and Memory Management
Functions

C Multidimensional Array

In this lesson, you will learn about two-dimensional arrays, their usage, and examples to better understand the topic.

What is a Two-Dimensional Array?

In the previous lesson, we explained the one-dimension array and its usage. However, sometimes in your program, you may need to store data in a 2-dimensional variable. In multidimensional arrays, this can be stored. Also known as the set of arrays is the multidimensional C array; it enables tabular information to be stored in an array. A Multidimensional C array can be depicted in the form of matrix rows *column.

Multidimensional C array can be two two-dimensional arrays and three two-dimensional arrays. In this lesson, we will focus on the two-dimensional array in the C programming language.

Syntex of two-dimensional arrays

DataType arrayName[rows][columns];

Example

int twoDimensionalArray [3][4];

In the example above, the array named twoDimensionalArray is of int type, and its size is three rows and four columns in each row. It means twoDimensionalArray can have 24 integer types of values memory spaces.

Initialize an Array two-dimensional array

You can initialize a twoDimensionalArray array like this. Here, twoDimensionalArray is a two-dimensional array that holds total memory of 12 elements. twoDimensionalArray is in the form of a table like the below arrays has three rows, and each row has four columns.

int twoDimensionalArray [0][0]= {{11, 3, 70}, {-1, 25,69}};
int twoDimensionalArray [0][1]= {{19, 13, 0}, {-1, 50, 98}};
int twoDimensionalArray [0][2]= {{31, 63, 0}, {25, 57, 93}};

Example of a two-dimensional array in C

#include <stdio.h>
const int STD = 3;
const int MARKS = 4;
int main ()
{
int result[STD][MARKS];	// declare the 2D result array of 3 rows 4 columns
// Nested for loop to store elements in a 2d array
int ob_marks = 78;
for (int i = 0; i < STD; ++i)	//rows
{
for (int j = 0; j < MARKS; ++j)	//columns
{
printf ("Student %d, Marks Obatined %d :\n", i + 1, j + 1);
result[i][j] = ob_marks++;
}
}
printf ("\n------------2DArray------------\n\n");
// nested loop will display elements of a 2d array
for (int i = 0; i < STD; ++i)
{
for (int j = 0; j < MARKS; ++j)
{
printf ("Final Result[%d][%d] = %d\n", i, j, result[i][j]);
}
}
return 0;
}


Output

Student 1, Marks Obatined 1 :
Student 1, Marks Obatined 2 :
Student 1, Marks Obatined 3 :
Student 1, Marks Obatined 4 :
Student 2, Marks Obatined 1 :
Student 2, Marks Obatined 2 :
Student 2, Marks Obatined 3 :
Student 2, Marks Obatined 4 :
Student 3, Marks Obatined 1 :
Student 3, Marks Obatined 2 :
Student 3, Marks Obatined 3 :
Student 3, Marks Obatined 4 :

------------2DArray------------

Final Result[0][0] = 78
Final Result[0][1] = 79
Final Result[0][2] = 80
Final Result[0][3] = 81
Final Result[1][0] = 82
Final Result[1][1] = 83
Final Result[1][2] = 84
Final Result[1][3] = 85
Final Result[2][0] = 86
Final Result[2][1] = 87
Final Result[2][2] = 88
Final Result[2][3] = 89

In the next lesson, you will learn about strings in C and their usage.