Emery_fly 2016-12-03 08:26 采纳率: 50%
浏览 1198
已采纳

将代码添加完整,使输出如图所示

如图图片说明注意!!!只是添加生成矩阵A和B,显示矩阵,矩阵相加这几个函数的代码和有?的地方,不能改变其他内容!
图片中有要求!!!

  • 写回答

2条回答 默认 最新

  • fx_dev 2016-12-03 11:04
    关注

    完整的示例程序如下:
    #include

    typedef struct
    {
    int pMat;/保存矩阵数据*/
    int row;
    int col;
    }MAT,*pMAT;
    /*字符串类型*/
    typedef char * string;
    /*生成矩阵A*/
    void CreateMatA(pMAT a);
    /*生成矩阵B*/
    void CreateMatB(pMAT a);
    /*显示矩阵*/
    void ShowMat(string str,pMAT a);
    /*矩阵相加*/
    void MatAdd(pMAT a,pMAT b,pMAT c);

    void main()
    {
    int m = 3;
    int n = 4;
    MAT A={0,m,n},B={0,m,n};
    pMAT C = new MAT;

    CreateMatA(&A);
    CreateMatB(&B);
    MatAdd(&A,&B,C);
    ShowMat("A:",&A);
    ShowMat("B:",&B);
    ShowMat("C=A+B:",C);
    delete A.pMat;/*释放内存*/
    delete B.pMat;
    delete C->pMat;
    delete C;
    printf("按任意键退出");
    getch();/*按任意键退出*/
    

    }
    void CreateMatA(pMAT a)
    {
    int row,col;
    int offset = 0;
    int nV1,nV2;
    a->pMat = new int[a->row * a->col];
    for(row = 0;row < a->row;row++)
    {
    nV1 = (row + 1) * 10;
    for (col = 0;col < a->col;col++)
    {
    nV2 = col * 2;
    offset = row * a->col + col;
    a->pMat[offset] = nV1 + nV2;
    }
    }
    }
    void CreateMatB(pMAT a)
    {
    int row,col;
    int offset = 0;
    int nV1,nV2;
    a->pMat = new int[a->row * a->col];
    for(row = 0;row < a->row;row++)
    {
    nV1 = (row + 1) * 10;
    for (col = 0;col < a->col;col++)
    {
    nV2 = col * 1;
    offset = row * a->col + col;
    a->pMat[offset] = nV1 + nV2;
    }
    }
    }
    void ShowMat(string str,pMAT a)
    {
    int row,col;
    int offset = 0;
    printf("%s\n",str);
    for(row = 0;row < a->row;row++)
    {
    for (col = 0;col < a->col;col++)
    {
    offset = row * a->col + col;
    printf("%2d ",a->pMat[offset]);
    }
    printf("\n");
    }
    printf("\n");
    }
    void MatAdd(pMAT a,pMAT b,pMAT c)
    {
    int row,col;
    int offset = 0;
    c->row = a->row;
    c->col = a->col;
    c->pMat = new int[a->row * a->col];
    for(row = 0;row < a->row;row++)
    {
    for (col = 0;col < a->col;col++)
    {
    offset = row * a->col + col;
    c->pMat[offset] = a->pMat[offset] + b->pMat[offset];
    }
    }
    }
    运行结果如下:
    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题