In this program , we have to input the elements in an 2D array or matrix . So , the no. of rows & columns are stored in m & n respectively . Then,we take inputs in matrix 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 matrix 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 matrix");
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 matrix : \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("\t a[%d][%d] = %d",i,j,a[i][j]);
}
printf("\n");
}
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 matrix 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 matrix");
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 matrix : \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("\t a[%d][%d] = %d",i,j,a[i][j]);
}
printf("\n");
}
getch();
}

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