Best post on Multidimensional array in c -2

Multidimensional array in C

We can have an array as an element of another array, in simple terms array of an array and this is called a multidimensional array.
In this type of array each element of the array is an array itself.
Two dimensional arrays are the most common simplest form of multidimensional arrays.
We can have more dimensions but it will get complicated and hard to handle.
All the elements in the inner array are of the same data type.
Syntax :
Data_type Array_name[Size_of_array_1][Size_of_array_2][Size_of_array_n];

If we are creating three dimensional array then we will create like this :
int numbs[4][3][7];

Now let’s talk about the most common multidimensional array i.e. two dimensional array.
In overview, a two dimensional array is an array where each element is one array.
E.g of two dimensional array
int a[2][4]={1,2,3,4,5,6,7,8};

In the above example first size i.e. 2 represents the size of outer array and second size i.e. 4 represents the size of inner array.

We can see it as a grid of 2×4 where there are 2 rows and 4 columns, and each block in the grid stores int value. (please refer to the image below)

two dimensional array in c multidimensional array itvoyagers

Initialization of multidimensional array

int a[2][4] = {{1,2,3,4},{5,6,7,8}};
int a[][4] = {1,2,3,4,5,6,7,8};

In second we can see that we have not specified the size of the outer array but we did specify the size of the inner array.
Here the compiler will calculate a size required for the outer array based on the number of the inner arrays.
In this case we have set the size of the inner array to 4 and there are 8 elements in total so we will have 2 inner arrays or we can say that we have 2 array elements.
So the compiler will set the size of the outer element to 2, and if we have set the size of the inner arrays to 2 then we would have 4 inner arrays then the compiler would have set size of outer array to 4.

Access element from two dimensional array.

Let’s take above array as an example
int a[2][4] = {{1,2,3,4},{5,6,7,8}};

Now if we want to print or access value 7 then we have to specify its position or index.
Now first we must know the index of value ‘7’ in that particular inner array.
We can say that the index number of ‘7’ in the inner array is 2. Next we have to check the index number of that particular inner in outer array and that is 1.
So if we want to access or print value 7 from the above array then first we have to specify the index number of the inner array which has it and then index number of 7 in the inner array.
We will use the following line to print/access the value 7 from the above array.

printf(“%d”,a[1][2]);

In the above code, the first index i.e. 1 is the index of the inner array in the outer array and second index i.e. 2 is an index of ‘7’ in the inner array.

Example of two dimensional array in C

/*
Author : ITVoyagers (https://itvoyagers.in/)

Date :25th December 2018

Description : Two Dimensional Array.
*/

#include <stdio.h>
int main()
{
int a[][3]={1,2,3,4,5,6,7,8,9},row,col;
for(row=0;row<3;row++)
{
for(col=0;col<3;col++)
{
printf("%d t",a[row][col]);
}
printf("n");
}
return 0;
}

Advantages of multidimensional array

Two dimensional arrays are easy to access and maintain.
There is no need for various variables we can use one single array to store all value of same data types
It is faster to access.
In his memory locations are allotted in contiguous form.

CHECKOUT OTHER RELATED TOPICS

CHECKOUT OTHER QUIZZES

PRACTICALS IN C PROGRAM

Programs to understand the basic data types and I/O.
Programs on Operators and Expressions.
Programs on decision statements.
Programs on looping.
Programs on arrays.
Programs on functions.
Programs on structures and unions.
Programs on pointers.
Programs on string manipulations.
Practice Programs
1 – C program for leap year, easy and simple code
2 – Best C program to find sum of all digits
3 – Best C program to check Palindrome Number
4 – Best C program to check prime number
5 – Best C program to print Fibonacci series
6 – Best C program to reverse a number
7 – Best C program to reverse an array
8 – Best C program to find average of array
9 – Best C program to find frequency of array element
10 – Best C program to sort array in ascending order
11 – Best C program to find factorial using recursion
12 – Best C program to find sum of all numbers in array
13 – Best C program to find largest element in array
14 – Best C program to swap two numbers without third variable
15 – C Program to check if a number is even or odd
16 – C program to find Simple interest
17 – Best C program to add two matrices
18 – Best C program to count vowels and consonants in a string
19 – Best C program to find LCM
20 – Best C program to find HCF
21 – Best C program to swap two numbers using pointers
22 – Best C program to add two numbers using pointers
23 – Best C program to print Multiplication Table
24 – Best C program to check Armstrong number
25 – Best C program to print half pyramid of star(*)
26 – Best C program to print half pyramid of numbers
27 – Best C program to print reverse half pyramid of star
28 – Best C program to print reverse half pyramid of number
More coming soon…
We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment