Can anyone help me with this. I am suppose to put out the students names and grade, also the total, average, highest and lowest. But everything i do doesn't work.
#include <iostream>
#include <iomanip>
using namespace std;
float average(float grades, float sum, int count)
{
float avg = sum / count;
return avg;
}
void printOut(float minim, float maxim, float avg)
{
cout << " The Largest is: "<<maxim<<setprecision(2)<<endl;
cout << "The Smallest is: "<<minim<<setprecision(2)<<endl;
cout << "The average is: "<<avg<<setprecision(2)<<endl;
}
int main ()
{
float grades, sum, minim, maxim, avg;
int count;
sum = 0.0;
count = 0;
cout << "Enter Grades then -1 :"<<"\n=======================>> %";
cin >> grades;
while (grades >= 0.0)
{
minim = grades;
maxim = grades;
sum += grades;
maxim=largest( grades);
minim=smallest(grades);
}
cout << "Enter Grades then -1: "<<"\n=======================>> %";
cin >> grades;
main.cpp:24:7: warning: unused parameter ‘grades’ [-Wunused-parameter]
float average(float grades, float sum, int count)
^
main.cpp: In function ‘int main()’:
main.cpp:40:6: warning: variable ‘count’ set but not used [-Wunused-but-set-variable]
int count;
^
main.cpp: In function ‘float largest(float)’:
main.cpp:9:2: warning: ‘maxim’ is used uninitialized in this function [-Wuninitialized]
if (grades > maxim)
^
main.cpp: In function ‘int main()’:
main.cpp:56:29: warning: ‘avg’ may be used uninitialized in this function [-Wmaybe-uninitialized]
printOut(minim, maxim, avg);
^
Not to mention completely broken logic here.
Advise: read your grades into container, then pass it to functions which will return maximum, minimum, average, etc.
I would suggest using an std::multimap as the container.
A multimap of std::string to float would associate an arbitrary number of grades to a student's name. However maybe it's not such a good idea to jump into that if you're not very experienced in C++.
A simpler approach, without student's names, would be to store the grades in an std::list of float. Then you could feed that list to functions from the algorithm and numeric headers to find minimum, maximum and average.