In C programming, what does a standard matrix multiplication program compute when multiplying an m x n matrix A by an n x p matrix B?

Difficulty: Medium

Correct Answer: It produces an m x p result matrix C where each element C[i][j] is the sum over k from 0 to n - 1 of A[i][k] * B[k][j]

Explanation:


Introduction / Context:
Matrix multiplication is a standard problem in C programming labs and technical interviews. A correct understanding of how two matrices are multiplied is important in areas such as graphics, scientific computing, and machine learning. The key idea is that matrix multiplication is not performed element by element but instead uses dot products of rows and columns.


Given Data / Assumptions:

  • Matrix A has dimensions m x n.
  • Matrix B has dimensions n x p.
  • The product matrix C will have dimensions m x p.
  • Array indices in C code are typically zero based, so valid row indices are 0 to m - 1 and column indices are 0 to p - 1.


Concept / Approach:
To compute the matrix product C = A * B, each element C[i][j] in the result is obtained by taking the dot product of row i of A with column j of B. This means multiplying pairs of elements A[i][k] and B[k][j] for k from 0 to n - 1 and summing those products. The three nested loops commonly used in C reflect this: an outer loop over rows i, a middle loop over columns j, and an inner loop over k that performs the summation.


Step-by-Step Solution:
Step 1: Initialize all elements of C to zero.Step 2: For each row index i from 0 to m - 1, iterate through each column index j from 0 to p - 1.Step 3: For each pair (i, j), run an inner loop over k from 0 to n - 1.Step 4: Accumulate C[i][j] += A[i][k] * B[k][j] inside the inner loop.Step 5: After finishing the inner loop, C[i][j] holds the dot product of the i th row of A and the j th column of B.


Verification / Alternative check:
Consider A as a 2 x 3 matrix and B as a 3 x 1 matrix. The result C must be 2 x 1. For the single column j = 0, C[0][0] is A[0][0] * B[0][0] + A[0][1] * B[1][0] + A[0][2] * B[2][0]. This matches the formula in option A and corresponds to what a correct C program would compute.


Why Other Options Are Wrong:
Option B describes element wise multiplication (Hadamard product) and incorrectly fixes the result dimension.Option C claims the result is always square with size max(m, n, p), which does not match matrix algebra rules.Option D describes sorting values rather than performing matrix multiplication.


Common Pitfalls:
Students often confuse the dimensions of the result or attempt to multiply element by element. Another common error in C implementations is to swap indices, effectively computing the transpose or a different product.


Final Answer:
The correct description is It produces an m x p result matrix C where each element C[i][j] is the sum over k from 0 to n - 1 of A[i][k] * B[k][j].

More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion