| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 976 人关注过本帖
标题:矩阵的加减乘,但是老是调用不出来,不知道为什么
只看楼主 加入收藏
房东的猫
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-5-25
结帖率:0
收藏
 问题点数:0 回复次数:1 
矩阵的加减乘,但是老是调用不出来,不知道为什么
#include<stdio.h>
#include<stdlib.h>
#define N 20
void matrix_addition(int a[N][N],int b[N][N],int c[N][N])
{
    int a1,a2,b1,b2;
    int i,j;
    if(a1==b1&&a2==b2)
    {
    for(i=0;i<a1;i++)
        for(j=0;j<a2;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
        }
        for(i=0;i<a1;i++)
        {
            for(j=0;j<a2;j++)
            printf("%d",c[i][j]);
            printf("\n");
        }
}
    else
    {
        printf("两矩阵不能相加\n");
    }
}
void matrix_subtraction(int a[N][N],int b[N][N],int c[N][N])
{
    int a1,a2,b1,b2;
    int i,j;
    if(a1==b1&&a2==b2)
    {
    for(i=0;i<a1;i++)
        for(j=0;j<a2;j++)
        {
            c[i][j]=a[i][j]-b[i][j];
        }
        for(i=0;i<a1;i++)
        {
            for(j=0;j<a2;j++)
             printf("%d",c[i][j]);
             printf("\n");
        }
}
    else
    {
        printf("两矩阵不能相减\n");
    }
}
void matrix_multiplication(int a[N][N],int b[N][N],int c[N][N])
{
    int a1,a2,b1,b2;
    int i,j,k;
    if(a2==b1)
    {
    for(i=0;i<a1;i++)
        for(j=0;j<b2;j++)
            c[i][j]=0;
    for(i=0;i<a1;i++)
        for(j=0;j<a2;j++)
            for(k=0;k<a2;k++)
            {
                c[i][j]=a[i][k]*b[k][j];
            }

            printf("运算结果为:\n");
            for(i=0;i<a1;i++)
            {
                for(j=0;j<a2;j++)
                printf("%d",c[i][j]);
                printf("\n");
            }
    }
    else
    {
        printf("两矩阵不能相乘\n");
    }
}
main()
{
printf("  \n");
printf("  \n");
printf("  \n");
printf("  \n");
printf("  \n");
printf("                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("                ~                                      ~\n");
printf("                ~                                      ~\n");
printf("                ~              矩阵的运算              ~\n");
printf("                ~                                      ~\n");
printf("                ~                                      ~\n");
printf("                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("  \n");
printf("  \n");
printf("  \n");
int a[N][N],b[N][N],c[N][N];
int i=0,j=0,a1=0,a2=0,b1=0,b2=0;
printf("请输入a矩阵的行数和列数:\n");
scanf("%d%d",&a1,&a2);
if(a1>N||a2>N)
{
    printf("error!");
}
printf("请输入a矩阵:\n");
       for(i=0;i<a1;i++)
           for(j=0;j<a2;j++)
           {
               scanf("%d",&a[i][j]);
           }
           printf("矩阵a为:\n");
           for(i=0;i<a1;i++)
           {
               for(j=0;j<a2;j++)
                   printf("%d",a[i][j]);
               printf("\n");
           }
printf("请输入b矩阵的行数和列数:\n");
scanf("%d%d",&b1,&b2);
if(b1>N||b2>N)
{
    printf("error!");
}

printf("请输入b矩阵:\n");
       for(i=0;i<b1;i++)
           for(j=0;j<b2;j++)
           {
               scanf("%d",&b[i][j]);
            }
           printf("矩阵b为:\n");
           for(i=0;i<b1;i++)
           {
               for(j=0;j<b2;j++)
                   printf("%d",b[i][j]);
               printf("\n");
           }
int flag;
printf("                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("                ~   请选择要进行的操作:                ~\n");
printf("                ~     1、加法                          ~\n");
printf("                ~     2、减法                          ~\n");
printf("                ~     3、乘法                          ~\n");
printf("                ~     4、退出                          ~\n");
printf("                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
scanf("%d",&flag);
while((flag==1)||(flag==2)||(flag==3)||(flag==4))
{
    if(flag==1)
        matrix_addition(a,b,c);
    else if(flag==2)
        matrix_subtraction(a,b,c);
    else if(flag==3)
        matrix_multiplication(a,b,c);
    else {exit(0);}
printf("           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("           ~   请选择要进行的操作:                ~\n");
printf("           ~     1、加法                          ~\n");
printf("           ~     2、减法                          ~\n");
printf("           ~     3、乘法                          ~\n");
printf("           ~     4、退出                          ~\n");
printf("           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
scanf("%d",&flag);
}
}        


[此贴子已经被作者于2018-5-26 00:26编辑过]

搜索更多相关主题的帖子: 矩阵 int for i++ printf 
2018-05-26 00:03
康明贤
Rank: 2
来 自:NWPU
等 级:论坛游民
帖 子:46
专家分:32
注 册:2017-10-23
收藏
得分:0 
#include<stdio.h>
#include<stdlib.h>
#define N 20
void matrix_addition(int a[N][N],int b[N][N],int c[N][N],int a1,int a2,int b1,int b2)
{
    int i,j;
    if(a1==b1&&a2==b2)
    {
    for(i=0;i<a1;i++)
        for(j=0;j<a2;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
        }
        for(i=0;i<a1;i++)
        {
            for(j=0;j<a2;j++)
            printf("%d",c[i][j]);
            printf("\n");
        }
}
    else
    {
        printf("两矩阵不能相加\n");
    }
}
void matrix_subtraction(int a[N][N],int b[N][N],int c[N][N],int a1,int a2,int b1,int b2)
{
    int i,j;
    if(a1==b1&&a2==b2)
    {
    for(i=0;i<a1;i++)
        for(j=0;j<a2;j++)
        {
            c[i][j]=a[i][j]-b[i][j];
        }
        for(i=0;i<a1;i++)
        {
            for(j=0;j<a2;j++)
             printf("%d",c[i][j]);
             printf("\n");
        }
}
    else
    {
        printf("两矩阵不能相减\n");
    }
}
void matrix_multiplication(int a[N][N],int b[N][N],int c[N][N],int a1,int a2,int b1,int b2)
{
    int i,j,k;
    if(a2==b1)
    {
    for(i=0;i<a1;i++)
        for(j=0;j<b2;j++)
            c[i][j]=0;
    for(i=0;i<a1;i++)
        for(j=0;j<a2;j++)
            for(k=0;k<a2;k++)
            {
                c[i][j]=a[i][k]*b[k][j];
            }

            printf("运算结果为:\n");
            for(i=0;i<a1;i++)
            {
                for(j=0;j<a2;j++)
                printf("%d",c[i][j]);
                printf("\n");
            }
    }
    else
    {
        printf("两矩阵不能相乘\n");
    }
}
main()
{
printf("  \n");
printf("  \n");
printf("  \n");
printf("  \n");
printf("  \n");
printf("                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("                ~                                      ~\n");
printf("                ~                                      ~\n");
printf("                ~              矩阵的运算              ~\n");
printf("                ~                                      ~\n");
printf("                ~                                      ~\n");
printf("                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("  \n");
printf("  \n");
printf("  \n");
int a[N][N],b[N][N],c[N][N];
int i=0,j=0,a1=0,a2=0,b1=0,b2=0;
printf("请输入a矩阵的行数和列数:\n");
scanf("%d%d",&a1,&a2);
if(a1>N||a2>N)
{
    printf("error!");
}
printf("请输入a矩阵:\n");
       for(i=0;i<a1;i++)
           for(j=0;j<a2;j++)
           {
               scanf("%d",&a[i][j]);
           }
           printf("矩阵a为:\n");
           for(i=0;i<a1;i++)
           {
               for(j=0;j<a2;j++)
                   printf("%d",a[i][j]);
               printf("\n");
           }
printf("请输入b矩阵的行数和列数:\n");
scanf("%d%d",&b1,&b2);
if(b1>N||b2>N)
{
    printf("error!");
}

printf("请输入b矩阵:\n");
       for(i=0;i<b1;i++)
           for(j=0;j<b2;j++)
           {
               scanf("%d",&b[i][j]);
            }
           printf("矩阵b为:\n");
           for(i=0;i<b1;i++)
           {
               for(j=0;j<b2;j++)
                   printf("%d",b[i][j]);
               printf("\n");
           }
int flag;
printf("                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("                ~   请选择要进行的操作:                ~\n");
printf("                ~     1、加法                          ~\n");
printf("                ~     2、减法                          ~\n");
printf("                ~     3、乘法                          ~\n");
printf("                ~     4、退出                          ~\n");
printf("                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
scanf("%d",&flag);
while((flag==1)||(flag==2)||(flag==3)||(flag==4))
{
    if(flag==1)
        matrix_addition(a,b,c,a1,a2,b1,b2);
    else if(flag==2)
        matrix_subtraction(a,b,c,a1,a2,b1,b2);
    else if(flag==3)
        matrix_multiplication(a,b,c,a1,a2,b1,b2);
    else {exit(0);}
printf("           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("           ~   请选择要进行的操作:                ~\n");
printf("           ~     1、加法                          ~\n");
printf("           ~     2、减法                          ~\n");
printf("           ~     3、乘法                          ~\n");
printf("           ~     4、退出                          ~\n");
printf("           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
scanf("%d",&flag);
}
}
ps:你的矩阵乘法有错,思考一下。

千里之行,始于足下。
2018-05-27 11:35
快速回复:矩阵的加减乘,但是老是调用不出来,不知道为什么
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042959 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved