﻿ C exercises and solutions: max and min scores

C exercises and solutions: max and min scores

C structure: max and min scores

Step 9: Defining the showmax(student st[], int itemcount) and showmin(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. //function to show max total score
void showmax(student st[], int itemcount)
{
float max = st.total;
int index=0,j;
if (itemcount >= 2)
{

for (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.total;
}

else printf("Not record found!\n");

if (index != -1) printf("The student with ID %s gets the highest score %f",st[index].stnumber,max);

}

//function to show min total score
void showmin(student st[], int itemcount)
{

float min = st.total;
int index = 0,j;
if (itemcount >= 2)
{
for (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.total;
}
else printf("No record found!\n");

if (index != -1) printf("The student with ID %s gets the highest score %f",st[index].stnumber,min);

}