永发信息网

编程实现任意两个矩阵的相乘

答案:2  悬赏:0  手机版
解决时间 2021-05-05 04:08
  • 提问者网友:人生佛魔见
  • 2021-05-04 04:20
1.两个矩阵的行数和列数由用户在运行时输入
2.两个矩阵的元素由用户运行时输入
3.运算结果由系统输出
4.软件应能根据用户输入的矩阵的行数和列数产生矩阵的输入模型
最佳答案
  • 五星知识达人网友:鸽屿
  • 2021-05-04 04:48
#include<stdio.h>
#define M 10
#define N 10
void matrix(int a[M][N],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void comput(int a[M][N],int b[M][N],int c[M][N],int m1,int n1,int m2,int n2)
{
int i,j,k,sum;
for(i=0;i<m1;i++)
for(j=0;j<n2;j++)
{
sum=0;
for(k=0;k<n1;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
void print(int c[M][N],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%3d",c[i][j]);
printf("\n");
}
}
void main()
{
int m,n,r,p,a[M][N],b[M][N],c[M][N];
printf("输入第一个矩阵的行数与列数");
scanf("%d%d",&m,&n);
matrix(a,m,n);
printf("输入第二个矩阵的行数与列数");
scanf("%d%d",&r,&p);
matrix(b,r,p);
comput(a,b,c,m,n,r,p);
print(c,m,p);
}
全部回答
  • 1楼网友:想偏头吻你
  • 2021-05-04 05:00

    #include"stdio.h"         #include"stdlib.h"

    main() {   int i,j,select,temp,t;    int m,n,k;    int a[20][20],b[20][20],c[20][20]={0};     int s;  while(1)          {     printf("\t******************************** \n");     printf("\t\tMatrix operations    \n");     printf("\t1:Input matrix    \n");     printf("\t2:Matrix addition operation \n");     printf("\t3:Matrix multiplication    \n");     printf("\t4:Matrix transpose operation\n");     printf("\t5:Display matrix\n    \n");     printf("\t6:Exit the system    \n");     printf("\t******************************** \n");     printf("please select:(one to seven)");     scanf("%d",&select);

 if(select==1)  {

  printf("please input the matrix line:");   scanf("%d",&m);   printf("please input the matrix list:");   scanf("%d",&n);   printf("please input the first matrix:\n");   for(i=0;i<m;i++)   for(j=0;j<n;j++)   scanf("%d",&a[i][j]);   printf("please input the scend matrix:\n");   for(i=0;i<m;i++)   for(j=0;j<n;j++)   scanf("%d",&b[i][j]);   }

 else if(select==2)  {   printf("Matrix addition operation is:\n");

   for(i=0;i<m;i++)   {    for(j=0;j<n;j++)   {    c[i][j]=a[i][j]+b[i][j];    printf("%d  ",c[i][j]);   }   printf("\n");     }

    while(1)     {   printf("\n1:Back to main menu  2:Exit the system : ");     scanf("%d",&temp);     if(temp==1)  break;     else if(temp==2)  exit(0);     printf("\n\t\tSorry Input error! please input\n");     }if(temp==1)  continue;     }  else if(select==3)  {if(m==n)  {   printf("Matrix multiplication is:\n");   for(i=0;i<m;i++)  {   for(j=0;j<m;j++)   {

   for(k=0;k<n;k++)

    c[i][j]=c[i][j]+a[i][k]*b[k][j];

  }  }  for(i=0;i<m;i++)  {   for(j=0;j<n;j++)    printf("%d  ",c[i][j]);   printf("\n");   }   }   else  printf("Sorry Input error!");     while(1)     {   printf("\n1:Back to main menu  2:Exit the system : ");     scanf("%d",&temp);     if(temp==1)  break;     else if(temp==2)  exit(0);     printf("\n\t\tSorry Input error! please input\n");     }     }   else if(select==4)  {   printf("Matrix transpose operation  is:\n");    for(i=0;i<m;i++)     for(j=0;j<n;j++)     c[j][i]=a[i][j];     for(i=0;i<n;i++)     {     for(j=0;j<m;j++)     printf("%4d ",c[i][j]);     printf("\n\n");     }     for(i=0;i<m;i++)     for(j=0;j<n;j++)     c[j][i]=b[i][j];     for(i=0;i<n;i++)     {     for(j=0;j<m;j++)     printf("%4d ",c[i][j]);     printf("\n");     }     while(1)     {   printf("\n1:Back to main menu  2:Exit the system : ");     scanf("%d",&temp);     if(temp==1)  break;     else if(temp==2)  exit(0);     printf("\n\t\tSorry Input error! please input\n");     }if(temp==1)  continue;     }    else if(select==5)     {   printf("the first matrix is:\n");   for(i=0;i<m;i++)   for(j=0;j<n;j++)   printf("%4d ",&a[i][j]);   printf("\n\n");   printf("the scend matrix is:\n");   for(i=0;i<m;i++)   {for(j=0;j<n;j++)    printf("%4d ",&b[i][j]);    printf("\n");    }

    while(1)     {   printf("\n1:Back to main menu  2:Exit the system : ");     scanf("%d",&temp);     if(temp==1)  break;     else if(temp==2)  exit(0);     printf("\n\t\tSorry Input error! please input\n");     }

    }     else if(select==6)  exit(0);     else   printf("Input error!\n");     getch();

    }     }

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯