C Language | Arrays 11

Double Dimension Numeric Array

While declaring a double dimension array we have to specify two dimensions one for number of rows and second for number of columns.

Syntax:
Datatype arrayname[rows][cols];

int a[3][3];
int a[4][5];
int a[2][3];

int A[3][3];

 

Col 0

Col 1

Col 2

Row :0

A[0][0]

A[0][1]

A[0][2]

Row :1

A[1][0]

A[1][1]

A[1][2]

Row :2

A[2][0]

A[2][1]

A[2][2]

(logical representation of double dimension array)

Initialization of two dimensional array

(i)
int a[3][3]={1,2,3,4,5,6,7,8,9};
(ii)
int a[3][3]={
                     {1,2,3},
                     {4,5,6},
                     {7,8,9}
                 };

 

Col 0

Col 1

Col 2

Row :0

1

2

3

Row :1

4

5

6

Row :2

7

8

9

Examples:

Question:1.
C program to Take input for the elements of a double dimension array of dimension R X C. Further display the elements in the form of a matrix.
Sol:

#include<stdio.h>

int main()
{
    int a[10][10],r,c,i,j;
    /* clrscr(); */
    printf("Enter total rows and cols ");
    scanf("%d %d",&r,&c);
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("Enter [%d][%d] element ",i,j);
            scanf("%d",&a[i][j]);
        }
    }
  for(i=0;i<r;i++)
  {
        for(j=0;j<c;j++)
        {
            printf("%4d",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}
/* Output */

Enter total rows and cols 3
3
Enter [0][0] element 1
Enter [0][1] element 2
Enter [0][2] element 3
Enter [1][0] element 4
Enter [1][1] element 5
Enter [1][2] element 6
Enter [2][0] element 7
Enter [2][1] element 8
Enter [2][2] element 9

   1   2   3
   4   5   6
   7   8   9

Question:2.
C program to Take the input for the elements of a double dimension array of dimension R X C. Further display the elements in the form of a matrix. Also, calculate and print the sum of all the elements.

Example:
1 2 3
4 5 6
7 8 9
Sum = 45

Sol:

#include<stdio.h>

int main()
{
    int a[10][10],r,c,i,j,s=0;
    /* clrscr(); */
    printf("Enter total rows and cols ");
    scanf("%d %d",&r,&c);
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("Enter [%d][%d] element ",i,j);
            scanf("%d",&a[i][j]);
        }
    }
  for(i=0;i<r;i++)
  {
        for(j=0;j<c;j++)
        {
            printf("%4d",a[i][j]);
            s=s+a[i][j];
        }
        printf("\n");
    }
    printf("Sum of all the elements = %d\n",s);
    return 0;
}
/* Output */

Enter total rows and cols 3
3
Enter [0][0] element 1
Enter [0][1] element 2
Enter [0][2] element 3
Enter [1][0] element 4
Enter [1][1] element 5
Enter [1][2] element 6
Enter [2][0] element 7
Enter [2][1] element 8
Enter [2][2] element 9
   1   2   3
   4   5   6
   7   8   9
Sum of all the elements = 45

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects