#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;
#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])即可,还不出错。