Lab 03 This lab gives you exercises with using two dimensional arrays of Java an
ID: 3888362 • Letter: L
Question
Lab 03
This lab gives you exercises with using two dimensional arrays of Java and practices with using methods that return arrays. What you should do is to download and complete the instructor’s program lab03.java:
1. Implement print2D(A) so that it prints out its 2D array argument A in the matrix form.
2. Implement add2Ds(A,B) so that it creates and returns a new 2D array C such that C=A+B.
3. Implement multiScalar2D(c,A) so that it creates and returns a new 2D array B such that B=c×A.
4. Implement transpose2D(A), which creates and returns a new 2D array B such that B is the transpose of A.
Your output should look like the following:
A =
1 2 3 4
5 6 7 8
9 10 11 12
B =
2 4 6 8
10 12 14 16
18 20 22 24
A + B =
3 6 9 12
15 18 21 24
27 30 33 36
5 X A =
5 10 15 20
25 30 35 40
45 50 55 60
Transpose of A =
1 5 9
2 6 10
3 7 11
4 8 12
Note that your methods should work for 2D arrays of any sizes.
------------------------------------------------------------
public class lab03 {
static void print2D(int[][]A) {
}
static int[][] add2Ds(int[][]A, int[][]B) {
}
static int[][] multiScalar2D(int scalar, int[][]A) {
}
static int[][] transpose2D(int[][]A) {
}
public static void main(String args[]) {
int A[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int B[][] = {{2, 4, 6, 8}, {10, 12, 14, 16}, {18, 20, 22, 24}};
System.out.println("Instructor's test program ");
System.out.println("A = ");
print2D(A);
System.out.println("B = ");
print2D(B);
System.out.println("A + B = ");
print2D(add2Ds(A,B));
System.out.println("5 X A = ");
print2D(multiScalar2D(5,A));
System.out.println("Transpose of A = ");
print2D(transpose2D(A));
}
}
Explanation / Answer
public class lab03 {
//function to print the value in array
static void print2D(int[][] A) {
//two loop to denote row and column
for (int[] A1 : A) {
for (int j = 0; j < A[0].length; j++) {
System.out.print(A1[j] + " ");
}
System.out.println();
}
}
// function to add to arrays of same size
static int[][] add2Ds(int[][] A, int[][] B) {
//to store the resultant array
int c[][] = new int[A.length][A[0].length];
//two loop to denote row and column
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);
}
//function to multiply scalar value with array values
static int[][] multiScalar2D(int scalar, int[][] A) {
//to store the resultant array
int c[][] = new int[A.length][A[0].length];
//two loop to denote row and column
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A[0].length; j++) {
c[i][j] = A[i][j] * scalar;
}
}
return (c);
}
static int[][] transpose2D(int[][] A) {
// to store the resultant array
int c[][] = new int[A[0].length][A.length];
//two loop to denote row and column
for (int i = 0; i < A[0].length; i++) {
for (int j = 0; j < A.length; j++) {
c[i][j] = A[j][i];
}
}
return (c);
}
public static void main(String[] args) {
int A[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int B[][] = {{2, 4, 6, 8}, {10, 12, 14, 16}, {18, 20, 22, 24}};
System.out.println("Instructor's test program ");
System.out.println("A = ");
print2D(A);
System.out.println("B = ");
print2D(B);
System.out.println("A + B = ");
print2D(add2Ds(A, B));
System.out.println("5 X A = ");
print2D(multiScalar2D(5, A));
System.out.println("Transpose of A = ");
print2D(transpose2D(A));
}
}
test#1
Instructor's test program
A =
1 2 3 4
5 6 7 8
9 10 11 12
B =
2 4 6 8
10 12 14 16
18 20 22 24
A + B =
3 6 9 12
15 18 21 24
27 30 33 36
5 X A =
5 10 15 20
25 30 35 40
45 50 55 60
Transpose of A =
1 5 9
2 6 10
3 7 11
4 8 12
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.