【问题标题】:Sum and product of two matrices in C (with functions)C中两个矩阵的和和乘积(带函数)
【发布时间】:2019-11-02 22:42:20
【问题描述】:

我必须用 C 语言编写一个程序来求两个矩阵的和和乘积。

我编写了函数,但在main 中调用它们时遇到了困难。我不知道结果矩阵的行和列是哪个变量。

#include <stdio.h>
void enterMatrix(int a[][10], int rows, int columns)
{
    int i,j;
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            printf("a(%d,%d)=",i,j);
            scanf("%d",&a[i][j]);
        }
    }
}
void displayMatrix(int a[][10], int rows, int columns)
{
    int i,j;
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            printf("%d", a[i][j]);
            printf(" ");
        }
        printf("\n");
    }
}
void matrixSum(int a[][10], int b[][10], int c[][10], int rows, int columns)
{
    int i,j;
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
        }
    }
}
void matrixProduct(int a[][10], int b[][10], int c[][10], int rows, int columns)
{
    int i,j,k;
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            c[i][j]=0;
            for(k=0;k<columns;k++)
            {
                c[i][j]+=a[i][k]*b[k][j];
            }

        }
    }
}
int main(void)
{
    int a[10][10], b[10][10], sum[10][10], product[10][10];
    int rowsA,columnsA,rowsB,columnsB;
    printf("Number of rows for matrix A: \n");
    scanf("%d",&rowsA);
    printf("Number of columns for matrix A: \n");
    scanf("%d",&columnsA);
    printf("Number of rows for matrix B: \n");
    scanf("%d",&rowsB);
    printf("Number of columns for matrix B: \n");
    scanf("%d",&columnsB);
    printf("Enter first matrix: \n");
    enterMatrix(a,rowsA,columnsA);
    printf("Show first matrix: \n");
    displayMatrix(a,rowsA,columnsA);
    printf("Enter second matrix: \n");
    enterMatrix(b,rowsB,columnsB);
    printf("Show second matrix: \n");
    displayMatrix(b,rowsB,columnsB);

    if((rowsA==rowsB) && (columnsA==columnsB))
    {
        matrixSum(a,b,sum, ???, ???);
        printf("The sum matrix is: \n");
        displayMatrix(sum, ???, ???);
    }
    else
    {
        printf("Wrong information.");
    }
    if((rowsA==columnsB) && (rowsB==columnsA))
    {
        matrixProduct(a,b,product,???,???);
        printf("The product matrix is \n");
        displayMatrix(product,???,???);
    }
    return 0;
}

【问题讨论】:

  • 验证每个输入,例如if (scanf("%d",&amp;a[i][j]) != 1) { /* handle error */ }.

标签: c function matrix


【解决方案1】:

对于 matrixSum,您只需给出 rowsA 和 columnsA,因为它们等于 rowsB 和 columnsB。

对于 matrixProduct,您需要三个数字:rowsA、columnsA 和 columnsB。不需要 rowsB,因为它等于 columnsA。

您需要更改 matrixProduct 函数以在正确的位置使用这三个数字。除非所有数字都相等,否则您当前的代码不起作用。

在调用 matrixProduct 之前你的测试 if((rowsA==columnsB) &amp;&amp; (rowsB==columnsA)) 也只需要 if(rowsB==columnsA)

if((rowsA==rowsB) && (columnsA==columnsB))
{
    matrixSum(a,b,sum, rowsA, columnsA);
    printf("The sum matrix is: \n");
    displayMatrix(sum, rowsA, columnsA);  // the sum has the same dimensions as A and B
}
else
{
    printf("Both matrices don't have equal dimension.\n");
}
if(rowsB==columnsA)
{
    matrixProduct(a,b,product,rowsA,columnsA,columnsB);
    printf("The product matrix is \n");
    displayMatrix(product,rowsA,columnsB);  // the product matrix has the
       // number of rows of A and the number of columns of B
}
else
{
    printf("The number of columns of A needs to be equal to the number or rows of B.\n");
}

您的 matrixProduct 函数可以修改如下:

void matrixProduct(int a[][10], int b[][10], int c[][10], int rowsA, int columnsA, int columnsB)
{
    int i,j,k;
    for(i=0;i<rowsA;i++)
    {
        for(j=0;j<columnsB;j++)
        {
            c[i][j]=0;
            for(k=0;k<columnsA;k++)
            {
                c[i][j]+=a[i][k]*b[k][j];
            }
        }
    }
}

【讨论】:

  • 谢谢你的回答。编译器说我有太多的参数来函数'matrixProduct'。
  • 您需要调整对 matrixproduct 的调用以及函数本身的声明。
猜你喜欢
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
  • 2013-03-31
  • 2018-09-14
  • 1970-01-01
  • 2013-08-28
  • 2019-09-22
  • 1970-01-01
相关资源
最近更新 更多