C++|数组,switch语句?需要一些指导
C++ | An array, switch statement? Need some guidance
我的目标是录取(3)名学生,给他们每个人(3)个成绩,然后是每个学生的平均成绩和字母成绩。这部分效果很好。我需要的最后一部分是取这(3)个平均成绩,把它们相加,然后再取另一个平均值,即班级的平均成绩。
我该如何获得班级平均成绩?大堆切换语句?我之所以问,是因为我真的不知道。我完全不知道如何才能做到这一点。
#include <iostream>
#include <ostream>
#include "stdafx.h"
using namespace std;
// start the program
int main() {
// LOCAL VARIABLES
// int student1, student2, student3; //the (3) students -- NOT EVEN SURE
// I NEED THIS
int all_students; // number of students
double grade1, grade2, grade3; // the (3) grade variable declarations
double grade_total; // holds the sum of all grades
double grade_avg; // holds the average of the sum of grades
cout << "Enter the number of students in the class: "; // number of students
// request
cin >> all_students;
all_students = 1; // initialize counter for the loop
// need to create a loop for all three students and input for 3
// grades/student
while (all_students <= 3) // start the loop for the students
{
cout << "nnPlease enter (3) numeric grades for student "
<< all_students << ":"; // enters the 3 grades for student
cin >> grade1 >> grade2 >>
grade3; // the grades are stored in these 3 variables
grade_total = grade1 + grade2 +
grade3; // sum is the 3 sets of graders added together
grade_avg = grade_total / 3; // avg. is the sum divided by 3
// displays the output for the student grades, grade average, and letter
// grade
cout << "Student " << all_students << " grades are n" << grade1 << "n"
<< grade2 << "n" << grade3 << "n"
<< "with an average of: " << grade_avg
<< ", which is letter grade: "; // need this line to also read the
// letter grade for the average
// grade
if (grade_avg >= 90) // 90+ gets an A
cout << "A";
else if (grade_avg >= 80) // 80-89 gets a B
cout << "B";
else if (grade_avg >= 70) // 70-79 gets a C
cout << "C";
else if (grade_avg >= 60) // 60-69 gets a D
cout << "D";
else // anything less than a 60% is an F
cout << "F";
// increases counter! Incrementer.
all_students++; // Yes, I want to increment the count after the loop.
//****************************************************************//
// I need to figure out how to create an array to hold //
// each student average in order to calculate the overall //
// class average. Or use a switch statement? Advice? //
//****************************************************************//
//****************************************************************//
//****************************************************************//
}
}
您需要在while外部创建一个具有最大学生数的数组,然后在while循环内部(在循环结束时)将每个计算的平均值分配给该数组中的一个条目。
然后在while循环之外,编写一个for循环来计算数组中元素的平均值,这些元素是每个学生的平均值
最简单的方法是使用运行平均
float classAvg = 0;
const unit NUM_STUDENTS = 3;
while (all_students <= NUM_STUDENTS) //start the loop for the students
{
...
classAvg += grade_avg;
}
classAvg /= NUM_STUDENTS
cout<<"Class average is " << classAvg;
我还建议将数字等级转换为字母等级的部分转换为函数。这将使您的代码更加模块化和干净。大致如下:
void displayLetterGrade(float grade)
{
if (grade >= 90) // 90+ gets an A
cout << "A";
else if (grade >= 80) // 80-89 gets a B
cout << "B";
else if (grade >= 70) // 70-79 gets a C
cout << "C";
else if (grade >= 60) // 60-69 gets a D
cout << "D";
else // anything less than a 60% is an F
cout << "F";
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 任意大小的 constexpr 数组是否可以用作 switch 语句中的案例?
- 在编译时自动生成用于稀疏数组索引的switch语句
- 显示使用函数、switch 语句和多维数组的程序的多个不合逻辑的错误
- C++|数组,switch语句?需要一些指导
- 在哪里可以找到如何用函数数组替换"switch"运算符的示例?
- C++:在 switch 语句中构造数组