Introduction to Java Programming Comprehensive Version 10th
Introduction to Java Programming, Comprehensive Version (10th Edition)
Methods And Arrays
Three arrays are declared as int p[][] = {{23,19,42,11},{37,52,-11,-1}}; and int q[] = {{34,22,76,0}, {3,2,97,98}}; and int prod[2][4].
a. Write a method named productElements that takes int three two dimensional arrays and returns a two dimensional array.
b. Use nested loops to go through each element of the array and compute the prodcut. Do not hard code the upper limit for the number of rows or columns.
c. Call the productElements from within main and pass in the argument p and q and print out the elements return array prod with a nested loop
Solution
import java.util.Scanner;
public class Multiply
{
public static void main(String[] ss)
{
int r, c; //For row and column
Scanner s = new Scanner(System.in);
System.out.print(\"Enter number of rows in One: \");
int rowsA = s.nextInt();
System.out.print(\"Enter number of columns in One / rows in Two: \");
int columnsA = s.nextInt();
System.out.print(\"Enter number of columns in One: \");
int columnsB = s.nextInt();
//Dynamically allocates memmory to first and second matrix
int[][] first = new int[rowsA][columnsA];
int[][] second = new int[columnsA][columnsB];
System.out.println(\"Enter data for matrix A\");
for (r = 0; r < first.length; r++)
{
for (c = 0; c < first[0].length; c++)
{
first[r][c] = s.nextInt();
}
}
System.out.println(\"Enter data for matrix Two \");
for (r = 0; r < second.length; r++)
{
for (c = 0; c < second[0].length; c++)
{
second[r][c] = s.nextInt();
}
}
//Calls function productElements to multiply
int[][] third = productElements(first, second);
System.out.println(\"Product of One and Two is: \");
for (r = 0; r < third.length; r++)
{
for (c = 0; c < third[0].length; c++)
{
System.out.print(third[r][c] + \" \");
}
System.out.println();
}
}
public static int[][] productElements(int[][] f, int[][] s)
{
int r, c, tt;
int rowsF = f.length;
int columnsF = f[0].length; // Same as rows in second
int columnsS = s[0].length;
//Dynamically allocates memory to the temporary matrix
int[][] temp = new int[rowsF][columnsS];
//Multiply matrix
for (r = 0; r < rowsF; r++)
{
for (c = 0; c < columnsS; c++)
{
for (tt = 0; tt < columnsF; tt++)
{
temp[r][c] = temp[r][c] + f[r][tt] * s[tt][c];
}
}
}
return temp;
}
}
Output:
Enter number of rows in A: 3
Enter number of columns in A / rows in B: 2
Enter number of columns in One: 4
Enter data for matrix A
1
2
3
4
5
6
Enter data for matrix Two
1
2
3
4
5
6
7
8
Product of One and Two is:
11 14 17 20
23 30 37 44
35 46 57 68
![Introduction to Java Programming, Comprehensive Version (10th Edition) Methods And Arrays Three arrays are declared as int p[][] = {{23,19,42,11},{37,52,-11,-1} Introduction to Java Programming, Comprehensive Version (10th Edition) Methods And Arrays Three arrays are declared as int p[][] = {{23,19,42,11},{37,52,-11,-1}](/WebImages/9/introduction-to-java-programming-comprehensive-version-10th-999159-1761514519-0.webp)
![Introduction to Java Programming, Comprehensive Version (10th Edition) Methods And Arrays Three arrays are declared as int p[][] = {{23,19,42,11},{37,52,-11,-1} Introduction to Java Programming, Comprehensive Version (10th Edition) Methods And Arrays Three arrays are declared as int p[][] = {{23,19,42,11},{37,52,-11,-1}](/WebImages/9/introduction-to-java-programming-comprehensive-version-10th-999159-1761514519-1.webp)