注册 登录
编程论坛 C++教室

多维数组求和怎么起名字

叶纤 发布于 2019-12-07 21:53, 1519 次点击
//sum是把每一行的和全统计出来了,怎么比较sum中每一行和的大小或者怎么命名
#include<iostream>
using namespace std;
int main()

{   double c[3][3] = {};
    string a[]= {"张","关","羽"};
    string b[]= {"语","数","外"};
    for(int i=0; i<3; i++)
    {   for(int j=0; j<3; j++)
        {
            cout << a[i] << "的"<<b[j]<<"成绩"<<"";
            cin >> c[i][j];
        }

    }
    cout << "\t" ;string namesum="总和";
    for(int i=0; i<3; i++)
    {   cout << b[i] << "\t";

    }
    cout << namesum <<"\t";
    cout<< endl;

    for(int i=0; i<3; i++)
    {   int sum=0;
        cout << a[i] <<"\t";
        
        for(int j=0; j<3; j++)
        {   sum=sum+c[i][j];
            cout <<c[i][j]<<"\t";
        }
        cout  <<sum<<"\t";
        cout <<endl;
    }   
}
只有本站会员才能查看附件,请 登录

2 回复
#2
rjsp2019-12-08 10:57
程序代码:
#include <iostream>
#include <algorithm>
using namespace std;

int main( void )
{
    // 以下这部分代码基本上是从你原有代码抄录过来的,没什么好说的
    const char* a[3]= {"","",""};
    const char* b[3]= {"","",""};
    double c[3][3];
    double d[3];
    for( size_t i=0; i!=3; ++i )
    {
        d[i] = 0;
        for( size_t j=0; j!=3; ++j )
        {
            cout << a[i] << "" <<b[j] << "成绩: ";
            cin >> c[i][j];
            d[i] += c[i][j];
        }
    }

    // 接下来根据 总分 排序
    size_t indexs[3] = { 0, 1, 2 };
    sort( indexs, indexs+3, [=](size_t lhs, size_t rhs){return d[lhs]>d[rhs];} );

    // 以下这部分代码基本上是从你原有代码抄录过来的,没什么好说的
    for( size_t i=0; i!=3; ++i )
        cout << '\t' << b[i];
    cout << "\t总和\n";
    for( size_t i=0; i!=3; ++i )
    {
        const size_t k = indexs[i];
        cout << a[k];
        for( size_t j=0; j!=3; ++j )
            cout << '\t' << c[k][j];
        cout << '\t' << d[k] << '\n';
    }
}

#3
叶纤2019-12-08 20:07
回复 2楼 rjsp
大大,我太膜拜你了,太厉害了太厉害了。从你的代码里我可以看到你真的很用心,用我能理解的方式给我说明问题,不过我要在深入一些了,要不我真的看不懂你的代码了,真的太感谢你在百忙之中替我解答。
1