Write a complete program to read student data from the standard input, sort it b
ID: 3565949 • 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 lastname , 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
My Code
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;
class student
{
public:
string fName;
string lName;
float gpa;
student() {};
student(string last, string first, double grade)
{
fName = first;
lName = last;
gpa = grade;
};
};
int main()
{
string first;
string last;
float grade;
student list[50];
int count = 0;
std::string line;
std::getline(cin, line);
std::istringstream iss(line);
cout<<"input data: ";
while ((iss >> last) && (iss >> first) && (iss >> grade))
{
list[count].lName = last;
list[count].fName = first;
list[count].gpa = grade;
cout <<"student: "<< last << " " << " " << first << " " << grade << endl;
count++;
}
for ( int i = 0; i < count-1; i++)
{
if (list[i].lName > list[i + 1].lName)
{
swap (list[i], list[i+1]);
}
}
for(int i = 0; i < count; i++)
{
cout << list[i].lName << " " << list[i].fName << " " << list[i].gpa << endl;
}
return 0;
}
My programming lab doesn't accept this answer. It tests the code and says it should out put
Explanation / Answer
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class student
{
public:
string firstName;
string lastName;
float gpa;
student()
{};
student(string last, string first, double grade)
{
firstName = first;
lastName = last;
gpa = grade;
};
};
int main()
{
string first;
string last;
float grade;
int size = 2;
student list[size];
for(int i = 0;i < size; i++)
{
cin >> last;
cin >> first;
cin >> grade;
list[i].lastName = last;
list[i].firstName = first;
list[i].gpa = grade;
}
/** Sort by last name**/
for ( int i = 0; i < size; i++)
{
for(int j = 0; j < size - i - 1; j++)
{
if (list[i].lastName > list[i + 1].lastName)
{
swap (list[i], list[i+1]);
}
}
}
/** sort by first name
for ( int i = 0; i < size; i++)
{
for(int j = 0; j < size - i - 1; j ++)
{
if (list[i].firstName > list[i + 1].firstName)
{
swap (list[i], list[i+1]);
}
}
}
**/
for(int i = 0; i < size; i++)
{
cout <<list[i].lastName <<" " <<list[i].firstName <<" " <<list[i].gpa <<endl;
}
return 0;
}
//Sample output
Executing the program....
$demo
/** Please change the size variable to support more objects **/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.