(E) Dropbox Folders-Compute al-wi8.pdf × e chegg study | Guided Solut + v O |O /
ID: 3875916 • Letter: #
Question
(E) Dropbox Folders-Compute al-wi8.pdf × e chegg study | Guided Solut + v O |O //cuser file:///C./Users/uean1A Packages/Microsoft MicrosoftEdge 8wekyb3d8bbwe/TempState/Downloads/a1-w18.pdf Question 1. Write a claia that has the following methoda. Use neeted for loopa for all of these array operationa: iant I] I natialize the array by fillin it with values uteise 'tats.ro, d sns: sal between min and max a, sns nan, int "ax) public "eats.c int t] t] "uan l 11] t] , snt [ ] [] b} : compute the sum oftwo arrays and return a new array with thoae values. Remember, each value in array a gets added to the corresponding value in aray b. Asaume that the arrays are the same size public static void rerer se ant(1 [1 a) . Teverse all the values in the arTay public scatse inctt oy create a new array that is a value-by-value copy of the argument array. public naesro d pnnArraylintu u al : print the values stored in the array m your man { ) method prompt the user to enter two integers that will be the rows and columns for two array. Use your methods to initialize both arrays, to compute the sun and store it ia a new aray, to create a copy of one of your arrays and to reverse one of them Print out your results as you go along. Sample output should appear on D2L before too long but get started!Explanation / Answer
Note: I took minimum and maximum values as 1 and 50.Plz let me know if u need any changes in tis code.thank you.
______________
ArraysDemo.java
import java.util.Random;
import java.util.Scanner;
public class ArraysDemo {
public static void main(String[] args) {
//Declaring variables
int rows,cols;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
System.out.print("Enter no of Rows :");
rows=sc.nextInt();
System.out.print("Enter no of Columns :");
cols=sc.nextInt();
int A[][]=new int[rows][cols];
int B[][]=new int[rows][cols];
initialize(A,1,50);
initialize(B,1,50);
int res[][]=new int[rows][cols];
System.out.println("Displaying the Array A :");
printArray(A);
System.out.println("Displaying the Array B :");
printArray(B);
res=sum(A,B);
System.out.println("Displaying the Array after adding A and B matrices :");
printArray(res);
int copied[][]=new int[rows][cols];
copied=copy(A);
System.out.println("Displaying the Array Copied Array :");
printArray(copied);
}
private static int[][] copy(int[][] a) {
int res[][]=new int[a.length][a[0].length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
{
res[i][j]=a[i][j];
}
}
return res;
}
private static void printArray(int[][] a) {
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
private static int[][] sum(int[][] a, int[][] b) {
int res[][]=new int[a.length][a[0].length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
{
res[i][j]=a[i][j]+b[i][j];
}
}
return res;
}
private static void initialize(int[][] a, int min, int max) {
//Creating a random Class object
Random r = new Random();
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
{
a[i][j]=r.nextInt((max - min) + 1) + min;
}
}
}
}
____________________
Output:
Enter no of Rows :3
Enter no of Columns :3
Displaying the Array A :
44 37 2
45 5 39
24 6 10
Displaying the Array B :
5 19 35
6 46 44
19 2 9
Displaying the Array after adding A and B matrices :
49 56 37
51 51 83
43 8 19
Displaying the Array Copied Array :
44 37 2
45 5 39
24 6 10
________________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.