amp-web-push-widget button.amp-subscribe { display: inline-flex; align-items: center; border-radius: 5px; border: 0; box-sizing: border-box; margin: 0; padding: 10px 15px; cursor: pointer; outline: none; font-size: 15px; font-weight: 500; background: #4A90E2; margin-top: 7px; color: white; box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.5); -webkit-tap-highlight-color: rgba(0, 0, 0, 0); } .amp-logo amp-img{width:190px} .amp-menu input{display:none;}.amp-menu li.menu-item-has-children ul{display:none;}.amp-menu li{position:relative;display:block;}.amp-menu > li a{display:block;} /* Inline styles */ span.acss0eb77{background-color:#00ccff;}figure.acss2676c{max-width:300px;}figure.acss465ae{max-width:270px;}div.acss138d7{clear:both;}div.acssf5b84{--relposth-columns:3;--relposth-columns_m:2;--relposth-columns_t:2;}div.acss157f4{aspect-ratio:1/1;background:transparent url(https://code4coding.com/wp-content/uploads/2017/02/datatype1-300x220.jpg) no-repeat scroll 0% 0%;height:150px;max-width:150px;}div.acss6bdea{color:#333333;font-family:Arial;font-size:12px;height:75px;}div.acss0b642{aspect-ratio:1/1;background:transparent url(https://code4coding.com/wp-content/uploads/2021/06/string-elemnts.jpg) no-repeat scroll 0% 0%;height:150px;max-width:150px;}div.acss540ab{aspect-ratio:1/1;background:transparent url(https://code4coding.com/wp-content/uploads/2021/06/chaarray1.jpg) no-repeat scroll 0% 0%;height:150px;max-width:150px;}div.acss0df58{aspect-ratio:1/1;background:transparent url(https://code4coding.com/wp-content/uploads/2021/06/array-in-id.jpg) no-repeat scroll 0% 0%;height:150px;max-width:150px;}a.acss9bfd5{font-size:14.52427184466pt;}a.acssc37f8{font-size:16.427184466019pt;}a.acss29e97{font-size:16.631067961165pt;}a.acss361c8{font-size:17.174757281553pt;}a.acss51c7b{font-size:20.029126213592pt;}a.acssa2e10{font-size:20.097087378641pt;}a.acss5dd67{font-size:21.728155339806pt;}a.acssf0e8e{font-size:12.077669902913pt;}a.acss759e3{font-size:17.922330097087pt;}a.acss0abf8{font-size:21.252427184466pt;}a.acss6bf84{font-size:13.504854368932pt;}a.acss349b0{font-size:10.038834951456pt;}a.acssf23c5{font-size:8pt;}a.acss7e0a8{font-size:9.2233009708738pt;}a.acsse6f77{font-size:16.970873786408pt;}a.acssc51bb{font-size:14.116504854369pt;}a.acss38f57{font-size:11.26213592233pt;}a.acss066f0{font-size:22pt;}a.acss4e811{font-size:17.31067961165pt;}a.acss9cc90{font-size:12.417475728155pt;}a.acss01721{font-size:15.339805825243pt;}a.acsse9f66{font-size:15.543689320388pt;}a.acss72254{font-size:20.708737864078pt;}a.acsseedeb{font-size:20.776699029126pt;}a.acss25b87{font-size:14.320388349515pt;}a.acss7c517{font-size:12.757281553398pt;}a.acss7a3ee{font-size:18.941747572816pt;}a.acssf92d5{font-size:18.26213592233pt;}a.acss551d3{font-size:16.291262135922pt;} .icon-widgets:before {content: "\e1bd";}.icon-search:before {content: "\e8b6";}.icon-shopping-cart:after {content: "\e8cc";}
Array

Two dimension Array in C programming language

Two dimensional Array in C programming language

We will learn about Two dimension Array in C programming language

In the C programming language, an array is a data structure which is of fixed size and sequential collection of an element of the same type of data. An array can be used to represent a list of the similar type of elements(eg – list of numbers, list of names).

It is one of the ways to group similar type of elements(data) with a single variable name.

 

Three type of arrays in C Programming language

  1. One dimensional Array in C language
  2. Two dimensional Array in C language
  3. Three dimensional Array in C language

Two Dimension Array

Declaration of two dimension Array

Syntax

return_type Array_name[size][size];

Example

int marks[4][6];
char alphabet[3][4];
String name[5][6];

2 D array memory representation

This is a Two dimension array named x with 5 rows and 5 column

int array= x[5][5];
Two D Array

Index is started in zero (0), there fore first index of array – x[0][0]; and last index of array – x[4][4];

 

 

How to find an index in two dimension Array

Find the index of 2 D array

 

Initializing Two dimension Array

Method 1

arr[0][0]=65;   //initialize first elements by value 65

//we can assign the value to every index, like this

 

Method 2

int age[3][4]={
{23,54,65,76},
{34,65,78,98},
{65,56,43,86}
};

Method 3

int age[3][4]={34,65,78,98,76,54,32,13,15,26,3,38}
more valid declarations 
int array_name[2][3]={23,45,65,43,73,32}; //declaration 1 - mentioned above 
int array_name[][3]={23,45,65,43,73,32}; //declaration 2 
int array_name[2][]={23,45,65,43,73,32}; //declaration 3 
int array_name[][]={23,45,65,43,73,32}; //declaration 4

 

Example for method 1

int marks [4][6]={

{50,71,47,63,58,54}     //initialized row index 0

{54,86,37,40,72,51}     //initialized row index 1

{96,83,58,52,48,34}   //initialized row index 2

{84,69,65,48,63,86}    //initialized row index 3

 

Program 1

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int array[4][5]={  //Two dimension array    
        {353,684,653,897,356},
        {585,764,809,357,865},
        {974,267,836,163,682},
        {734,486,263,543,765}
    };//declaration & initialization
printf("Output here\n\n");
    printf("%d,",array[0][0]);
    printf("%d,",array[0][1]);
    printf("%d,",array[0][2]);
    printf("%d,",array[0][3]);
    printf("%d",array[0][4]);
printf("\n");
    printf("%d,",array[1][0]);
    printf("%d,",array[1][1]);
    printf("%d,",array[1][2]);
    printf("%d,",array[1][3]);
    printf("%d",array[1][4]);
printf("\n");
    printf("%d,",array[2][0]);
    printf("%d,",array[2][1]);
    printf("%d,",array[2][2]);
    printf("%d,",array[2][3]);
    printf("%d",array[2][4]);
printf("\n");
    printf("%d,",array[3][0]);
    printf("%d,",array[3][1]);
    printf("%d,",array[3][2]);
    printf("%d,",array[3][3]);
    printf("%d",array[3][4]);
    getch();
    return 0;
}

 

When we executed above program, produces the following result

353,684,653,897,356
585,764,809,357,865
974,267,836,163,682
734,486,263,543,765

 

Program 2

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int array[4][5]={
        {353,684,653,897,356},
        {585,764,809,357,865},
        {974,267,836,163,682},
        {734,486,263,543,765}
    };//declaration & initialization
printf("Output here\n\n");
printf("print first row element\n\n");
    printf("array[0][0]:%d \n",array[0][0]);
    printf("array[0][1]:%d \n",array[0][1]);
    printf("array[0][2]:%d \n",array[0][2]);
    printf("array[0][3]:%d \n",array[0][3]);
    printf("array[0][4]:%d \n\n",array[0][4]);
printf("print second row element\n\n");
    printf("array[1][0]:%d \n",array[1][0]);
    printf("array[1][1]:%d \n",array[1][1]);
    printf("array[1][2]:%d \n",array[1][2]);
    printf("array[1][3]:%d \n",array[1][3]);
    printf("array[1][4]:%d \n\n",array[1][4]);
printf("print third row element\n\n");
    printf("array[2][0]:%d \n",array[2][0]);
    printf("array[2][1]:%d \n",array[2][1]);
    printf("array[2][2]:%d \n",array[2][2]);
    printf("array[2][3]:%d \n",array[2][3]);
    printf("array[2][4]:%d \n\n",array[2][4]);
printf("print forth row element\n\n");
    printf("array[3][0]:%d \n",array[3][0]);
    printf("array[3][1]:%d \n",array[3][1]);
    printf("array[3][2]:%d \n",array[3][2]);
    printf("array[3][3]:%d \n",array[3][3]);
    printf("array[3][4]:%d \n",array[3][4]);
    getch();
    return 0;
}

 

When we executed above program, produces the following result

Output here

print first row element

array[0][0]:353
array[0][1]:684
array[0][2]:653
array[0][3]:897
array[0][4]:356

print second row element

array[1][0]:585
array[1][1]:764
array[1][2]:809
array[1][3]:357
array[1][4]:865

print third row element

array[2][0]:974
array[2][1]:267
array[2][2]:836
array[2][3]:163
array[2][4]:682

print forth row element

array[3][0]:974
array[3][1]:267
array[3][2]:836
array[3][3]:163
array[3][4]:682

 

Two dimension Array Access using for loop in C language

input element using for loop

for(i=0; i<=3; i++){
for(j=0; j<=4; j++){
scanf("%d",&array_name[i][j])
}
}

Display element using for loop

for(i=0; i<=3; i++){
for(j=0; j<=4; j++){
printf("%d",array_name[i][j])
}
}

Example

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int array[2][4]={{23,45,65,43},{54,87,76,32}};
    int i,j;
    for (i=0; i<2; i++){
        for(j=0; j<4; j++){
            printf("%d\n",array[i][j]);
        }
    }

    return 0;
}

When we executed above program, produces the following result

23
45
65
43
54
87
76
32

 

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int marks[3][4];
    int i,j;
    for (i=0; i<=2; i++){
        for(j=0; j<=3; j++){
                //ask input from user
            printf("Enter the value for marks[%d][%d] :",i,j);
        scanf("%d",&marks[i][j]);
        }
    }
printf("\n here display marks :\n");
    for (i=0; i<=2; i++){
        for(j=0; j<=3; j++){
            printf("marks[%d][%d] :%d\n",i,j,marks[i][j]);
        }
    }
    getch();
    return 0;
}

 

When we execute the above program, it produces the following result

Enter the value for marks[0][0] :45
Enter the value for marks[0][1] :67
Enter the value for marks[0][2] :98
Enter the value for marks[0][3] :64
Enter the value for marks[1][0] :79
Enter the value for marks[1][1] :94
Enter the value for marks[1][2] :36
Enter the value for marks[1][3] :73
Enter the value for marks[2][0] :26
Enter the value for marks[2][1] :60
Enter the value for marks[2][2] :48
Enter the value for marks[2][3] :37

 here display marks :
marks[0][0] :45
marks[0][1] :67
marks[0][2] :98
marks[0][3] :64
marks[1][0] :79
marks[1][1] :94
marks[1][2] :36
marks[1][3] :73
marks[2][0] :26
marks[2][1] :60
marks[2][2] :48
marks[2][3] :37

 

Two dimension Array Access using while loop in C language

input element using while loop

while(i<=2){
while(j<=2){
scanf("%d",&array_name[i][j])
}
}

 

Display element using while loop

while(i<=2){
while(j<=2){
printf("%d",&array_name[i][j])
}
}

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int marks[3][4]={
    {56,73,79,92},
    {39,95,72,47},
    {49,71,75,32},
    };
    int i=0;
    while(i<=2){
        int j=0;
        while(j<=3){
            printf("%d\n",marks[i][j]);
            j++;
        }
        i++;
    }
    printf("End the program\n");
    getch();
    return 0;
}

 

When we execute above program, it produces the following result

56
73
79
92
39
95
72
47
49
71
75
32
End the program

 

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int marks[3][4];
    int i=0;
    printf("Enter the values for marks\n");
                   while(i<=2){
                        int j=0;
                        while(j<=3){
                       scanf("%d",&marks[i][j]);
                            j++;
                        }
                    i++;
                    }
                   int k=0;
                    printf("Your marks here\n");
                   while(k<=2){
                        int l=0;
                        while(l<=3){
                        printf("%d\n",marks[k][l]);
                            l++;
                        }
                    k++;
                    }
                   printf("End the program\n");
                   getch();
    return 0;
    }

 

When we execute the above program, it produces the following the result

 

Enter the value for marks
56
67
78
89
90
98
87
76
46
73
42
37
Your marks here
56
67
78
89
90
98
87
76
46
73
42
37

 

Similar post

Two dim Array in C++ language

Three dim Array in C++ language

Single dim Array in Java language

Two dim Array in Java language

Three dim Array in Java language

Single dim Array in C language

Two dim Array in C language

Three dim Array in C language

 

Suggested for you

for loop in C++ language

While loop in C++ language

Nested for loop in C++ language

Nested while loop in C++ language

New keyword in Java language

For loop in Java language

While loop in Java language

Nested for loop in Java language

Nested while loop in Java language

 

Three dimension Array in C programming language
C language type of variables with example
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

10 best Ways to Subtract Two Numbers in Java (With Examples)

10 best Ways to Subtract Two Numbers in Java (With Examples) In this article, we…

4 weeks ago

Array Data Structure: Definition, Types, Operations & Advantages

Array Data Structure: Definition, Types, Operations & Advantages Array Data Structure Introduction In this post,…

1 month ago

20 ways to subtract two numbers in Java

20 ways to subtract two numbers in Java In this article, we will discuss the…

1 month ago

10 simple ways to add two numbers in Java

10 simple ways to add two numbers in Java In this article, we will discuss…

2 months ago

Write a Python program to find the first n prime numbers

Write a Python program to find the first n prime numbers In this article we…

3 months ago

Python: Calculate Average of odd and even in a list using loops

Python: Calculate Average of odd and even in a list using loops In this post,…

3 months ago