Calculating Averages Max and Min C++ -
// 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, , 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 find 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 find 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 highest grade in class"; // min grade string classmin = getmin(student1grade, student2grade, student3grade, student4grade, student5grade, student1, student2, student3, student4, student5); cout << classmin << " has lowest grade in class"; }
this first c ++ program. having trouble formatting cout statements , think there errors in math because keep getting values 2.56 x e. suggestion appreciated, have learned alot syntax far. dont think syntax correct on cout statements. need use endl?
if (min < s2)
, other instances of wrong. should if (min > s2)
avg = sum / 2
wrong. should avg = sum / 5
also have << std::endl;
or << "\n";
@ end of last cout
statement generate new line.
Comments
Post a Comment