2D – Array

Two-dimensional Array in C

A multidimensional array has more than one subscript. A two-dimensional array has two subscripts, a three-dimensional array has three subscripts, and so on. There is no limit to the number of dimensions a C array can have. (There is a limit on total array size, as discussed later in today’s lesson.) For example, you might write a program that plays checkers. The checkerboard contains 64 squares arranged in eight rows and eight columns. Your program could represent the board as a two-dimensional array, as follows:

2d-array-in-c-programming-language-androwep-tutorial
2d-array-in-c-programming-language-androwep

int checker[8][8];

Code Example


#include<stdio.h>
int main(){
   /* 2D array declaration*/
   int disp[2][3];
   /*Counter variables for the loop*/
   int i, j;
   for(i=0; i< 2; i++) {
      for(j=0;j< 3;j++) {
         printf("Enter value for disp[%d][%d]:", i, j);
         scanf("%d", &disp[i][j]);
      }
   }
   //Displaying array elements
   printf("Two Dimensional array elements:\n");
   for(i=0; i< 2; i++) {
      for(j=0;j< 3;j++) {
         printf("%d ", disp[i][j]);
         if(j==2){
            printf("\n");
         }
      }
   }
   return 0;
}

Output


  Enter value for disp[0][0]:1
  Enter value for disp[0][1]:2
  Enter value for disp[0][2]:3
  Enter value for disp[1][0]:4
  Enter value for disp[1][1]:5
  Enter value for disp[1][2]:6
  Two Dimensional array elements:
  1 2 3
  4 5 6

Code Example


#include<stdio.h>
int main(){
   /* 2D array declaration*/
   int abc[5][4];
   /*Counter variables for the loop*/
   int i, j;
   for(i=0; i< 5; i++) {
      for(j=0;j< 4;j++) {
         printf("Enter value for abc[%d][%d]:", i, j);
         scanf("%d", &abc[i][j]);
      }
   }
   return 0;
}

What is the use of two dimensional array?

The twodimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure.

What is a two dimensional array?

Twodimensional Arrays. … A 2D array has a type such as int[][] or String[][], with two pairs of square brackets. The elements of a 2D array are arranged in rows and columns, and the new operator for 2D arrays specifies both the number of rows and the number of columns. For example, int[][] A; A = new int[3][4];