codescracker
c

C Program for Two Dimensional Array



« Previous Program Next Program »

In this article, you will learn and get code about some programs that uses two-dimensional (2D) array. For example,

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

is a two dimensional array. The name of array is arr[][] having 4 pairs of 2-2 elements. That is, the first size tells the total number of pair, and the second size, tells that how many element(s) goes to store in each pair. All the numbers from 1 to 8 gets stored in the array arr[][] in a way that

You can also think it like the elements of two dimensional array are placed in the form of rows and columns. That is, the first index shows rows, whereas second one shows columns. So from the above statement where the array arr[][] is declared and defined, shows that, there are 4 rows and 2 columns. Now let's move on to the program given below.

Two Dimensional Array Program in C

This is the simplest C program that uses two dimensional array. Here the elements of two-dimensional array gets initialized automatically while running the program. Means that, it does not ask from user to help for anything like defining the size or entering some elements for 2D array. Let's have a look at the program.

// Two Dimensional Array Program in C
// ---codescracker.com---

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[4][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
    int i, j;
    printf("The Two-dimensional Array is:\n");
    for(i=0;  i<4; i++)
    {
        for(j=0; j<2; j++)
            printf("%d ", arr[i][j]);
        printf("\n");
    }
    getch();
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is the output produced by above program:

two dimensional array in c

Because the array size is defined as arr[4][2]. That means, we have 4 pairs. And in each pair there are 2 elements. So here we have first created a for loop that runs 4 times, and then inside it, we have created another for loop that runs 2 times for every time when program flow enters into this loop after coming from outer loop. In this way, the statement,

printf("%d ", arr[i][j]);

executes total of 8 times with its index value starts from arr[0][0] to arr[3][1]. Let's verify it using the program given below.

You can also modify the above program with the program given below, to print the elements of array along with its indexing. That is, which element stored at what index. Here is the program:

// 2D Array Program in C
// ---codescracker.com---

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[4][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
    int i, j;
    printf("The Two-dimensional Array is:\n");
    for(i=0;  i<4; i++)
    {
        for(j=0; j<2; j++)
            printf("arr[%d][%d] = %d\t", i, j, arr[i][j]);
        printf("\n");
    }
    getch();
    return 0;
}

and here is its output:

c program two dimensional array

Allow User to Define Array Size

Let's take another program where user is allowed to define the size of two dimensional array and then enters its element of given size. This is the last program on 2D dimensional array that gives you the complete understanding about it:

// C Two Dimensional Array Program
// ---codescracker.com---

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[10][10], row, col, i, j;
    printf("Enter Row Size of Array (max. 10): ");
    scanf("%d", &row);
    printf("Enter Column Size of Array (max. 10): ");
    scanf("%d", &col);
    printf("\nEnter %d Array Elements: ", row*col);
    for(i=0; i<row; i++)
    {
        for(j=0; j<col; j++)
            scanf("%d", &arr[i][j]);
    }
    printf("\nThe Array is:\n");
    for(i=0; i<row; i++)
    {
        for(j=0; j<col; j++)
            printf("%d ", arr[i][j]);
        printf("\n");
    }
    getch();
    return 0;
}

The snapshot given below shows the sample run of the program that are given just above this sentence:

two dimensional array program in c

To learn more about array in c, refer to Array in C tutorial.

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker