C++ exercises solutions: max and min scores


C++ structure exercise: max and min scores

Step 9: Defining the showmax(struct student st[], int itemcount) and showmin(struct student st[], int itemcount) functions show about the student who gets the maximum score and the student who gets the minimum score. To find the highest total core or lowest total core, we need to compare every total score of each element.

void showmax(struct student st[], int itemcount)
{
  float max = st[0].total;
  int index=0;
  if (itemcount >= 2)
  {

    for (int j = 0; j < itemcount-1; ++j)
    if (max < st[j+1].total) {
    max = st[j+1].total;
    index = j+1;

  }


}

 else if (itemcount == 1)
 {
   index = 0;
   max = st[0].total;
 }


 else cout<<"Not record found!\n";

 if (index != -1) cout<<"The student with ID "<<st[index].stnumber<<" gets the highest score "<<max<<endl;


 }

void showmin(struct student st[], int itemcount)
{

  float min = st[0].total;
  int index = 0;
  if (itemcount >= 2)
   {
    for (int j = 0; j < itemcount-1; ++j)
    if (min > st[j+1].total)
    {
      min = st[j+1].total;
      index = j+1;

    }


  }

  else if (itemcount == 1)
  {
    index = 0;
    min = st[0].total;
   }
  else cout<<"No record found!\n";

  if (index != -1) cout<<"The student with ID "<<st[index].stnumber<<" gets the highest score "<<min<<endl;



}





Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.