In this program , we have to input the elements in an 2D array . So , the no. of rows & columns are stored in m & n respectively . Then,we take inputs in 2D array using 2 for loops which will store in following format :
a[0][0] , a[0][1] , a[0][2] , a[1][0],a[1][1] & so on i.e. row wise & using another 2 for loops 2d array is displayed .
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],i=0,j=0,m,n;
clrscr();
printf("\n program to input & display elements in 2D array");
printf("\n\n enter no. of rows : ");
scanf("%d",&m);
printf(" enter no. of column : ");
scanf("%d",&n);
printf("\n now enter them : \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n displaying them : \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("\n a[%d][%d] = %d",i,j,a[i][j]);
}
}
getch();
}
a[0][0] , a[0][1] , a[0][2] , a[1][0],a[1][1] & so on i.e. row wise & using another 2 for loops 2d array is displayed .
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],i=0,j=0,m,n;
clrscr();
printf("\n program to input & display elements in 2D array");
printf("\n\n enter no. of rows : ");
scanf("%d",&m);
printf(" enter no. of column : ");
scanf("%d",&n);
printf("\n now enter them : \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n displaying them : \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("\n a[%d][%d] = %d",i,j,a[i][j]);
}
}
getch();
}

No comments:
Post a Comment
Guys if you think something is wrong or should be edit than please do comment.