作业二——实现简单的矩阵相乘

#include <stdio.h>
#include <stdlib.h>


int main()
{
    int control,row1,column1,row2,column2,i1,j1,i2,j2,i3,j3;
    control=0;
    while(control==0)
    {
        printf("please input the number of the first matrix's rows\n");
        scanf("%d",&row1);
        printf("please input the number of the first matrix's columns\n");
        scanf("%d",&column1);
        printf("please input the number of the second matrix's rows\n");
        scanf("%d",&row2);
        printf("please input the number of the second matrix's columns\n");
        scanf("%d",&column2);
        if(column1!=row2)
        {
            printf("error!please input again\n");
            control=0;
        }
        else
        {
            control=1;
        }
    }
    int matrix1[row1][column1],matrix2[row2][column2],matrix3[row1][column2];
    for(i1=0;i1<row1;i1++)
    {
        for(j1=0;j1<column1;j1++)
        {
            printf("please input the %d ,%d element of the first matrix\n",i1,j1);
            scanf("%d",&matrix1[i1][j1]);
        }
    }
    for(i2=0;i2<row2;i2++)
    {
        for(j2=0;j2<column2;j2++)
        {
            printf("please input the %d ,%d element of the second matrix\n",i2,j2);
            scanf("%d",&matrix2[i2][j2]);
        }
    }
    for(i3=0;i3<row1;i3++)
    {
        for(j3=0;j3<column2;j3++)
        {
            matrix3[i3][j3]=0;
            for(i2=0;i2<row2;i2++)
            {
                matrix3[i3][j3]+=(matrix1[i3][i2]*matrix2[i2][j3]);
               // printf("3%d\n",matrix3[i3][j3]);
            }
        }
    }
    printf("matrix1=\n");
    for(i1=0;i1<row1;i1++)
    {
        for(j1=0;j1<column1;j1++)
        {
            printf("%5d",matrix1[i1][j1]);
        }
        printf("\n");
    }
    printf("matrix2=\n");
    for(i2=0;i2<row2;i2++)
    {
        for(j2=0;j2<column2;j2++)
        {
            printf("%5d",matrix2[i2][j2]);
        }
        printf("\n");
    }
    printf("matrix3=\n");
    for(i3=0;i3<row1;i3++)
    {
        for(j3=0;j3<column2;j3++)
        {
            printf("%5d",matrix3[i3][j3]);
        }
        printf("\n");
    }


    return 0;

}

感想:在scanf("%d\n",matrix1[i1][j1]);中“\n”是画蛇添足,不可取。直接打scanf("%d",matrix1[i1][j1])即可,还不出错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值