functions in the programming

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 largest(float grades)
{

float maxim;

if (grades > maxim)
maxim = grades;


return maxim;

}

float smallest(float grades)
{

float minim = grades;
if (grades<minim)
minim = grades;
return minim;

}

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;

printOut(minim, maxim, avg);



return 0;
}
Chat Conversation End
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++.

http://www.cplusplus.com/reference/map/multimap/
http://www.cplusplus.com/reference/map/multimap/equal_range/

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.

http://www.cplusplus.com/reference/algorithm/min_element/
http://www.cplusplus.com/reference/algorithm/max_element/
http://www.cplusplus.com/reference/numeric/accumulate/

All in all, it's a good idea to store the grades somewhere. What you do currently is akin to reading then throwing them away.
1
2
3
4
5
6
7
8
9
float largest(float grades)
{
float maxim;

if (grades > maxim)
maxim = grades;

return maxim;
}


This is bad. You are declaring maxim locally so it's value will be unpredictable.
Topic archived. No new replies allowed.