Silver Falcon
Silver Falcon

Reputation: 176

sorting 'age' of names entered then displaying the result

I want to sort age in ascending order of respective entered names and then display the list according to that. I have managed to sort them but I can't seem to display the names correctly w.r.t their age.

Here is my code :-

#include <iostream>
using namespace std;

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

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

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

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

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

    int hold;

    for (i=0 ; i<5 ; i++)
    {
        for (int j=0 ; j<5 ; j++)
        {
            if (age [j] > age [j+1])
            {
                hold = age [j];
                age [j] = age [j+1];
                age [j+1] = hold;
            }
        }
    }

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

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

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

    return 0;
}

Problem is with the last for loop. Can anyone of you help me implementing this logic?

Upvotes: 0

Views: 2190

Answers (4)

midnight hunter
midnight hunter

Reputation: 11

Since age typically varies from 1 to 120 :) , we can use this to our advantage. Use a mapping technique to map the age to an array index. It will speed up sorting considerably when your data set expands. I've demonstrated this for 5 people , but it can easily be modified to work for large data sets by chaining linked lists.

#include<cstring>
#include <iostream>
using namespace std;

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

char ary[120][25]; // The array used to map.

for(int i=0;i<120;i++) 
strcpy(ary[i],"");     //initialize to ""

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

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

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

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

//The sorting , hashing actually 
for (i=0 ; i<5 ; i++)
{
    strcpy(ary[age[i]],name[i]);
}

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

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

for (i=0 ; i<120 ; i++)
{
    if(strcmp(ary[i],""))
    cout<<ary[i]<<" "<<i<<"\n";   // The index will be equal to age
}

return 0;
}

Upvotes: 1

Jatin Khurana
Jatin Khurana

Reputation: 1175

Also swap the corresponding names with age.

Also one more suggestion, Whenever you have such linked data, It's better to use the structure because It make sense to keep the relative data as a group.

And also if you swap them, all the data will be swapped so would not get this type of issues.

Upvotes: 1

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153955

When you sort the array with the ages (age) the connect between the ages and their names gets list: prior to sorting, the name and the corresponding age where stored at the same index.

To fix the problem you can do, e.g., one if these approaches:

  1. When swapping two ages during sorting also swap the two names.
  2. Use an auxiliary array with pointers or indices indicating the names and swap that in addition to the ages (similar to the first approach but avoiding to swap strings). When printing you'd find the relevant name through this auxiliary areay.
  3. Store a pair of name and age in the array and swap these pairs but use only the age part to determine the sort order.

Upvotes: 1

doptimusprime
doptimusprime

Reputation: 9413

Change your swap logic. Swap both name and age.

    char holdstr[25]; ///Temporary string to swap.

if (age [j] > age [j+1] || (age[j] == age[j+1])
{
    //Swap the age and name both.
    hold = age [j];
    strcpy(holdstr, name[j]);

    age [j] = age [j+1];
    strcpy(name[j], name[j+1]);

    age [j+1] = hold;
    strcpy(name[j+1], holdstr);
}

You should wrap condition inside in if to a function so that you can change your logic easily.

Upvotes: 1

Related Questions