Mike A.

asked • 02/08/21

Matrix Multiplication in C programming

Given an n x k matrix A and an k x m matrix B, with 1≤n,m,k≤300, write a C program that computes the matrix product C=AB. All entries in matrices A and B are integers with abolute value less than 1000, so you don't need to worry about overflow.

If matrices A and B do not have the right dimensions to be multiplied, the product matrix C should have its number of rows and columns both set to zero.

Input/Output: please use scanf and printf to handle the data input and output. Input


Line 1: Two space-separated integers, n and k.

Line 2 to n+1: Each line contains k integers separated by sapces: Each row of matrix A.

Line n+2: Two space-separated integers, k and m.

Line n+3 to n+k+4: Each line contains m integers separated by spaces: Each row of matrix B.

Sample Input:

3 2

1 1

1 2

-4 0

2 3

1 2 1

3 2 1

Output format:

Line 1: two space-separated n and m, the dimension of matrix C.

Line 2 to m+1: Each line contains m space-separated integers: Each row of matrix C.

Sample Output:

3 3

4 4 2

7 6 3

-4 -8 -4

please use 2D arrays and do not use a library for auto matrix multiplication.

Patrick B.

A couple of issues, but source code posted and uploaded to RESROUCES section under this link. (1) Sorry, 300 is too big for any data type. (2) You are more than welcome to change the data type from float to integer inside the matrix. Anything outside of multiplication/addition will require floats. But it works.


1 Expert Answer


Patrick B. answered • 02/08/21

4.7 (31)

Math and computer tutor/teacher

Mike A.

thank you but this code is way too complex and the inputs are basically user scan inputs not a file to read from as in the example, first input is 3 2 meaning that there will be a 3 rows and 2 col matrix then when you press enter you can enter the values in a matrix shape like 1 1 1 2 -4 0 and so the program knows that this is a completed matrix so then you can do the same for the second matrix


Patrick B.

My apologies... here is a rewritten, revision, but with USER INPUT rather than file IO. The logic is not that much different.


Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.


Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.