Teacher has Prepared the result of a class containing n number of students. He wants to give the award to 3 toppers of the class. Display the result for 3 toppers as reflected as percentage form in floating type data
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
struct Student
{
string name;
float percentage{ -1 };
};
int main()
{
const int n = 3;
Student stud[n];
Student result[3];
// Input of initial data
for (int i = 0; i < n; i++)
{
cout << "Enter student's name: ";
cin>> stud[i].name;
cout << "Enter the test result as a percentage: ";
cin >> stud[i].percentage;
}
// data processing
for (int i = 0; i < n; i++)
{
if (stud[i].percentage > result[0].percentage)
{
result[2].name = result[1].name;
result[2].percentage = result[1].percentage;
result[1].name = result[0].name;
result[1].percentage = result[0].percentage;
result[0].name = stud[i].name;
result[0].percentage = stud[i].percentage;
}
else if (stud[i].percentage <= result[0].percentage && stud[i].percentage > result[1].percentage)
{
result[2].name = result[1].name;
result[2].percentage = result[1].percentage;
result[1].name = stud[i].name;
result[1].percentage = stud[i].percentage;
}
else if (stud[i].percentage <= result[1].percentage && stud[i].percentage > result[2].percentage)
{
result[2].name = stud[i].name;
result[2].percentage = stud[i].percentage;
}
}
// output result
cout << "Students top print" << endl;
for (int i = 0; i < 3; i++)
{
cout << i + 1 << " " << result[i].name << " " << result[i].percentage << endl;
}
return 0;
}
Comments
Leave a comment