Skip to main content
To Get All Information of Sarkari Jobs | Results | Admit Card | And courses ,Internships then CLICK HERE

Two-Dimensional Array-:

Declaration of Two-Dimensional Array-:
Before using an array in program,it is must to declare it,An array, which has two subscript is known as two dimensional array.It has two subscripts, first one represents row and second one represents column. Subscripts are always starts with 0,The syntax to declare 2-d array is as follow-:
Syntax-:
data type array name[size1][size2];
Example-:
    int score[4][5];
     Each memory location will occupy 2 byte,because its data type is integer.
                            ads   
  Storage of Values in 2-d Array-:
In an array values can be stored by below two methods-:
  1. Initialization method.
  2. Input method.
Initialization Method-:
In this method values to be stored in an array are provided at the time,when the array is declared.If the values to be stored in array are known to programmer than this method is best to initialize array.
Syntax-: 
data type   array name[size 1][size 2]= {value 1,value 2,value 3,------value n};
Example-:
int num[3][5]={5,12,17,9,3,13,4,8,14,1,9,6,3,7,21};


While initializing an 2-darray it is optional to provide the number of rows.In such as case, the size of array is determine by the number of value initialized.
If the number of values initialized are less than the size of array then the remaining location are automatically filled with zero.

Input Method-:
 When the values of array are stored at run-time,then the input method is used in 2-d array.
Syntax-: 
for(i=0;i<no. of rows;i++)
 {for(j=0;j<no. of column;j++)
{printf("\n Enter array element ");
  scanf("format specifier",&array name[i]);
} }
Example-:
 int num[10][5];
for(i=0;i<10,i++)
for(j=0;j<5,j++)
{printf("\n Enter array element %d ");
  scanf("%d",&num[i][j]);
}}
How to print value stored in 2-d Array?
The following syntax is used to print value stored in 1-d array-:
Syntax-: 
for(i=0;i<no. of rows;i++)
 {for(j=0;j<no. of column;j++)
  printf("format specifier",&array name[i]);
Example-:
int num[10][5];
for(i=0;i<10,i++)
for(j=0;j<5,j++)
  printf("%d",&num[i][j]);
}
Q- Write a program to print a initialized two dimensional array.
#include <stdio.h>
int main()
{
int matrix[3][3] = {{8,9,6}, {3.2.1}, {7, 8, 9}}, i, j;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("%2d ", matrix[i] [j]);
}
printf("\n");
}
return 0;
}
Output-:
8 9 6
3 2 1
7 8 9
 Definition and Introduction Of Array CLICK HERE
 About One-Dimensional Array CLICK HERE
      Watch The Below  Videos

ads






Comments

Post a Comment

Please do not enter any spam link in the comment box.


Download The Book "Let Us C" By Yashavant P.Kanetkar,Click The Below Button

-->Email Subscription

Enter Your Email Address:-

Delivered by FeedBurner

Copyright © 2020 TechProgramiz|All Right Reserved|Made By-: Sudarshan Pandey