c - 我在这里做错了什么?为什么代码没有给出所需的输出?
问题描述
我正在尝试为矩阵乘法编写代码。我不知道我在这段代码中做错了什么,因为编译器没有给出错误,但程序仍然无法运行。我附上了输出的照片,代码在 2x2 矩阵中运行良好,但除此之外它不能正常工作。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[20][20], b[20][20], mul[20][20];
int m1, n1, m2, n2;
int i1, j1, i2, j2, i3, j3, k;
int sum = 0;
printf("Enter the number of Rows and Column for Matrix A: ");
scanf("%d %d", &m1, &n1);
printf("Enter the number of Rows and Column for Matrix B: ");
scanf("%d %d", &m2, &n2);
if (n1 == m2)
{
printf("Insert elements in matrix A-\n");
for (i1 = 1; i1 <= n1; i1++)
{
for (int j1 = 1; j1 <= m1; j1++)
{
printf("Enter A[%d][%d]: ", i1, j1);
scanf("%d", &a[i1][j1]);
}
}
printf("\n");
printf("Insert elements in matrix B-\n");
for (i2 = 1; i2 <= n2; i2++)
{
for (j2 = 1; j2 <= m2; j2++)
{
printf("Enter B[%d][%d]: ", i2, j2);
scanf("%d", &b[i2][j2]);
}
}
printf("\n");
printf("Matrix A-\n");
for (int i = 1; i <= n1; i++)
{
for (int j = 1; j <= m1; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("Matrix B-\n");
for (int i = 1; i <= n2; i++)
{
for (int j = 1; j <= m2; j++)
{
printf("%d ", b[i][j]);
}
printf("\n");
}
for (i3 = 1; i3 <= m1; i3++)
{
for (j3 = 1; j3 <= n2; j3++)
{
for (k = 1; k <= m2; k++)
{
sum = sum + a[i3][k] * b[k][j3];
}
mul[i3][j3] = sum;
sum = 0;
}
}
printf("\nThe Multiplication Matrix of the two entered Matrices is:\n");
for (int i = 1; i <= m1; i++)
{
for (int j = 1; j <= n2; j++)
{
printf("%d ", mul[i][j]);
}
printf("\n");
}
}
else
{
printf("The given information is not valid.\nFor the multiplication of two matrices (AxB), the no. of Columns of A should be equal to the no. of Rows of B.\nCheck the given Data.\n");
printf("Your given Data:-\nRow & Column of A = %d, %d\nRow & Column of B = %d, %d\n", m1, n1, m2, n2);
}
return 0;
}
解决方案
推荐阅读
- sql-server - SSIS 数据转换失败。将全为“00000000”的字段导入字符字段时可能丢失数据
- azure-web-app-service - 选择节点版本时权限被拒绝的部署问题
- android - 应用程序关闭后,工作变量被销毁
- mongodb - 当 MongoDB 的文档说 ObjectID“可能是唯一的”时,它是什么意思?
- javascript - 防止 Webpack 删除 jQuery 函数?
- asp.net-core - 配置 OData 测试服务器
- ios - Scenekit 场景需要太长时间才能显示在视图上
- react-native - React Native 在上下文状态更改后不会重新渲染
- ruby-on-rails - 附加普通 png 文件时出现 ActiveStorage::InvariableError?
- python - 当要求从文件导入时,Jupyter 使用过时版本的函数