gpt4 book ai didi

c++ - 计算平均值最大值和最小值 C++

转载 作者:行者123 更新时间:2023-11-30 03:44:48 28 4
gpt4 key购买 nike

// 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 吗?

最佳答案

if (min < s2)而其他情况是错误的。它们应该是 if (min > s2)

avg = sum / 2是错的。应该是avg = sum / 5

还有<< std::endl;<< "\n";在你最后一次 cout 结束时语句生成一个新行。

关于c++ - 计算平均值最大值和最小值 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35232042/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com