0% found this document useful (0 votes)
63 views

Matrix - Algebra JAVA ISC

The document describes an algorithm and Java program to perform matrix operations (multiplication, addition, subtraction) on two user-input matrices. It takes input for two matrices, performs the selected operation based on user choice, and displays the original and resultant matrices. The key steps are: 1) Taking input for two matrices, 2) Performing the selected operation of multiplication, addition, or subtraction, 3) Displaying the original and resultant matrices. It uses methods like input(), display_original(), Mult(), add(), diff() to implement the steps.

Uploaded by

Ankit Talukder
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Matrix - Algebra JAVA ISC

The document describes an algorithm and Java program to perform matrix operations (multiplication, addition, subtraction) on two user-input matrices. It takes input for two matrices, performs the selected operation based on user choice, and displays the original and resultant matrices. The key steps are: 1) Taking input for two matrices, 2) Performing the selected operation of multiplication, addition, or subtraction, 3) Displaying the original and resultant matrices. It uses methods like input(), display_original(), Mult(), add(), diff() to implement the steps.

Uploaded by

Ankit Talukder
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

QUESTION-16:

Write a menu driven java program to find the sum, difference or product of two matrices
and displaying the resultant matrix.
ALGORITHM:
1. Start
2. Create class variables: m ,n and mm,nn to store columns and rows numbers of matrices a[][]
and arr[][] respectively.
3. Create matrices: a[][], arr[][], prod[][], sum[][], sum[][] and Sub[][].
4. Create static object sc of scanner class of java.util package to take user inputs.
5. Create a method void create().
6. Take input of column and row numbers of matrices a and arr from user.
7. Store the inputs in m,n and mm,nn respectively.
8. Declare the matrices a[][] and arr[][] with column and row numbers m,n and mm,nn.
9. Create method input() to take input of the elements of the matrix using scanner class.
10. To take input for matrix a[][] run a loop from int i=0 to i<m , incrementing i by 1 with a nested
loop running from j=0 to j<n, incrementing j by 1.
11. To take input for matrix arr[][] run a loop from int i=0 to i<mm , incrementing i by 1 with a
nested loop running from j=0 to j<nn, incrementing j by 1.
12. Create a method print_display with parameters as int m[][],int r and int c.
13. Print the matrix m[][] by running a loop from int i=0 to i<r ,incrementing i by 1 with a nested
loop running from j=0 to j<c, incrementing j by 1.
14. Create method display_original to display the original matrices.
15. Call the method print_display() and pass a,m,n as the arguments to print the first matrix.
16. Call the method print_display() and pass arr,mm,nn as the arguments to print the second
matrix.
17. Create method Mult() to multiply two matrices.
18. Declare matrix prod[][] with rows as m and columns as nn.
19. Run a for loop from i=0 to i<m and incrementing i by 1.
20. Run a nested for loop from j=0 to j<nn, increment j by 1.
21. Run a nested for loop from k=0 to k<mm, increment k by 1.
22. Store a[i][k]*arr[k][j] in prod[i][j].
23. To print the matrix prod[][] call the method print_display() and pass prod,m,nn as parameters.
24. Create method add() to add two matrices.
25. Declare sum[][] with m as rows and n as columns.
26. Run a for loop from i=0 to i<m and incrementing i by 1.
27. Run a for loop from j=0 to j<n and incrementing j by 1.
28. Store a[i][j] + arr[i]j] in sum[i][j].
29. To print the matrix sum[][] call the method print_display() and pass sum,m,n as parameters.
30. Create method diff() to subtract two matrices.
31. Declare Sub[][] with m as rows and n as columns.
32. Run a for loop from i=0 to i<m and incrementing i by 1.
33. Run a for loop from j=0 to j<n and incrementing j by 1.
34. Store a[i][j] - arr[i]j] in Sub[i][j].
35. To print the matrix Sub[][] call the method print_display() and pass Sub,m,n as parameters.
36. Create main method.
Page | 1
37. Create object ob of the class.
38. Give choices to the user:
1. Enter (1) for Multiplication
2. Enter (2) for Addition
3. Enter (3) for Subtraction
39. Store the choice of user in int ch.
40. If user enters 1, then call the method create() using object ob.
41. Check if n==mm.
42. If true, then call methods: input(),display_original(),Mult(), using the object ob.
43. If false, then print an output: Multiplicaton not possible.
44. If user enters 2, then call the method create() using object ob.
45. Check if m=mm and n=nn.
46. If true, then call methods: input(),display_original(),add(), using the object ob.
47. If false, then print an output: Addition not possible.
48. If user enters 3, then call the method create() using object ob.
49. Check if m=mm and n=nn.
50. If true, then call methods: input(),display_original(),diff(), using the object ob.
51. If false, then print an output: Subtraction not possible.
52. If the user input is anything else then print : Invalid Choice.
53. Stop.

SOURCE CODE:
import java.util.*;
public class Matrix_algebra
{
static int m,n,mm,nn;
int a[][];
int arr[][];
int prod[][];
int sum[][];
int Sub[][];

static Scanner sc=new Scanner(System.in);


void create(){
System.out.println("Enter the number of rows and columns of the first matrix:");
m=sc.nextInt();
n=sc.nextInt();
a=new int[m][n];
System.out.println("Enter the number of rows and columns of the second matrix:");
Page | 2
mm=sc.nextInt();
nn=sc.nextInt();
arr=new int[mm][nn];
}
void input(){
System.out.println("Enter the elements of the first matrix:");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
a[i][j]=sc.nextInt();
}
}
System.out.println("Enter the elements of the second matrix:");
for(int i=0;i<mm;i++){
for(int j=0;j<nn;j++){
arr[i][j]=sc.nextInt();
}
}
}
void print_display(int m[][],int r,int c){
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
System.out.print(m[i][j]+" ");
}
System.out.println();
}
}
void display_original(){
System.out.println("First matrix:");
print_display(a,m,n);
System.out.println("Second matrix:");
print_display(arr,mm,nn);
}
Page | 3
void Mult(){
prod = new int[m][nn];
for (int i = 0; i < m; i++) {
for (int j = 0; j < nn; j++) {
for (int k = 0; k < mm; k++)
prod[i][j] = a[i][k] * arr[k][j];
}
}
System.out.println("Product Matrix:");
print_display(prod, m, nn);
}
void add(){
sum=new int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
sum[i][j] = a[i][j] + arr[i][j];
System.out.println("Sum:");
print_display(sum,m,n);
}
void diff(){
Sub =new int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
Sub[i][j] = a[i][j] - arr[i][j];
System.out.println("Difference:");
print_display(Sub,m,n);
}
public static void main(String[] args) {
Matrix_algebra ob=new Matrix_algebra();
System.out.println("Enter (1) for Multiplication:");
System.out.println("Enter (2) for Addition:");
System.out.println("Enter (3) for Subtraction:");
Page | 4
int ch=sc.nextInt();
switch(ch){
case 1:
ob.create();
if(n==mm) {
ob.input();
ob.display_original();
ob.Mult();
}
else System.out.println("Multiplication not possible");
break;
case 2:
ob.create();
if(m==mm && n==nn) {
ob.input();
ob.display_original();
ob.add();
}
else System.out.println("Addition not possible");
break;
case 3:
ob.create();
if(m==mm && n==nn) {
ob.input();
ob.display_original();
ob.diff();
}
else System.out.println("Subtraction not possible");
break;
default:
System.out.println("Invalid Choice!");
}
Page | 5
}
}

VARIABLE DESCRIPTION TABLE:


SL. VARIABLE
DATA TYPE DESCRIPTION OF THE VARIABLES
NO. NAME
1. m int Number of rows of a[][]

2. n int Number of columns of a[][]

3. mm int Number of rows of arr[][]

4. nn int Number of columns of arr[][]

5. a[][] int First matrix containing input by user

6. arr[][] int Second matrix containing input by user

7. prod[][] int Contains product of a[][] and arr[][]

8. sum[][] int Contains sum of a[][] and arr[][]

9. Sub[][] int Contains difference between a[][] and arr[][]

10. i int Iteration Variable

11. j int Iteration Variable

12. k int Iteration Variable

13. ob Object Object of the class

14. sc Object Object of scanner class

Page | 6
OUTPUT:

Page | 7
Page | 8
Page | 9
Page | 10

You might also like