hw help (final question of semester)


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

Professor told us to use classes to solve this problem. Also like the directions say, can't use any sorting functions from libraries. Nobody in the class managed to finish this problem, I need help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>
#include <string>
using namespace std;

class Student
{
private:
string lastname;
string firstname;
float GPA;
Student Student::SortName();
{
	

};
int main()
{
	 for(int i=0; i<50; i++)
Well, you could use an array, since you know there are no more than 50 students. However, an array is slower than a linked list. So, I would use a linked list instead, as it will sort it as it goes and be a: O log(n).

Have you gone over data structures at all yet?
Well your syntax in the Student class is rather bad on that SortName() function.

but this looks like a good way of going about it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Student{
    std::string last, first;
    float GPA;
public:
    Student(std::string var1, std::string var2, float var3){
        first = var1;
        last = var2;
        GPA = var3;
    }
    std::string getNameFirst(){return first;}
    std::string getNameLast(){return last;}
    float getScore(){return GPA;}
};
/*
That's a simple class setup, remember that if you're going to sort the classes
then you need to be able to access the name, hence the get'() functions
*/


That's a little help, as for the rest I think it'd be better if you did a little more thinking yourself. I'd suggest though that you have some form of data container that you can swap orders around in, an array will do.
Then I'd setup a couple of nest loops, the most outer will keep looping until all in correct order is detected, an inner loop will cycle through the students (overlapping, i.e. 0&1, then 1&2, then 2&3, etc), and i'd have a third loop which cycles through the letters in the names, comparing by alphabetical order.
IF a letter is hit in 1st element that is later in alphabet than 2nd test element then swap them around and signal a bool type to indicate that you found an out of order area.
This bool is used in the most outer loop to check if it's finished... keep looping until this bool is false
@elite zero: tell me more about this sorting algorithm that doesn't need to check all the elements.

@OP: ¿what's your problem? ¿you don't know how to sort?
@elite zero
The best complexity possible for a sorting algorithm is O(nlogn). That too if you use fast algorithms like quicksort or mergesort.

EDIT: That said, since N <= 50, complexity is not important here and any simple sorting algorithm like bubble sort or insertion sort can be used.
Last edited on
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/

The above link will teach you all you need to know about sorting .
FYI , the std::string::operator<()/std::string::operator>() will compare the strings lexicographicaly.
Last edited on
Topic archived. No new replies allowed.