用C语言写一个不同行列矩阵的乘法的程序
时间: 2023-05-24 12:01:01 浏览: 120
C语言中矩阵乘法代码
当您需要实现不同行列矩阵的乘法时,可以使用以下C语言代码:
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], c[10][10];
int m, n, p, q, i, j, k;
printf("Enter the size of the first matrix: ");
scanf("%d%d", &m, &n);
printf("Enter the elements of the first matrix: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the size of the second matrix: ");
scanf("%d%d",&p, &q);
if (n != p)
{
printf("The matrices cannot be multiplied");
return 0;
}
printf("Enter the elements of the second matrix: ");
for (i = 0; i < p; i++)
{
for (j = 0; j < q; j++)
{
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
{
c[i][j] = 0;
for (k = 0; k < n; k++)
{
c[i][j] += a[i][k]*b[k][j];
}
}
}
printf("The resultant matrix is:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
return 0;
}
希望这能对您有所帮助!
阅读全文