Write a complete program to read student data from the standard input, sort it b
ID: 3544048 • Letter: W
Question
Write a complete program to read student data from the standard input, sort it by last name / first name , and print out the result to standard output . The student data consists of a last name , first name , and a grade point average (a floating point value ). You should assume no more than 50 students.
Note, in this exercise you will need implement your own sort-- you should not rely on an external library function to do the sort.
SAMPLE INPUT:
Ware Henry 87.2
Dantes Edmond 91.4
Earhart Amelia 92.6
Given the above input the program would write the following to standard output :
Dantes Edmond 91.4
Earhart Amelia 92.6
Ware Henry 87.2
Explanation / Answer
#include <iostream>
using namespace std;
int main(){
int n, i, j;
cin >> n;
string first[n], last[n];
float gpa[n];
for(i = 0; i < n; i++){
cin >> last[i] >> first[i] >> gpa[i];
}
for(i = 0; i - n < 0; i = i + 1){
for(j = 0; j <= i; j = j + 1){
if(last[i] < last[j]){
string ftemp = first[i];
string ltemp = last[i];
float temp = gpa[i];
first[i] = first[j];
last[i] = last[j];
gpa[i] = gpa[j];
first[j] = ftemp;
last[j] = ltemp;
gpa[j] = temp;
}
else if(last[i] == last[j] && first[i] < first[j]){
string ftemp = first[i];
string ltemp = last[i];
float temp = gpa[i];
first[i] = first[j];
last[i] = last[j];
gpa[i] = gpa[j];
first[j] = ftemp;
last[j] = ltemp;
gpa[j] = temp;
}
}
}
cout << endl;
for(i = 0; i < n; i++){
cout << last[i] << " " << first[i] <<" " << gpa[i] << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.