c语言,在主函数中输入3×3的矩阵ab和c,编写三个函数分别计算c=a+b,c=a-b,c=a*b
答案:3 悬赏:60 手机版
解决时间 2021-03-01 15:46
- 提问者网友:wodetian
- 2021-02-28 23:40
c语言,在主函数中输入3×3的矩阵ab和c,编写三个函数分别计算c=a+b,c=a-b,c=a*b
最佳答案
- 五星知识达人网友:深街酒徒
- 2021-03-01 00:15
#include
void asum(double a[3][3],double b[3][3],double c[3][3])
{ int i,j; for ( i=0;i<3;i++ ) for ( j=0;j<3;j++ ) c[i][j]=a[i][j]+b[i][j];
}
void asub(double a[3][3],double b[3][3],double c[3][3])
{ int i,j; for ( i=0;i<3;i++ ) for ( j=0;j<3;j++ ) c[i][j]=a[i][j]-b[i][j];
}
void amul(double a[3][3],double b[3][3],double c[3][3])
{ int i,j; for ( i=0;i<3;i++ ) for ( j=0;j<3;j++ ) c[i][j]=a[i][j]*b[i][j];
}
void main()
{ double a[3][3],b[3][3],c[3][3];
int i,j;
for ( i=0;i<3;i++ ) for ( j=0;j<3;j++ ) scanf("%lf",&a[i][j]);
for ( i=0;i<3;i++ ) for ( j=0;j<3;j++ ) scanf("%lf",&b[i][j]);
printf("c=a+b\n");
asum(a,b,c); for ( i=0;i<3;i++,printf("\n") ) for ( j=0;j<3;j++ ) printf("%16lf",c[i][j]);
printf("c=a-b\n");
asub(a,b,c); for ( i=0;i<3;i++,printf("\n") ) for ( j=0;j<3;j++ ) printf("%16lf",c[i][j]);
printf("c=a*b\n");
amul(a,b,c); for ( i=0;i<3;i++,printf("\n") ) for ( j=0;j<3;j++ ) printf("%16lf",c[i][j]);
}
void asum(double a[3][3],double b[3][3],double c[3][3])
{ int i,j; for ( i=0;i<3;i++ ) for ( j=0;j<3;j++ ) c[i][j]=a[i][j]+b[i][j];
}
void asub(double a[3][3],double b[3][3],double c[3][3])
{ int i,j; for ( i=0;i<3;i++ ) for ( j=0;j<3;j++ ) c[i][j]=a[i][j]-b[i][j];
}
void amul(double a[3][3],double b[3][3],double c[3][3])
{ int i,j; for ( i=0;i<3;i++ ) for ( j=0;j<3;j++ ) c[i][j]=a[i][j]*b[i][j];
}
void main()
{ double a[3][3],b[3][3],c[3][3];
int i,j;
for ( i=0;i<3;i++ ) for ( j=0;j<3;j++ ) scanf("%lf",&a[i][j]);
for ( i=0;i<3;i++ ) for ( j=0;j<3;j++ ) scanf("%lf",&b[i][j]);
printf("c=a+b\n");
asum(a,b,c); for ( i=0;i<3;i++,printf("\n") ) for ( j=0;j<3;j++ ) printf("%16lf",c[i][j]);
printf("c=a-b\n");
asub(a,b,c); for ( i=0;i<3;i++,printf("\n") ) for ( j=0;j<3;j++ ) printf("%16lf",c[i][j]);
printf("c=a*b\n");
amul(a,b,c); for ( i=0;i<3;i++,printf("\n") ) for ( j=0;j<3;j++ ) printf("%16lf",c[i][j]);
}
全部回答
- 1楼网友:猎心人
- 2021-03-01 02:13
a、b、c都是3*3的矩阵么?追问是
- 2楼网友:持酒劝斜阳
- 2021-03-01 00:36
#include
int main(void)
{
void output(int num[][3]);
int i, j;
int a[3][3], b[3][3], c[3][3];
puts("请输入矩阵a : ");
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
scanf("%d", a[i] + j);
}
puts("请输入矩阵b : ");
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
scanf("%d", b[i] + j);
}
puts("a+b : ");
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
c[i][j] = a[i][j] + b[i][j];
}
output(c);
puts("a-b : ");
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
c[i][j] = a[i][j] - b[i][j];
}
output(c);
puts("a*b : ");
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
c[i][j] = a[i][j] * b[i][j];
}
output(c);
return 0;
}
void output(int num[][3])
{
int i, j;
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
printf("%-5d", num[i][j]);
putchar(' ');
}
}
int main(void)
{
void output(int num[][3]);
int i, j;
int a[3][3], b[3][3], c[3][3];
puts("请输入矩阵a : ");
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
scanf("%d", a[i] + j);
}
puts("请输入矩阵b : ");
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
scanf("%d", b[i] + j);
}
puts("a+b : ");
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
c[i][j] = a[i][j] + b[i][j];
}
output(c);
puts("a-b : ");
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
c[i][j] = a[i][j] - b[i][j];
}
output(c);
puts("a*b : ");
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
c[i][j] = a[i][j] * b[i][j];
}
output(c);
return 0;
}
void output(int num[][3])
{
int i, j;
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
printf("%-5d", num[i][j]);
putchar(' ');
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯