Matrix Multiplication using C
#include<stdio.h>
void matmul(int mat1[][], int mat2[][], int res[][], int m, int n, int q);
void matinput(int mat[][], int x, int y);
void matprint(int mat[][], int x, int y);
int main()
{
int m, n, p, q, i, j, k;
int mat1[10][10], mat2[10][10], res[10][10];
printf("Enter order of first matrix: ");
scanf("%d %d", &m, &n);
printf("Enter order of second matrix: ");
scanf("%d %d", &p, &q);
if(n == p)
{
printf("Enter elements of matrix 1: ");
matinput(mat1, m, n);
printf("Enter elements of matrix 2: ");
matinput(mat2, p, q);
matmul(mat1, mat2, res, m, q, n);
matprint(res, m, q);
}
else
{
printf("Invalid input...");
}
return 0;
}
void matinput(int mat[][10], int x, int y)
{
int i, j;
for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
scanf("%d", &mat[i][j]);
}
}
}
void matprint(int mat[10][10], int x, int y)
{
int i, j;
printf("The resultant matrix is : \n");
for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
void matmul(int mat1[10][10], int mat2[10][10], int res[10][10], int x, int y, int z)
{
int i, j, k;
for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
res[i][j] = 0;
for (k = 0; k < z; k++)
{
res[i][j] = res[i][j] + mat1[i][k] * mat2[k][j];
}
}
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.