// Input : Student name, 1 test grade perstudent
// Output: Student Averages, Class Averages, Top Student Last Student
#include <string>
#include <iostream>
using namespace std;
float CalcAvgClassGrade(int s1, int s2, int s3, int s4, int s5)
{
float avg, sum;
// Sum grades, and get avg
sum = s1 + s2 + s3 + s4 + s5;
avg = sum / 2;
return avg;
}
string getMax(int s1, int s2, int s3, int s4, int s5, string n1, string n2, string n3, string n4, string n5)
{
float max;
string maxName;
// Compares each grade to find the max
max = s1;
maxName = n1;
if (max < s2)
{
max = s2;
maxName = n2;
}
if (max < s3)
{
max = s3;
maxName = n3;
}
if (max < s4)
{
max = s4;
maxName = n4;
}
if (max < s5)
{
max = s5;
maxName = n5;
}
return maxName;
}
string getMin(int s1, int s2, int s3, int s4, int s5, string n1, string n2, string n3, string n4, string n5)
{
float min;
string minName;
// Compares each grade to find the min
min = s1;
minName = n1;
if (min < s2)
{
min = s2;
minName = n2;
}
if (min < s3)
{
min = s3;
minName = n3;
}
if (min < s4);
{
min = s4;
minName = n4;
}
if (min < s5)
{
min = s5;
minName = n5;
}
return minName;
}
int main()
{
string student1, student2, student3, student4, student5;
int student1Grade, student2Grade, student3Grade, student4Grade, student5Grade;
// Enter Student Names;
cout << "Enter Student Name: ";
cin >> student1;
cout << "Enter Student Grade: ";
cin >> student1Grade;
cout << "Enter Student Name: ";
cin >> student2;
cout << "Enter Student Grade: ";
cin >> student2Grade;
cout << "Enter Student Name: ";
cin >> student3;
cout << "Enter Student Grade: ";
cin >> student3Grade;
cout << "Enter Student Name: ";
cin >> student4;
cout << "Enter Student Grade: ";
cin >> student4Grade;
cout <<"Enter Student Name : " ;
cin >> student5;
cout <<"Enter Student Grade : " ;
cin >> student5Grade;
// Class AVG
float classAvg = CalcAvgClassGrade(student1Grade, student2Grade, student3Grade, student4Grade, student5Grade);
cout << "Class average = " << classAvg;
// MAX GRADE
string classMax = getMax(student1Grade, student2Grade, student3Grade, student4Grade, student5Grade, student1, student2, student3, student4, student5);
cout << classMax << " has the highest grade in the class";
// Min Grade
string classMin = getMin(student1Grade, student2Grade, student3Grade, student4Grade, student5Grade, student1, student2, student3, student4, student5);
cout << classMin << " has the lowest grade in the class";
}
这是我的第一个c ++程序。我在格式化cout语句时遇到了麻烦,我认为我的数学中存在一些错误,因为我不断得到像2.56 x e这样的值。感谢任何建议,到目前为止我已经学到了很多关于语法的知识。我不认为我的语法在cout语句中是正确的。我需要使用endl吗?
答案 0 :(得分:1)
if (min < s2)
和其他实例是错误的。它们应该是if (min > s2)
avg = sum / 2
错了。应为avg = sum / 5
在上一个<< std::endl;
语句的末尾也有<< "\n";
或cout
来生成新行。
答案 1 :(得分:1)
你有5名学生,你必须计算5名学生的平均成绩:
float CalcAvgClassGrade(int s1, int s2, int s3, int s4, int s5)
{
float avg, sum;
// Sum grades, and get avg
sum = s1 + s2 + s3 + s4 + s5;
avg = sum / 5; //math: average on 5 students
return avg;
}
在getMin
方法中,您会错误地比较成绩和min
。
如果任何数字小于
min
,请将该数字作为新的最小值
所以在<
方法
>
转为getMin
答案 2 :(得分:0)
我在这里看到一个错误:
string getMin(int s1, int s2, int s3, int s4, int s5, string n1, string n2, string n3, string n4, string n5)
{
float min;
string minName;
// Compares each grade to find the min
min = s1;
minName = n1;
if (min < s2)
{
min = s2;
minName = n2;
}
if (min < s3)
{
min = s3;
minName = n3;
}
if (min < s4);
{
min = s4;
minName = n4;
}
if (min < s5)
{
min = s5;
minName = n5;
}
return minName;
}
您必须使用:>
而不是<