1) Sort All Arrays -Write a method sortAll() . sortAll() should take in an array
ID: 3703787 • Letter: 1
Question
1) Sort All Arrays
-Write a method sortAll(). sortAll() should take in an array of int arrays (an int[][]), which we will call myArrays, and sort each individual int[] within myArrays.
2)Matrix Sum
-Write a method called matrixSum(), which takes in two int[][] representing matrices and returns a new int[][] containing the sum of the matrices. Remember, you can only add two matrices together if they are the same size, so return null if they are different sizes.
3)Matric Product
-Write a method called matrixProduct(), that does matrix multiplication.
//This assignment is solved by using 2D arrays, please use java, and please complete all 3 assignments.
Explanation / Answer
1.
static int sortAll(int myArrays[][])
{
for (int r = 0; r < myArrays.length; r++)
{
for (int c = 0; c < myArrays[r].length; c++)
{
for (int i = 0; i < myArrays[r].length - c; i++)
{
if (myArrays[r][i] > myArrays[r][i + 1])
{
int tmp = myArrays[r][i];
myArrays[r][i] = myArrays[r][i + 1];
myArrays[r][i + 1] = tmp;
}
}
}
}
}
--------------------------------------------------
2.
public int[][] matrixSum(int a[][], int b[][])
{
int c[][]=new int[a.length][a[0].length];
int d[][];
if(a.length==b.length && a[0].length==b[0].length)
{
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
{
c[i][j]=a[i][j] + b[i][j];
}
}
return c;
}
else
return d;
}
--------------------------------------------------
3.
public int[][] matrixProduct(int a[], int b[][])
{
int c[][]=new int[a.length][b[0].length];
int d[][];
if(a[0].length==b.length)
{
for(int i = 0; i < a.length; i++)
{
for (int j = 0; j < b[0].length; j++)
{
for (int k = 0; k < a[0].length; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
else
return d;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.