# Matrix multiplication in C Program

Hello guys

I am a student of Computer Technology. I am studying C/C++ and very soon will start Java. Its really hard to study three language at a time. But there is no way to avoid this ! Because I have to study C/C++ and Java at a time to cut a good figure at exam. Beside its really fun to learn programming ! I always enjoy programming.

I have just come here at CodeClerks forum and thought that I can learn new programming logic here and also can share some of my programs. I hope you guys will also share your programming knowledge with me. ;)

Recently I have just wrote a program for Multiplying two Matrix. I believe that we can solve a problem with several algorithms. So I thought I should share my code here and hope you will also share your code with me. Then we will be able to learn different algorithms

Now, tell me about your algorithm of matrix multiply. Can we do this with one dimensional/linear array ??

I am a student of Computer Technology. I am studying C/C++ and very soon will start Java. Its really hard to study three language at a time. But there is no way to avoid this ! Because I have to study C/C++ and Java at a time to cut a good figure at exam. Beside its really fun to learn programming ! I always enjoy programming.

I have just come here at CodeClerks forum and thought that I can learn new programming logic here and also can share some of my programs. I hope you guys will also share your programming knowledge with me. ;)

Recently I have just wrote a program for Multiplying two Matrix. I believe that we can solve a problem with several algorithms. So I thought I should share my code here and hope you will also share your code with me. Then we will be able to learn different algorithms

#include <stdio.h>

int main()

{

int m, n, p, q, c, d, k, sum = 0;

int first_matrix[10][10], second_matrix[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");

scanf("%d%d", &m, &n);

printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

scanf("%d", &first_matrix[c][d]);

printf("Enter the number of rows and columns of second matrix\n");

scanf("%d%d", &p, &q);

if (n != p)

printf("Matrices with entered orders can't be multiplied with each other.\n");

else

{

printf("Enter the elements of second matrix\n");

for (c = 0; c < p; c++)

for (d = 0; d < q; d++)

scanf("%d", &second_matrix[c][d]);

for (c = 0; c < m; c++) {

for (d = 0; d < q; d++) {

for (k = 0; k < p; k++) {

sum = sum + first_matrix[c][k]*second_matrix[k][d];

}

multiply[c][d] = sum;

sum = 0;

}

}

printf("Product of entered matrices:-\n");

for (c = 0; c < m; c++) {

for (d = 0; d < q; d++)

printf("%d\t", multiply[c][d]);

printf("\n");

}

}

return 0;

}

int main()

{

int m, n, p, q, c, d, k, sum = 0;

int first_matrix[10][10], second_matrix[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");

scanf("%d%d", &m, &n);

printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

scanf("%d", &first_matrix[c][d]);

printf("Enter the number of rows and columns of second matrix\n");

scanf("%d%d", &p, &q);

if (n != p)

printf("Matrices with entered orders can't be multiplied with each other.\n");

else

{

printf("Enter the elements of second matrix\n");

for (c = 0; c < p; c++)

for (d = 0; d < q; d++)

scanf("%d", &second_matrix[c][d]);

for (c = 0; c < m; c++) {

for (d = 0; d < q; d++) {

for (k = 0; k < p; k++) {

sum = sum + first_matrix[c][k]*second_matrix[k][d];

}

multiply[c][d] = sum;

sum = 0;

}

}

printf("Product of entered matrices:-\n");

for (c = 0; c < m; c++) {

for (d = 0; d < q; d++)

printf("%d\t", multiply[c][d]);

printf("\n");

}

}

return 0;

}

Now, tell me about your algorithm of matrix multiply. Can we do this with one dimensional/linear array ??

April 10, 2017, 2:37 pm

## Responses (2)

codekid 0NomanProdhan 0Thanks for your reply.

Actually you are right that C,C++ and Java are nearly same. I mean they have some similar syntax. Currently I am focusing on C and also learning C++. I didn't started Java yet. But I will need to start it soon. Because I don't have much time to study all of these programming languages. As I told I am focusing on C now so I didn't run much programs in C++. I have just done some general programs with Class, Object, Inheritance etc. So, from my little experience I think C++ will better then C. Because we can't make class, object in C like we can do with C++. Don't know about Java. After learning all the three languages then I can tell which I will like most