Two Dimensional Array Programs
| Program to create an array of 3 X 3. Enter values and display it. | ||||||
| #include<stdio.h> #include<conio.h> void main() { int a[3][3],i,j; clrscr(); printf("Enter Elements of Array:\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter Value of a[%d][%d]:",i,j); scanf("%d",&a[i][j]); } } printf("Elements of Array Are:\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d ",a[i][j]); } printf("\n"); } getch(); } Output Enter Elements of Array: Enter Value of a[0][0]:10 Enter Value of a[0][1]:20 Enter Value of a[0][2]:30 Enter Value of a[1][0]:40 Enter Value of a[1][1]:50 Enter Value of a[1][2]:60 Enter Value of a[2][0]:70 Enter Value of a[2][1]:80 Enter Value of a[2][2]:90 Elements of Array Are: 10 20 30 40 50 60 70 80 90 |
||||||
| Program to add two 3 X 3 matrices. | ||||||
| #include<stdio.h> #include<conio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; clrscr(); printf("Enter Elements of Array A:\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("enter value of a[%d][%d]:",i,j); scanf("%d",&a[i][j]); } } printf("Enter Elements of Array B:\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("enter value of b[%d][%d]:",i,j); scanf("%d",&b[i][j]); } } printf("Elements of Array C Are:\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j] = a[i][j] + b[i][j]; printf("%d ",c[i][j]); } printf("\n"); } getch(); } Output Enter Elements of Array A: Enter Value of a[0][0]:1 Enter Value of a[0][1]:1 Enter Value of a[0][2]:1 Enter Value of a[1][0]:1 Enter Value of a[1][1]:1 Enter Value of a[1][2]:1 Enter Value of a[2][0]:1 Enter Value of a[2][1]:1 Enter Value of a[2][2]:1 Enter Elements of Array B: Enter Value of b[0][0]:2 Enter Value of b[0][1]:2 Enter Value of b[0][2]:2 Enter Value of b[1][0]:2 Enter Value of b[1][1]:2 Enter Value of b[1][2]:2 Enter Value of b[2][0]:2 Enter Value of b[2][1]:2 Enter Value of b[2][2]:2 Elements of Array C Are: 3 3 3 3 3 3 3 3 3 |
||||||
| Program to multiply two 3 X 3 matrices. | ||||||
| #include<stdio.h> #include<conio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j,k; clrscr(); printf(“Enter elements of A:”); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) scanf(“%d”,&a[i][j]); } printf(“Enter elements of B:”); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) scanf(“%d”,&b[i][j]); } k=0; while (k<=2) { for(i=0;i<=2;i++) { int sum=0; for(j=0;j<=2;j++) sum=sum+a[i][j]*b[j][k]; c[i][k]=sum; } k++; } printf(“Elements of array C\n “); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) printf(“%d “,c[i][j]); printf(“\n”); } getch(); } Output Enter Elements of array A 1 1 1 1 1 1 1 1 1 Enter Elements of array B 2 2 2 2 2 2 2 2 2 Elements of Array C 6 6 6 6 6 6 6 6 6 |
||||||
| Program to find maximum number from 2-D array. | ||||||
| #include<stdio.h> #include<conio.h> void main() { int a[3][3],i,j,max; clrscr(); printf("Enter Elements of Array :\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("enter value of a[%d][%d]:",i,j); scanf("%d",&a[i][j]); } } max=a[0][0]; for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(a[i][j]>max) max=a[i][j]; } } printf("max=%d",max); getch(); } Output Enter Elements of Array: Enter Value of a[0]:1 Enter Value of a[1]:2 Enter Value of a[2]:3 Enter Value of a[3]:4 Enter Value of a[4]:5 Enter Value of a[5]:6 Enter Value of a[6]:7 Enter Value of a[7]:8 Enter Value of a[8]:9 Enter Value of a[9]:10 max=10 |
||||||