Knowee
Questions
Features
Study Tools

Given 2 matrices, find their product.Input FormatThe first line of input contains T - the number of test cases. The first line of each test case contains N1, M1 - the size of the 1st matrix. It is followed by N1 lines each containing M1 integers - elements of the 1st matrix. The next line contains N2, M2 - the size of the 2nd matrix. It is followed by N2 lines each containing M2 integers - elements of the 2nd matrix. Note that M1 = N2.Output FormatFor each test case, print the resultant product matrix, separated by a new line.Constraints1 <= T <= 1001 <= N1,M1,N2,M2 <= 50-100 <= mat[i][j] <= 100

Question

Given 2 matrices, find their product.Input FormatThe first line of input contains T - the number of test cases. The first line of each test case contains N1, M1 - the size of the 1st matrix. It is followed by N1 lines each containing M1 integers - elements of the 1st matrix. The next line contains N2, M2 - the size of the 2nd matrix. It is followed by N2 lines each containing M2 integers - elements of the 2nd matrix. Note that M1 = N2.Output FormatFor each test case, print the resultant product matrix, separated by a new line.Constraints1 <= T <= 1001 <= N1,M1,N2,M2 <= 50-100 <= mat[i][j] <= 100

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To solve this problem, you need to follow these steps:

  1. Read the number of test cases, T.
  2. For each test case, do the following: a. Read the size of the first matrix, N1 and M1. b. Create a 2D array (matrix1) of size N1 x M1 and fill it with the input values. c. Read the size of the second matrix, N2 and M2. d. Create a 2D array (matrix2) of size N2 x M2 and fill it with the input values.
  3. Check if the number of columns in the first matrix (M1) is equal to the number of rows in the second matrix (N2). If not, the matrices cannot be multiplied.
  4. If they can be multiplied, create a new 2D array (resultMatrix) of size N1 x M2.
  5. For each cell in the resultMatrix, calculate its value by multiplying the corresponding row in the first matrix by the corresponding column in the second matrix and adding the results.
  6. Print the resultMatrix.
  7. Repeat steps 2-6 for each test case.

Note: The multiplication of two matrices is done by taking each row from the first matrix and multiplying it element-wise with each column of the second matrix, then summing the results. This sum is the value of the cell in the resulting matrix at the corresponding row and column. If the number of columns in the first matrix is not equal to the number of rows in the second matrix, the matrices cannot be multiplied.

This problem has been solved

Similar Questions

Given 2 matrices A and B of size NxM. You have to find the sum between 1st row of matrix A and Nth row of matrix B, 2nd row of matrix A and (N-1)th row of matrix B and so on.Input FormatFirst line of input contains N,M - the size of the matrices. Its followed by 2xN lines, each containing M integers - elements of the matrices. First N lines for matrix A and the next N lines for matrix B.Output FormatPrint the sum of row 1 of A and row N of B, row 2 of A and row (N-1) of B and so on.Constraints1 <= N, M <= 100-102 <= mat[i][j] <= 102ExampleInput3 33 -2 51 3 44 0 72 2 4-8 4 37 1 9Output23719ExplanationSelf Expla

Given a 2D matrix of size NxN, print the sum of the elements of all its diagonals.Input FormatThe first line of input contains T - the number of test cases. The first line of each test case contains the N - the size of the matrix. Each of the next N lines contains N integers - the elements of the matrix.Output FormatFor each test case, print the sum of the elements of all the diagonals, separated by a new line. Refer to samples for more clarity.Constraints1 <= T <= 1001 <= N <= 100-100 <= ar[i][j] <= 100ExampleInput43-5 0 42 8 -63 7 11-425 -2-4 16-2 -3 -6 -5 50 38 7 10 -5 -3 306 3 70 9 -20 -7-9 9 -6 7 3 2-1 7 7 6 -4 38 5 6 -9 40 8Output4 -6 4 9 3-4-2 6 -43 80 -15 -29 22 86 51 13 4 4 8ExplanationTest Case 1Sum of the elements of the 1st diagonal: 4Sum of the elements of the 2nd diagonal: 0 + -6 = -6Sum of the elements of the 3rd diagonal: -5 + 8 + 1 = 4Sum of the elements of the 4th diagonal: 2 + 7 = 9Sum of the elements of the 5th diagonal: 3Test Case 2Sum of the elements of the 1st and only diagonal: -4Test Case 3Sum of the elements of the 1st diagonal: -2Sum of the elements of the 2nd diagonal: 5 + 1 = 6Sum of the elements of the 3rd diagonal: -4

Accept two square matrices A and B of dimensions n×n as input and compute their product AB.The first line of the input will contain the integer n. This is followed by 2n lines. Out of these, each of the first n lines is a sequence of comma-separated integers that denotes one row of the matrix A. Each of the last n lines is a sequence of comma-separated integers that denotes one row of the matrix B.Your output should again be a sequence of n lines, where each line is a sequence of comma-separated integers that denotes a row of the matrix AB.CODE WITH ALGORITHM# CODE WITH ALGORITHM# 1. Accept an integer 'n' as input.# 2. Initialize empty lists 'A' and 'B' to store matrices.# 3. Loop 'n' times to accept matrix A:#     3.1. Initialize an empty list 'row'.#     3.2. Split the input line by ',' and convert each element to an integer, adding it to 'row'.#     3.3. Append 'row' to matrix 'A'.# 4. Loop 'n' times to accept matrix B:#     4.1. Initialize an empty list 'row'.#     4.2. Split the input line by ',' and convert each element to an integer, adding it to 'row'.#     4.3. Append 'row' to matrix 'B'.# 5. initialize matrix 'C' as a zero-matrix:#     5.1. Loop 'n' times to initialize each element of 'C' to 0.# 6. [Perform matrix product:]#     6.1. Loop 'n' times to iterate through rows of 'A':#         6.1.1. Loop 'n' times to iterate through columns of 'B':#             6.1.1.1. Initialize a variable 'esum' to 0.#             6.1.1.2. Loop 'n' times to iterate through the elements of 'A' and 'B':#                 6.1.1.2.1. Add the product of corresponding elements from 'A' and 'B' to 'esum'.#             6.1.1.3. Update the corresponding element in 'C' with 'esum'.#             6.1.1.4. If it's not the last element in the row:#                 6.1.1.4.1. Print the element followed by a comma.#                 6.1.1.4.2. Else, print the last element in the row without a comma.Sample Test CasesDownload All Test Case 1InputExpected OutputActual Output21,23,45,67,819,2243,50Test Case 2InputExpected OutputActual Output31,0,00,1,00,0,12,3,45,9,110,12,132,3,45,9,110,12,13

Problem StatementHarpreet is a student learning about matrix multiplication. She wants to write a program to multiply two matrices, a and b, each of size N x N. Can you help her with the code?Write a program that takes an integer N as input and two matrices a and b of size N x N. Implement matrix multiplication and print the resulting matrix c.Note: A square matrix is where the number of rows equals the number of columns.Input format :The first line of input consists of an integer N, representing the matrix size.The next N lines consist of N space-separated elements in each line, representing the first matrix.After being separated by a new line, the next N lines consist of N space-separated elements in each line, representing the second matrix.Output format :The output prints the product of two matrices.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:2 ≤ N ≤ 80 ≤ elements ≤ 9Sample test cases :Input 1 :32 3 23 2 33 3 34 5 62 3 11 2 3Output 1 :16 23 21 19 27 29 21 30 30 Input 2 :22 22 35 07 8Output 2 :24 16 31 24 Input 3 :81 5 2 4 7 3 2 12 3 4 5 6 7 8 94 5 6 1 2 3 7 82 5 8 7 4 1 9 61 4 7 8 5 2 6 99 8 6 4 7 5 1 27 5 3 9 5 1 4 83 4 8 9 7 1 2 05 8 7 4 6 1 2 09 7 4 5 6 3 2 17 5 6 8 4 1 2 31 4 5 2 3 9 8 72 5 8 7 4 3 6 94 5 8 7 1 0 0 11 5 6 7 8 4 2 23 2 1 4 3 5 3 6Output 3 :99 131 152 141 106 88 97 115 145 200 236 248 183 155 137 181 155 177 179 207 172 108 88 113 157 201 217 233 202 166 146 169 149 186 206 223 179 176 157 194 204 243 255 231 185 110 128 136 152 208 212 197 188 177 164 179 136 178 206 184 146 133 148 159

Understand the problem statement from the given sample input and output.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line contains 2 strings A and B, separated by space, consisting only of lowercase English alphabets.Output FormatFor each test case, print the desired output, separated by a new line.Constraints10 points1 <= T <= 1001 <= len(A), len(B) <= 10040 points1 <= T <= 10001 <= len(A) <= 50001 <= len(A), len(B) <= 5000ExampleInput2data structuressmart interviewsOutputsrucuresineview

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.