﻿ C++ exercises solutions: max and min scores

# 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;

}