c - 通过 C 中的函数进行矩阵乘法
问题描述
我只想创建一个可以解决矩阵乘法的程序。我可以只使用 main 函数来解决这个问题,但我没有在使用不同的函数时解决这个问题。在我的代码 1st 中,我创建了 4 个函数。第一个函数将给出用户的行数和列数。第二个函数将收集矩阵的值。第三个函数将打印函数的值。第 4 个函数将显示矩阵相乘的结果。这是我的代码以及编译器为此代码提供的错误。
#include <stdio.h>
void inputRowColForGun(int Ar, int Ac, int Br, int Bc)
{
do
{
printf("Enter the value of row & column of A: ");
scanf("%d %d", &Ar, &Ac);
printf("\n");
printf("Enter the value of row & column of B: ");
scanf("%d %d", &Br, &Bc);
printf("\n");
if (Ac == Br)
{
break;
}
printf("Error ! Your A matrix column and B matrix row should be same.\n");
} while (Ac != Br);
}
void inputmat(int Ar, int Ac, int A[Ar][Ac])
{
for (int a = 0; a < Ar; a++)
{
for (int b = 0; b < Ac; b++)
{
printf("Enter the value of Mat%d%d : ", a + 1, b + 1);
scanf("%d", &A[a][b]);
}
printf("\n");
}
}
void printmat(int r, int c, int A[r][c])
{
printf("\n\nMatrix=\n");
for (int e = 0; e < r; e++)
{
for (int f = 0; f < c; f++)
{
printf(" %d", A[e][f]);
}
printf("\n");
}
}
void resultOfGun(int A[Ar][Ac], int B[Br][Bc], int Ar, int Ac, int Br, int Bc)
{
printf("\nA*B = \n");
for (int i = 0; i < Ar; i++)
{
for (int j = 0; j < Bc; j++)
{
int R = 0;
for (int k = 0; k < Ac; k++)
{
R = R + A[i][k] * B[k][j];
}
printf(" %d ", R);
}
printf("\n");
}
}
void main()
{
int Ar, Ac, Br, Bc;
printf("=== Matrix's Multiplication === \n\n\n");
inputRowColForGun(Ar, Ac, Br, Bc);
int A[Ar][Ac], B[Br][Bc];
inputmat(A, Ar, Ac);
inputmat(B, Br, Bc);
printmat(A, Ar, Ac);
printmat(B, Br, Bc);
resultOfGun(A, B, Ar, Ac, Br, Bc);
}
Errors:
PS D:\Github\Learn c> cd "d:\Github\Learn c\" ; if ($?) { gcc tempCodeRunnerFile.c -o tempCodeRunnerFile } ; if ($?) { .\tempCodeRunnerFile
}
tempCodeRunnerFile.c:47:24: error: 'Ar' undeclared here (not in a function)
void resultOfGun(int A[Ar][Ac], int B[Br][Bc], int Ar, int Ac, int Br, int Bc)
^~
tempCodeRunnerFile.c:47:28: error: 'Ac' undeclared here (not in a function)
void resultOfGun(int A[Ar][Ac], int B[Br][Bc], int Ar, int Ac, int Br, int Bc)
^~
tempCodeRunnerFile.c:47:39: error: 'Br' undeclared here (not in a function)
void resultOfGun(int A[Ar][Ac], int B[Br][Bc], int Ar, int Ac, int Br, int Bc)
^~
tempCodeRunnerFile.c:47:43: error: 'Bc' undeclared here (not in a function)
void resultOfGun(int A[Ar][Ac], int B[Br][Bc], int Ar, int Ac, int Br, int Bc)
^~
tempCodeRunnerFile.c: In function 'main':
tempCodeRunnerFile.c:71:14: warning: passing argument 1 of 'inputmat' makes integer from pointer without a cast [-Wint-conversion]
inputmat(A, Ar, Ac);
^
tempCodeRunnerFile.c:21:19: note: expected 'int' but argument is of type 'int (*)[(sizetype)(Ac)]'
void inputmat(int Ar, int Ac, int A[Ar][Ac])
~~~~^~
tempCodeRunnerFile.c:71:21: warning: passing argument 3 of 'inputmat' makes pointer from integer without a cast [-Wint-conversion]
inputmat(A, Ar, Ac);
^~
tempCodeRunnerFile.c:21:35: note: expected 'int (*)[(sizetype)(Ac)]' but argument is of type 'int'
void inputmat(int Ar, int Ac, int A[Ar][Ac])
~~~~^~~~~~~~~
tempCodeRunnerFile.c:72:14: warning: passing argument 1 of 'inputmat' makes integer from pointer without a cast [-Wint-conversion]
inputmat(B, Br, Bc);
^
tempCodeRunnerFile.c:21:19: note: expected 'int' but argument is of type 'int (*)[(sizetype)(Bc)]'
void inputmat(int Ar, int Ac, int A[Ar][Ac])
~~~~^~
tempCodeRunnerFile.c:72:21: warning: passing argument 3 of 'inputmat' makes pointer from integer without a cast [-Wint-conversion]
inputmat(B, Br, Bc);
^~
tempCodeRunnerFile.c:21:35: note: expected 'int (*)[(sizetype)(Ac)]' but argument is of type 'int'
void inputmat(int Ar, int Ac, int A[Ar][Ac])
~~~~^~~~~~~~~
tempCodeRunnerFile.c:73:14: warning: passing argument 1 of 'printmat' makes integer from pointer without a cast [-Wint-conversion]
printmat(A, Ar, Ac);
^
tempCodeRunnerFile.c:34:19: note: expected 'int' but argument is of type 'int (*)[(sizetype)(Ac)]'
void printmat(int r, int c, int A[r][c])
~~~~^
tempCodeRunnerFile.c:73:21: warning: passing argument 3 of 'printmat' makes pointer from integer without a cast [-Wint-conversion]
printmat(A, Ar, Ac);
^~
tempCodeRunnerFile.c:34:33: note: expected 'int (*)[(sizetype)(c)]' but argument is of type 'int'
void printmat(int r, int c, int A[r][c])
~~~~^~~~~~~
tempCodeRunnerFile.c:74:14: warning: passing argument 1 of 'printmat' makes integer from pointer without a cast [-Wint-conversion]
printmat(B, Br, Bc);
^
tempCodeRunnerFile.c:34:19: note: expected 'int' but argument is of type 'int (*)[(sizetype)(Bc)]'
void printmat(int r, int c, int A[r][c])
~~~~^
tempCodeRunnerFile.c:74:21: warning: passing argument 3 of 'printmat' makes pointer from integer without a cast [-Wint-conversion]
printmat(B, Br, Bc);
^~
tempCodeRunnerFile.c:34:33: note: expected 'int (*)[(sizetype)(c)]' but argument is of type 'int'
void printmat(int r, int c, int A[r][c])
~~~~^~~~~~~
tempCodeRunnerFile.c:75:17: error: type of formal parameter 1 is incomplete
resultOfGun(A, B, Ar, Ac, Br, Bc);
^
tempCodeRunnerFile.c:75:20: error: type of formal parameter 2 is incomplete
resultOfGun(A, B, Ar, Ac, Br, Bc);
^
PS D:\Github\Learn c>
请解决这个问题。非常感谢
解决方案
推荐阅读
- amazon-web-services - 屏幕:连续记录缓冲区值并保存到文件中
- r - sparkr中的foreach函数
- javascript - AJAX - 发布到 MVC 控制器方法而不重新加载页面
- opengl - 顶点属性的插值
- c++ - 将成员函数作为 C++ 标准库算法的比较运算符传递
- vue.js - 使用签名 URL 和 dropzone (Vuejs) 上传到 Google 云存储
- nginx - nginx 在特定端口上启用身份验证
- c# - 如何生成楼梯?
- python-2.7 - 导入 ConfigParser (Python 2.7.12) 时编译的控制台应用程序立即退出
- vba - 如何调整访问表单的大小以适应屏幕