C-Two Dimensional Array | Complete Explanation with Examples

0
C-Two Dimensional Array | Complete Explanation with Examples
C-Two Dimensional Array | Complete Explanation with Examples


📑 Table of Contents 

2D Array Definition

2-D Array in C: A two-dimensional array in C is a data structure that can hold multiple values arranged in row and columns using square brackets. Array is a collection of data of same data types. It can store data of same data types means an integer array can store integer value and character array can store character value. 

Array can be used to group of data simultaneously. We can not fetch data from array value directly therefore we can use index point value. 

The indexing of array always start with row 0 and column index value is always integer value. Arrays is allow to store multiple values of the same data type in a single variable.

Syntax of One Dimensional Array in C

  • Data_type: The array data type of data that the array will hold (int, float, char, etc.).
  • Array_name: The name you choose for the 2D array.
  • Row: The number of rows in the 2D array.
  • Columns: The number of columns in the array.

data_type array_name[row_size][column_size]={Array_Values};
int a[3][3]={{10,20,30},{40,50,60},{70,80,90}};

2D Array Initialization in C

Array is the are several ways to initialize 2D arrays. The choice of initialization method depends on the requirements of your C program. Here are some common methods for initializing 2D arrays in C.

Syntax for 2D Array Initialization at Declaration


int a[3][3]={{10,20,30},{40,50,60},{70,80,90}};
a[0][0]=10
a[0][1]=20
a[0][2]=30
a[1][0]=40
a[1][1]=50
a[1][2]=60
a[2][0]=70
a[2][1]=80
a[2][2]=90

The values to initialize the array with the number of elements inside the curly braces must match the size of the array.

We declare and initialize an integer array numbers with a size of row 3 and column size 3 and we provide 9 values.

2D Array Example Program in C

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int a[3][3]={{10,20,30},{40,50,60},{70,80,90}};
printf("Value of a[0][0]:%d\n",a[0][0]);
printf("Value of 
a[0][1]:%d\n",a[0][1]);
printf("Value of 
a[0][2]:%d\n",a[0][2]);
printf("Value of 
a[1][0]:%d\n",a[1][0]);
printf("Value of a[1][1]:%d\n",a[1][1]);
printf("Value of a[1][2]:%d\n",a[1][2]);
printf("Value of a[2][0]:%d\n",a[2][0]);
printf("Value of a[2][1]:%d\n",a[2][1]);
printf("Value of a[2][2]:%d\n",a[2][2]);
getch();
}

============OUTPUT============
Value of a[0][0]:10

Value of a[0][1]:20
Value of a[0][2]:30
Value of a[1][0]:40
Value of a[1][1]:50
Value of a[1][2]:60
Value of a[2][0]:70
Value of a[2][1]:80
Value of a[2][2]:90

Print Array Element Using For Loop  

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int i,j;
int a[3][3]={{10,20,30},{40,50,60},{70,80,90}};
for(i=0;i<3;i++){

for(j=0;j<3;j++)
{

printf("Value of a[%d][%d]:%d\n",i,j,a[i][j]);
}
}
getch();
}

============OUTPUT============
Value of a[0][0]:10
Value of a[0][1]:20
Value of a[0][2]:30
Value of a[1][0]:40
Value of a[1][1]:50
Value of a[1][2]:60
Value of a[2][0]:70
Value of a[2][1]:80
Value of a[2][2]:90

2d Array Matrix Program in C  

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int i,j;
int a[3][3]={{10,20,30},{40,50,60},{70,80,90}};
for(int i=0;i<3;i++){

for(j=0;j<3;j++)
{

printf("%d ",a[i][j]);
}
printf("\n");

}
getch();
}
============OUTPUT============
10 20 30
40 50 60
70 80 90
10 20 30 40 50 60 70 80 90

Size of 2 Dimensional Array in C

A array size total number of array elements can be stored in 2D array can be calculate by multiply the size of both dimensions row and columns including the array a[5]5]. 2D array is data type int a[5][5] can be store total array elements like (5*5) => 25 elements.  
You can find size of memory in bytes than you can multiply the size of a single element by the total number of elements in the array. integer data type consume 4 bytes in memory. You can find memory size this method 
a[5][5]=5*5*4=100 bytes.

How To Get Array Length in C  

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int i,j;
int a[5][5] = {{20, 48, 87,56,68}, {56, 19,10,89,80},{54,56,34,68,45},{67,45,76,34,68},{44,34,56,77,55}};
for(i=0;i<5;i++){
for(j=0;j<5;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
}

============OUTPUT============
20 48 87 56 68
56 19 10 89 80
54 56 34 68 45
67 45 76 34 68
44 34 56 77 55

Run Time Initialization of 2D Array in C

Runtime initialization 2d array when the values for the array elements are  determined and assigned during the execution of the program. This is useful when you need to read values from user input perform calculations to determine the array elements. Arrays initialized at runtime can be modified during program execution.

How to Take User Input in 2D Array in C  

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int a[3][3];
int i,j,r,c;
printf("Enter Row Size:");
scanf("%d",&r);
printf("Enter Colum Size:");
scanf("%d",&c);
for(i=0;i<r;i++){
for(j=0;j<c;j++){
    printf("Enter 2D Array Element[%d][%d]:",i,j);
    scanf("%d",&a[i][j]);
}
}
printf("Your 2D Matrix\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
}

============OUTPUT============
Enter Row Size:3

Enter Column Size:3
Enter 2D Array Elements[0][0]:12
Enter 2D Array Elements[0][1]:13
Enter 2D Array Elements[0][2]:45
Enter 2D Array Elements[1][0]:67
Enter 2D Array Elements[1][1]:87
Enter 2D Array Elements[1][2]:45
Enter 2D Array Elements[2][0]:78
Enter 2D Array Elements[2][1]:98
Enter 2D Array Elements[2][2]:12
Your 2D Matrix
12 13 45
67 87 45
78 98 12

Update 2D Array Elements in C

2D Array modify or change the value of an array element by assigning a new value to  using index number.

How To Update 2D Array Element in C  

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int i,j,a[3][3] = {{10, 28, 80},{56, 90, 20},{20,60,70}};
printf("Before Update 2D Matrix\n");
for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            printf("%d ", a[i][j]);
        }
printf("\n");
}
printf("After Update 2D Matrix\n");
a[1][0] = 99;
for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            printf("%d ", a[i][j]);
        }
printf("\n");
}
getch();
}
============OUTPUT============
Before Update 2D Matrix
10 28 80
56 90 20
20 60 70
After Update 2D Matrix
10 28 80
99 90 20
20 60 70

2D Arithmetic Method : It is a below program is a two-dimensional array in C. You can use this program create more program like Addition, Subtraction, Multiplication and division using this method. You can create fully dynamically 2D array program. You can only addition sign change now you can use arithmetic operation perform like (+,-,*,/). 

2D Array Addition Program in C  

#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
int x[3][3];
int y[3][3];
int z[3][3];
int i,j,r,c;
printf("Enter First Matrix Row Size:");
scanf("%d",&r);
printf("Enter First Matrix Colum Size:");
scanf("%d",&c);
for(i=0;i<r;i++){
for(j=0;j<c;j++){
    printf("Enter First Matrix Array Element[%d][%d]:",i,j);
    scanf("%d",&x[i][j]);
}
}
printf("Your First Matrix\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("%d ",x[i][j]);
}
printf("\n");
}
printf("Enter Second Matrix Row Size:");
scanf("%d",&r);
printf("Enter Second Matrix Colum Size:");
scanf("%d",&c);
for(i=0;i<r;i++){
for(j=0;j<c;j++){
    printf("Enter Second Matrix Array Element[%d][%d]:",i,j);
    scanf("%d",&y[i][j]);
}
}
printf("Your Second Matrix\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("%d ",y[i][j]);
}
printf("\n");
}
printf("Addition of Two Matrix\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
z[i][j]=x[i][j]+y[i][j]; //sign change create more program.
printf("%d ",z[i][j]);
}
printf("\n");
}
}

============OUTPUT============
Enter First Matrix Row Size:2

Enter First Matrix Colum Size:2
Enter First Matrix Array Element[0][0]:1
Enter First Matrix Array Element[0][1]:2
Enter First Matrix Array Element[1][0]:3
Enter First Matrix Array Element[1][1]:4
Your First Matrix
1 2
3 4
Enter Second Matrix Row Size:2
Enter Second Matrix Colum Size:2
Enter Second Matrix Array Element[0][0]:5
Enter Second Matrix Array Element[0][1]:6
Enter Second Matrix Array Element[1][0]:7
Enter Second Matrix Array Element[1][1]:8
Your Second Matrix
5 6
7 8
Addition of Two Matrix
6 8
10 12

Conclusion of Two Dimensional Array in C

Conclusion: This is a all about 2D array in C language. I have complete every concept of related 2d array. Array program you can understand simple easy method for beginners. If your any query or problems array related than you can contact me by email. I can more easy solution provide.
Tags

Post a Comment

0 Comments
Post a Comment (0)
To Top