Question

In: Computer Science

Java. Write 3 methods named addArrays, subArrays, and mulArrays each. addArrays gets two matrices(int [][] array1,...

Java.

Write 3 methods named addArrays, subArrays, and mulArrays each.

addArrays gets two matrices(int [][] array1, int [][] array2) as parameters and add them and returns the result (matrix).

subArrays gets two matrices(int [][] array1, int [][] array2) as parameters and subtract array2 from array1 and returns the result (matrix).

mulArrays gets two matrices(int [][] array3, int [][] array4) as parameters, multiplying them and returning the result(matrix).

Solutions

Expert Solution

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;


/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static int [][]addArrays(int [][]arr1, int [][]arr2,int rows,int col)
{
int [][]c = new int[rows][col];
for(int i = 0; i < rows ; i++)
for(int j = 0; j < col; j++)
{
c[i][j] = arr1[i][j] + arr2[i][j];
}
return c;
}
public static int [][]subArrays(int [][]arr1, int [][]arr2,int rows,int col)
{
int [][]c = new int[rows][col];
for(int i = 0; i < rows ; i++)
for(int j = 0; j < col; j++)
{
c[i][j] = arr1[i][j] - arr2[i][j];
}
return c;
}
//m is rows of first matrix, p is rows and q is col of second matrix
public static int [][]mulArrays(int [][]arr1, int [][]arr2,int m,int p,int q)
{
int [][]multiply = new int[m][q];
int sum = 0;
for(int c=0; c<m; c++)
{
for(int d=0; d<q; d++)
{   
for(int k=0; k<p; k++)
{
sum = sum + arr1[c][k]*arr2[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}
return multiply;
}

   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
   Scanner in = new Scanner(System.in);
   System.out.print("Enter Number of Rows and Columns of First Matrix : ");
int m = in.nextInt();
int n = in.nextInt();
  
int arr1[][] = new int[m][n];
  
System.out.print("Enter First Matrix Elements : ");
  
for(int c=0 ; c<m; c++)
{
for(int d=0; d<n; d++)
{
arr1[c][d] = in.nextInt();
}
}
System.out.print("Enter Number of Rows and Columns of Second Matrix : ");
int p = in.nextInt();
int q = in.nextInt();

int arr2[][] = new int[p][q];
  
System.out.print("Enter Second Matrix Elements :\n");

for(int c=0; c<p; c++)
{
for(int d=0; d<q; d++)
{
arr2[c][d] = in.nextInt();
}
}

//check matrix addition
  
if(m == p && n == q)
{
int add[][] = new int[p][q];
int sub[][] = new int[p][q];
add = addArrays(arr1,arr2,m,n);
System.out.print("Addition of two Matrices is :\n");

for(int c=0; c<m; c++)
{
for(int d=0; d<n; d++)
{
System.out.print(add[c][d] + "\t");
}
System.out.print("\n");
}
System.out.print("Subtraction of two Matrices is :\n");
sub = subArrays(arr1,arr2,m,n);
for(int c=0; c<m; c++)
{
for(int d=0; d<n; d++)
{
System.out.print(sub[c][d] + "\t");
}
System.out.print("\n");
}
}
else
System.out.print("two Matrices can not be added or subtracted\n");
if ( n != p )
{
System.out.print("Matrix of the entered order can't be Multiplied..!!");
}
else
{
int mul[][] = new int[p][q];
mul = mulArrays(arr1,arr2,m,p,q);
System.out.print("Multiplication of two Matrices is :\n");

for(int c=0; c<m; c++)
{
for(int d=0; d<q; d++)
{
System.out.print(mul[c][d] + "\t");
}
System.out.print("\n");
}
}
  

   }
  

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

//input:

2 3

1        2        3       

7        8        9

2 3

5        6        7       

3        4        5

//output

Addition of two Matrices is :

6        8        10     

10      12      14     

Subtraction of two Matrices is :

-4      -4      -4     

4        4        4       

//input for multiplication

2 3

1 2 3

4 5 6

3 2

7 8

9 10

11 12

//output

Multiplication of two Matrices is :

58      64     

139    154   


Related Solutions

Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets...
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets two file names and write the content of the first file (sourceFile) into the beginning of the second file (destinationFile. For example, if sourceFile contains “Hello ” and destinationFile contains “World!”, this method must keep sourceFile as it is, but replace the content of the second file by “Hello World!”.public static voidMergeFiles(String sourceFile, String destinationFile) b. A recursive method with the following signature that...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
1. Given the following array named 'array1': array1 DWORD 50h, 51h, 52h, 53h Write instructions to...
1. Given the following array named 'array1': array1 DWORD 50h, 51h, 52h, 53h Write instructions to swap the items in array1 (using only MOV and XCHG instruction), so that the array1 become: 53h, 51h, 50h, 52h ( That means the first item in the array is 53h, second item is 51h, third item is 50h, forth item is 52h). You can use registers to perform the MOV and XCHG operation.   Write the code to perform above mentioned transformation.
Given the following array named 'array1': array1 DWORD 50h, 51h, 52h, 53h Write instructions to swap...
Given the following array named 'array1': array1 DWORD 50h, 51h, 52h, 53h Write instructions to swap the items in array1 (using only MOV and XCHG instruction), so that the array1 become: 53h, 51h, 50h, 52h ( That means the first item in the array is 53h, second item is 51h, third item is 50h, forth item is 52h). You can use registers to perform the MOV and XCHG operation.   Write the code to perform above mentioned transformation.
in java Write a class named “Stock” to model a stock. The properties and methods of...
in java Write a class named “Stock” to model a stock. The properties and methods of the class are shown in figure below. The method “ percentage ” computes the percentage of the change of the current price vs the previous closing price. Write a program to test the “Stock” class. In the program, create a Stock object with the stock symbol SUND, name Sun Microsystem V, previous closing price of 100. Set a new current price randomly and display...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the matrices are same or 0 otherwise. Set appropriate parameters and return type if necessary.
Write a program that contains the following Write a function copies with two int parameters, named...
Write a program that contains the following Write a function copies with two int parameters, named n and x. It should dynamically allocate a new array of size n.  The function should then copy the value in x to every position in the array. The function should return a pointer to the new array. In the main function, call the copies function from part 1. with size 5 and value -1.  Output the resulting array to the screen, and deallocate the array....
Write a java program calls the following methods: a. printStars(): Takes an int (n) as parameter...
Write a java program calls the following methods: a. printStars(): Takes an int (n) as parameter and prints n stars (*) using for loop. ex. 6 ******
write a program named Combinations.java that gets user input for two integers n and k, then...
write a program named Combinations.java that gets user input for two integers n and k, then computes and displays the value of n choose k using the efficient method of equation. you may assume that the user's input makes sense (i.e, n and k are non negative, and n is at least k). your code should work for any reasonably small non- negative values of n and k, including zero.
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT