0% found this document useful (0 votes)
3 views2 pages

C Code for Matrix Multiplication

The document discusses the implementation of matrix multiplication in C, specifically the function MatMul for multiplying two matrices A and B to produce C. It provides the correct inner loop code for the multiplication, which initializes C[i][j] to 0 and accumulates the product of corresponding elements from A and B. The document also offers to explain why other provided options are incorrect.

Uploaded by

study152003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

C Code for Matrix Multiplication

The document discusses the implementation of matrix multiplication in C, specifically the function MatMul for multiplying two matrices A and B to produce C. It provides the correct inner loop code for the multiplication, which initializes C[i][j] to 0 and accumulates the product of corresponding elements from A and B. The document also offers to explain why other provided options are incorrect.

Uploaded by

study152003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Matrix multiplication code

I can see the question clearly from your images. It’s about matrix multiplication
implementation in C.

The question is:

Consider the following code for multiplication of two matrices C = A × B.


MatMul(int *A, int *B, int *C, int nRowsA, int nColsA, int nRowsB, int
nColsB)

Inside, they’ve given:

int i, j, k;
for(i=0; i<nRowsA; i++)
for(j=0; j<nColsB; j++)
{
....
}

Now, we must fill the inner loop.

Correct inner code for matrix multiplication:


The formula is:

nColsA−1
C[i][j] = ∑ ​
A[i][k] × B[k][j]
k=0

So, the correct code is:

1/2
C[i][j] = 0;
for(k=0; k<nColsA; k++)
C[i][j] += A[i][k] * B[k][j];

✅ Answer:
The correct option among the given choices is the one that first initializes C[i][j] =
0; and then uses:

for(k=0; k<nColsA; k++)


C[i][j] += A[i][k] * B[k][j];

Would you like me to also explain why the other options are wrong (like if they miss
initialization or swap indices)?

2/2

You might also like