To Perform Matrix Operations-Addition and Multiplication.
Algorithm
1) Start.
2) Read the order of the first matrix r1, c1.
3) Allocate matrix a[r1][c1].
4) Repeat step 5 for i=0 to r1.
5) Repeat step 6 for j=0 to c1.
6) Read a[i][j].
7) Read the order of the second matrix r2, c2.
8) Allocate matrix b[r2][c2].
9) Repeat step 10 for i=0 to r2.
10) Repeat step 11 for j=0 to c2.
11) Read b[i][j].
12) Display the menu.
13) Read the choice ch.
14) If ch=1 go to step 15 else go to step 19.
15) If r1=r2 and c1=c2, go to step 16 through 17 else go to step 31
16) Repeat step 17 for i=0 to r1.
17) Repeat step 18 for j=0 to c1.
18) c(i, j)=a (i, j)+b (i, j).
19) Repeat step 20 for i=0 to n.
20) Repeat step 21 for j=0 to n.
21) Display c[i][j].
22) If ch=2 go to step 23 else to step 31.
23) Repeat step 24 for i=0 to c2.
24) Repeat steps 25 through 30 for j=0 to r1.
25) Set C (i, j)=0, k=0.
26) Repeat step 27 for k=0 to c2.
27) c (i, j)=c (i, j)+a (i, k)*b (k, j).
28) Repeat step 29 for i=0 to n.
29) Repeat step 30 for j=0 to n.
30) Display c[i][j].
31) Stop.
Program
import java.io.DataInputStream; public class MatOper { public static void main(String[] args) { DataInputStream in= new DataInputStream(System.in); int a[][]=null; int b[][]=null; int c[][]=null; int i,j,k,r1,c1,r2,c2,ch; try { System.out.println("\nEnter the number of rows and columns of the 1st matrix :"); r1=Integer.parseInt(in.readLine()); c1=Integer.parseInt(in.readLine()); a=new int[r1][c1]; System.out.println("\nEnter the elements of the 2st matrix"); for(i=0;i<r1;i++) for(j=0;j<c1;j++) a[i][j]=Integer.parseInt(in.readLine()); System.out.println("\nEnter the number of rows and columns of the 2nd matrix :"); r2=Integer.parseInt(in.readLine()); c2=Integer.parseInt(in.readLine()); System.out.println("\nEnter the elements of the 2nd matrix"); b=new int[r2][c2]; for(i=0;i<r2;i++) for(j=0;j<c2;j++) b[i][j]=Integer.parseInt(in.readLine()); System.out.println("\nThe 1st matrix is:"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) System.out.print(a[i][j]+" "); System.out.print("\n"); } System.out.println("\nThe 2nd matrix is:"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) System.out.print(b[i][j]+" "); System.out.print("\n"); } System.out.println("\n1.Addition \n2.Multiplication"); System.out.println("\nEnter the operation you want to perform :"); ch=Integer.parseInt(in.readLine()); if(ch==1) { if(r1==r2&&c1==c2) { c=new int [r1][c1]; System.out.println("\nSum of the matrices :"); for(i=0;i<r1;i++) { System.out.print("\n"); for(j=0;j<c1;j++) { c[i][j]=a[i][j]+b[i][j]; System.out.print(c[i][j]+" "); } } } else System.out.println("Addition not possible!!!"); } else if(ch==2) { if(c1==r2) { c=new int [r1][c2]; System.out.println("Product of the matrices :"); for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { c[i][j]=0; for(k=0;k<c1;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; System.out.print(c[i][j]+" "); } System.out.print("\n"); } } else System.out.println("Multiplication not possible!!!!"); } else System.out.println("Invalid option!!!"); } catch(Exception e) { System.out.println("Error : "+e); } } } |
Output
Enter the number of rows and columns of the 1st matrix :
2
2
Enter the elements of the 2st matrix
1
2
3
4
Enter the number of rows and columns of the 2nd matrix :
2
2
Enter the elements of the 2nd matrix
1
2
3
4
The 1st matrix is:
1 2
3 4
The 2nd matrix is:
1 2
3 4
1.Addition
2.Multiplication
Enter the operation you want to perform :
1
Sum of the matrices :
2 4
6 8
Enter the number of rows and columns of the 1st matrix :
3
3
Enter the elements of the 2st matrix
1
2
3
4
5
6
7
8
9
Enter the number of rows and columns of the 2nd matrix :
3
3
Enter the elements of the 2nd matrix
9
8
7
6
5
4
3
2
1
The 1st matrix is:
1 2 3
4 5 6
7 8 9
The 2nd matrix is:
9 8 7
6 5 4
3 2 1
1.Addition
2.Multiplication
Enter the operation you want to perform :
2
Product of the matrices :
30 24 18
84 69 54
138 114 90