Program opened below. You can scroll through the algorithm, Java code and output.
Program 03
Matrix Product with Its Transpose
Question
Read a square matrix of order m and print the matrix product A × Aᵀ.
Algorithm
Step 1: Start and create Scanner.
Step 2: Read the order m of the square matrix.
Step 3: Reject an order outside the permitted practical range.
Step 4: Declare the original matrix A.
Step 5: Declare a result matrix with the same order.
Step 6: Read each element using nested row and column loops.
Step 7: Begin the outer loop for each result row.
Step 8: Begin a second loop for each result column.
Step 9: Initialize each result element to zero through the default array values.
Step 10: Run a third loop over the multiplication index k.
Step 11: Multiply A at row i, column k by A at row j, column k.
Step 12: Add each multiplication result to the current result element.
Step 13: Repeat until every result element has been calculated.
Step 14: Print the product matrix row by row.
Step 15: Stop execution.
Java program
Javaimport java.util.*;
class MatrixTransposeProduct {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
if (m < 1 || m > 10) {
System.out.println("Invalid order");
return;
}
int[][] a = new int[m][m];
int[][] product = new int[m][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = sc.nextInt();
}
}
// A-transpose[k][j] equals A[j][k].
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < m; k++) {
product[i][j] += a[i][k] * a[j][k];
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
System.out.print(product[i][j] + " ");
}
System.out.println();
}
}
}Sample output
Input: 2 / 1 2 / 3 4
Output: 5 11 / 11 25