Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a Matrix Calculator in C that performs matrix addition, subtraction, scal

ID: 3739058 • Letter: D

Question

Design a Matrix Calculator in C that performs matrix addition, subtraction, scalar multiplication and matrix multiplication using User Defined Functions. The user should be able to choose the operation they want to perform from a menu that loops. The User Defined Functions are given with each menu operation below. Matricies can be of any size, but your program needs to test for operation size errors. If the operation cannot be done, print an error message. Menu idea: MATRIX CALCULATOR Enter an operation: Associated User Defined Function 1- Add two matricies 2 - Subtract two matricies 3 - Scalar Multiply one matrix 4 - Multiple two matricies void matrixAddSub (int array1[10](10], int array2[10j[10), etc) uses same User Defined Function above void matriScalarMultiplyfint array1[10]/10J, etc) void matrixMultiply (int array1[10][10], int array2[10)[10], etc); (message if 1 to 4 not entered) You must also include User Defined functions for a readMatrix and a printMatrix void readMatrix(int array1[10][10], int rows, int columns) void printMatrix(int array1[10)][10], int rows, int columns) Test Cases: 1 3 -2 [5 2 1 [6 5-3 4 2 3 +3 27 3 5 15 443 23 82 [4 2 1]-[ 23Error Message: Size Error - cannot subtract. -2 4 4] (User should not have to enter 3 -3 4 -9 12 6 9 12 7 10 13 8 11 14 o 1 Error Message: Size Error - cannot multiply. 4 6 10] [1 46 753 27 5 93 42 92 70 60 102 9 0 11 31 0?

Explanation / Answer

import java.util.Scanner;

public class a {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int op = 0;

while(op != 4) {

System.out.println("1. Add two matrices 2. Substract two matrices 3. Scalar multiply one matrix 4. Multiply two matrics 5.Exit");

System.out.println("Enter your choice:");

op = Integer.parseInt(input.nextLine());

switch(op) {

case 1:

matrixAdd();

break;

case 2:

matrixSub();

break;

case 3:

break;

case 4:

matrixMultiply();

break;

case 5:

System.out.println("Bye Bye!");

System.exit(0);

break;

default:

System.out.println("Invalid choice!");

}

}

}

static void matrixAdd(){

int p, q, m, n;

Scanner s = new Scanner(System.in);

System.out.print("Enter number of rows in first matrix:");

p = s.nextInt();

System.out.print("Enter number of columns in first matrix:");

q = s.nextInt();

System.out.print("Enter number of rows in second matrix:");

m = s.nextInt();

System.out.print("Enter number of columns in second matrix:");

n = s.nextInt();

if (p == m && q == n)

{

int a[][] = new int[p][q];

int b[][] = new int[m][n];

int c[][] = new int[m][n];

System.out.println("Enter all the elements of first matrix:");

for (int i = 0; i < p; i++)

{

for (int j = 0; j < q; j++)

{

a[i][j] = s.nextInt();

}

}

System.out.println("Enter all the elements of second matrix:");

for (int i = 0; i < m; i++)

{

for (int j = 0; j < n; j++)

{

b[i][j] = s.nextInt();

}

}

System.out.println("First Matrix:");

for (int i = 0; i < p; i++)

{

for (int j = 0; j < q; j++)

{

System.out.print(a[i][j]+" ");

}

System.out.println("");

}

System.out.println("Second Matrix:");

for (int i = 0; i < m; i++)

{

for (int j = 0; j < n; j++)

{

System.out.print(b[i][j]+" ");

}

System.out.println("");

}

for (int i = 0; i < p; i++)

{

for (int j = 0; j < n; j++)

{

for (int k = 0; k < q; k++)

{

c[i][j] = a[i][j] + b[i][j];

}

}

}

System.out.println("Matrix after addition:");

for (int i = 0; i < p; i++)

{

for (int j = 0; j < n; j++)

{

System.out.print(c[i][j]+" ");

}

System.out.println("");

}

}

else

{

System.out.println("Addition would not be possible");

}

}

static void matrixSub(){

int p, q, m, n;

Scanner s = new Scanner(System.in);

System.out.print("Enter number of rows in first matrix:");

p = s.nextInt();

System.out.print("Enter number of columns in first matrix:");

q = s.nextInt();

System.out.print("Enter number of rows in second matrix:");

m = s.nextInt();

System.out.print("Enter number of columns in second matrix:");

n = s.nextInt();

if (p == m && q == n)

{

int a[][] = new int[p][q];

int b[][] = new int[m][n];

int c[][] = new int[m][n];

System.out.println("Enter all the elements of first matrix:");

for (int i = 0; i < p; i++)

{

for (int j = 0; j < q; j++)

{

a[i][j] = s.nextInt();

}

}

System.out.println("Enter all the elements of second matrix:");

for (int i = 0; i < m; i++)

{

for (int j = 0; j < n; j++)

{

b[i][j] = s.nextInt();

}

}

System.out.println("First Matrix:");

for (int i = 0; i < p; i++)

{

for (int j = 0; j < q; j++)

{

System.out.print(a[i][j]+" ");

}

System.out.println("");

}

System.out.println("Second Matrix:");

for (int i = 0; i < m; i++)

{

for (int j = 0; j < n; j++)

{

System.out.print(b[i][j]+" ");

}

System.out.println("");

}

for (int i = 0; i < p; i++)

{

for (int j = 0; j < n; j++)

{

for (int k = 0; k < q; k++)

{

c[i][j] = a[i][j] - b[i][j];

}

}

}

System.out.println("Matrix after subtraction:");

for (int i = 0; i < p; i++)

{

for (int j = 0; j < n; j++)

{

System.out.print(c[i][j]+" ");

}

System.out.println("");

}

}

else

{

System.out.println("subtraction would not be possible");

}

}

static void matrixMultiply(){

int n;

Scanner input = new Scanner(System.in);

System.out.println("Enter the base of squared matrices");

n = input.nextInt();

int[][] a = new int[n][n];

int[][] b = new int[n][n];

int[][] c = new int[n][n];

System.out.println("Enter the elements of 1st martix row wise ");

for (int i = 0; i < n; i++)

{

for (int j = 0; j < n; j++)

{

a[i][j] = input.nextInt();

}

}

System.out.println("Enter the elements of 2nd martix row wise ");

for (int i = 0; i < n; i++)

{

for (int j = 0; j < n; j++)

{

b[i][j] = input.nextInt();

}

}

System.out.println("Multiplying the matrices...");

for (int i = 0; i < n; i++)

{

for (int j = 0; j < n; j++)

{

for (int k = 0; k < n; k++)

{

c[i][j] = c[i][j] + a[i][k] * b[k][j];

}

}

}

System.out.println("The product is:");

for (int i = 0; i < n; i++)

{

for (int j = 0; j < n; j++)

{

System.out.print(c[i][j] + " ");

}

System.out.println();

}

input.close();

}

}