Product of Matrix and it's Transpose Program In Python
Generate a 4x4 matrix following specific rules and compute the element-wise product of the matrix with its transpose
Understand the Problem
Problem Statement
The program must accept two positive integers M and N as the input. The program must generate a 4×4 integer matrix by using the following conditions,
– The first element of the matrix must be the value of M.
– The successive elements are obtained by adding N to the current element.
Finally, the program must print the product of the generated matrix and it's transpose.
Note: For the matrix product, the elements in the same positions are multiplied to get the resultant matrix.
Constraints
- 1 ≤ M ≤ 100
- 1 ≤ N ≤ 100
- Matrix size is fixed at 4×4
- All elements must be positive integers
- Elements are generated sequentially by adding N
Examples
4 116 40 72 112
40 81 130 187
72 130 196 270
112 187 270 361Starting with 4 and incrementing by 1, the generated matrix is: 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Its transpose is: 4 8 12 16 5 9 13 17 6 10 14 18 7 11 15 19 The element-wise product gives the output matrix.
3 59 184 559 1134
184 784 1584 2584
559 1584 2809 4234
1134 2584 4234 6084Starting with 3 and incrementing by 5, the generated matrix is: 3 8 13 18 23 28 33 38 43 48 53 58 63 68 73 78 The element-wise product with its transpose produces the given output.
Solution
#include <stdio.h>
int main() {
int M, N;
scanf("%d %d", &M, &N);
// Generate 4x4 matrix
int matrix[4][4];
int current = M;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
matrix[i][j] = current;
current += N;
}
}
// Create transpose and compute element-wise product
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
// Transpose element at [i][j] is matrix[j][i]
// Product is matrix[i][j] * matrix[j][i]
printf("%d ", matrix[i][j] * matrix[j][i]);
}
printf("\n");
}
return 0;
}Step-by-step explanation:
- Read M and N from standard input
- Create a 4×4 integer array to store the matrix
- Initialize current value to M
- Nested loop to fill matrix: outer loop for rows, inner loop for columns
- Each element gets current value, then current is incremented by N
- Second nested loop computes element-wise product:
- For position [i][j], multiply matrix[i][j] with matrix[j][i] (transpose element)
- Print each row on separate lines with space-separated values