二维数组的使用,打印学生的成绩单
#include <iostream>
using namespace std;
int main()
{
//二维数组的使用,打印学生的成绩单
string stu_names[]={"刘备","关羽","张飞"};
string course_names[]={"语文","数学","英语"};
int row = 3;//定义三行三列的数组;也可以用sizeof(stu_names) / sizeof(stu_names[0]),前提是名字一样长
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 << endl;
//打印学生的课程表
cout << "\t" ;
for(int i = 0; i < col; i++){//1、先打印课程名称
cout << course_names[i] << "\t";
}
cout << endl;
for(int i = 0; i < col; i++){//外层循环打印行,内层循环打印列。打印图时外层加换行cout << endl;
cout << stu_names[i] << "\t";//打印学生姓名
for(int j = 0; j < row ; j++){
cout << scores[i][j] << "\t";//打印各科成绩
}
cout << endl;
}
return 0;
}
打印输出
请输入刘备的语文成绩78
请输入刘备的数学成绩89
请输入刘备的英语成绩66
请输入关羽的语文成绩99
请输入关羽的数学成绩88
请输入关羽的英语成绩99
请输入张飞的语文成绩78
请输入张飞的数学成绩98
请输入张飞的英语成绩79
语文 数学 英语
刘备 78 89 66
关羽 99 88 99
张飞 78 98 79
Process returned 0 (0x0) execution time : 12.840 s
Press any key to continue.