首页 > 解决方案 > 通过 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> 

请解决这个问题。非常感谢

标签: cfunctionmatrixmatrix-multiplication

解决方案


推荐阅读