Sort names alphabetically

43.4k Views Asked by At

I'm trying to sort names alphabetically e.g If user enters names and GPA:

Names          GPA
Peter          2.8
Robert         5.6
David          7.8

The output should be : -

Names          GPA
David          7.8
Peter          2.8
Robert         5.6

Here is my program so far (INCOMPLETE):-

#include <iostream>
using namespace std;

int main()
{
    char name [5][25];
    float gpa [5];
    int i;

    for (i=0 ; i<5 ; i++)
    {
        cout << "Enter name " << i+1 << "  :   ";
        cin >> name [i];
        cout << "Enter GPA     :   ";
        cin >> gpa [i];
        cout << endl;
    }

    cout << "\n********** Your entered data **********\n\n";

    cout << "\tName" << "\t\t" << "GPA\n\n";

    for (i=0 ; i<5 ; i++)
    {
        cout << "\t" << name [i] << "\t\t" << gpa [i];
        cout << endl;
    }

    for (i=0 ; i<5 ; i++)
    {
        for (int j=0 ; j<1 ; j++)
        {
            cout << (int) name [i][j] << endl;



        }
    }

    cout << "\n\n******* Sorted data (w.r.t name) *******\n\n";

    cout << "\tName" << "\t\t" << "GPA\n\n";

    for (i=0 ; i<5 ; i++)
    {
        cout << "\t" << name [i] << "\t\t" << gpa [i];
        cout << endl;
    }

    cout << endl;

    return 0;
}

Remember, only name should be sorted alphabetically. I have taken the ASCII values of the first characters of entered names in the middle for loop but:- 1- ASCII code for 's' is not the same as 'S' (That's a problem for me) 2- I can't seem create a logic to compare the ASCII values of the first letters of names then sort them accordingly. Then afterwards linking the name with the sorted letter list and displaying the result. Also the GPA should be linked with the names.

Any help would be appreciated.

4

There are 4 best solutions below

4
On

If you convert the names' character to upper-case using std::toupper you should then just be able to compare the the strings using < operator.

Edit: if you don't want to use std::sort :-)

4
On

Here's an answer using std::sort. I changed some of your C-like approach and using std::sort actually forces me to do it. The comparison function(compareStudents) needs objects so I had to create the struct. Vector has been used for the same reason although it would have been possible to keep using arrays but that's generally frowned upon.

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

struct Student {
    string name;
    float gpa;

    Student(string name, float gpa) {
        this->name = name;
        this->gpa = gpa;
    }
};

bool compareStudents(Student a, Student b) {
    return a.name.compare(b.name) < 0;
}

int main() {
    const int studentCount = 2;
    vector<Student> studentVector;
    int i;

    for (i = 0 ; i < studentCount ; i++) {
        cout << "Enter name " << i + 1 << "  :   ";
        string name;
        cin >> name;
        cout << "Enter GPA     :   ";
        float gpa;
        cin >> gpa;
        cout << endl;

        studentVector.push_back(Student(name, gpa));
    }

    cout << "\n********** Your entered data **********\n\n";

    cout << "\tName" << "\t\t" << "GPA\n\n";

    vector<Student>::iterator it = studentVector.begin();
    for (; it != studentVector.end(); ++it) {
        Student student = *it;
        cout << "\t" << student.name << "\t\t" << student.gpa;
        cout << endl;
    }

    sort(studentVector.begin(), studentVector.end(), compareStudents);

    cout << "\n\n******* Sorted data (w.r.t name) *******\n\n";

    cout << "\tName" << "\t\t" << "GPA\n\n";

    it = studentVector.begin();
    for (; it != studentVector.end(); ++it) {
        Student student = *it;
        cout << "\t" << student.name << "\t\t" << student.gpa;
        cout << endl;
    }

    cout << endl;

    return 0;
}
0
On
    #include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
class stu
{
char name[40];
int cgpa;
public:
void assign()
{cin>>name;
cin>>cgpa;
}
void display()
{cout<<name<<endl<<cgpa<<endl;
}
friend void align(stu *s,int v);
};
void align(stu *s,int v)
{for(int j=0;j<v-1;j++)
{for(int i=0;i<v-j-1;i++)
{//buble sort
if(strcmp((s+i)->name,(s+i+1)->name)>0)
{char l[40];
strcpy(l,(s+i)->name);
strcpy((s+i)->name,(s+i+1)->name);
strcpy((s+i+1)->name,l);
//swapping cgpa
int t=(s+i)->cgpa;
(s+i)->cgpa=(s+i+1)->cgpa;
(s+i+1)->cgpa=t;
}
}}}

int main()
{stu s[10];int i;
for(i=0;i<5;i++)
s[i].assign();
align(s,5);
cout<<endl<<endl;
for(i=0;i<5;i++)
s[i].display();
}
0
On
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    //zbuduj program wyswietlajacy w kolejnosci alfabetycznej 5 wczytanych imion
    string wyrazy[5];
    int c;
    for(int i=0; i<5;i++){
        c= i +1;
      cout<<"podaj imie "<<c<<"; ";
      cin>>wyrazy[i];
    }

    sort(wyrazy, wyrazy+5);

    cout<<"posortowane"<<endl;
    for(int b=0; b<5;b++){
      cout<<wyrazy[b]<<endl;
    }
    return 0;
}