C++打印学生成绩直方图

Pratice problem: As we introduced in the lecture, try to implement a project to finish the task:” Calculate and print out the histogram of the test scores.”.

Requirements:

1. The number of grades of the scores is 11. A detailed division of the grades can be found on the slide.

2. Maximum number of students is 10. You can assign the values in the program or input them during runtime.

3. Use the attached files where some of the functions are already implemented. You can also use your own way to design functions as well if you like.

#include<iostream>
#include<string>
using namespace std;

//定义宏常量
#define STU_NUM 10      //学生人数

//定义学生结构体
struct Student {
	int ID;             //学生ID
	int grade;          //学生成绩
};
//结构体数组赋初值函数(地址传递)
void assignArr(Student* arr, int length) {
	for (int i = 0; i < length; i++)
	{
		arr[i].ID = i + 1;
		arr[i].grade = 0;
	}
}
//循环赋值结构体数组的函数
void numArr(Student* arr, int length) {
	cout << "下面开始进行对学生成绩的录入:" << endl;
	for (int i = 0; i < length; i++)
	{
		int temp;
		cout << "第" << arr[i].ID << "个学生的成绩为:\t" << endl;
		cin >> temp;
		arr[i].grade = temp;
	}
}

//打印输出成绩直方图的函数
void printArr(const Student* arr, int length) {
	for (int i = 0; i < length; i++)
	{
		cout << "第" << arr[i].ID << "个学生的成绩为:\t";
		//根据成绩打印输出直方图
		for (int j = 0; j < arr[i].grade; j++)
		{
			cout << "*" << " ";
		}
		cout << endl;
	}
}
int main() {
	//创建结构体数组
	Student arr[STU_NUM];
	//计算结构体数组的长度
	int length = sizeof(arr) / sizeof(arr[0]);
	//初始化结构体数组
	assignArr(arr, length);
	//对结构体数组进行赋值
	numArr(arr, length);
	//绘制直方图
	printArr(arr, length);
	system("pause");
	return 0;
}

结果展示:

 

注意:其实这个问题非常基础简单,主要考察的是结构体,数组,结构体数组,函数地址传递和嵌套循环的应用,很有意思,不是很难。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值