初识二维数组

这里写图片描述
用二维数组打印出上图效果。

代码:

#include <iostream>

using namespace std;

int main()
{
    //使用二维数组
    string stu_names[] = {"刘备", "关羽", "张飞"};
    string course_names[] = {"语文", "数学", "英语"};
    const int ROW = 3;
    const int COL = 3;
    double scores[ROW][COL];

    for(int i = 0; i < ROW; i++)
    {
        for(int j = 0; j < COL; j++)
        {
            cout << stu_names[i] << "的" << course_names[j] << "成绩: ";
            cin >> scores[i][j];
        }
    }

    cout << '\t';
    for(int i = 0; i < COL; i++)
    {
        cout << course_names[i] << '\t';
    }
    cout << endl;

     for(int i = 0; i < ROW; i++)
    {
        cout << stu_names[i] << '\t';
        for(int j = 0; j < COL; j++)
        {
            cout << scores[i][j] << '\t';
        }
        cout << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ghyyys/article/details/82053016