Sunny Spectral
Sunny Spectral

Reputation: 1

Display their names in ascending order with respect to their ages?

The stated problem is "Write a program that takes names and ages of 10 employees as input in a 2D char array and display their names in ascending order with respect to their ages".

I've pasted below what I have so far, but I don't know how to display the names in ascending order with respect to age. What am I missing?

#include<iostream.h>
#include<string.h>
void sort(int[],int);
void main(void)
{
    char a[10][5],x;
    int b[5],i,j;
    for(j=0;j<=5;j++)
    {
        cout<<"Enter name of Employee:";        
        for(i=0;i<10;i++)
        {

            cin>>x;
            x=a[j][i];
        }
        cout<<"Enter Employee's age:";
        cin>>b[j];
        cout<<endl;
    }
    sort(b,j);
    cout<<"Name of Employee \t\t Age \n";
    for(j=0;j<5;j++)
    {
        for(i=0;i<10;i++)
        {
            x=a[j][i];
            cout<<"\t"<<x;
        }   
        cout<<"\t\t"<<b[j]<<"\n";
    }
}
void sort(int x[],int size)
{
    int temp,i,j;
    for(j=0;j<size;j++)
    {
        for(i=0;i<size;i++)
        {
            if(x[i]>x[i+1])
            {
                temp=x[i+1];
                x[i+1]=x[i];
                x[i]=temp;
            }
        }
    }
}

Upvotes: 0

Views: 12098

Answers (4)

yezina addisu
yezina addisu

Reputation: 1

#include <iostream.h>

int main() {
    int a;
    int b;
    int sum;

    std::cout << "Enter two numbers" << std::endl;

    std::cout << "Enter the value of a" << std::endl;
    std::cin >> a;

    std:: cout << "Enter the value of b" << std::endl;
    std::cin >> b;

    sum = a + b;
    std::cout << sum << std::endl;

    return 0;
}

Upvotes: -2

Sebastian
Sebastian

Reputation: 8164

Use a std::multimap (a key/value store, fitting your needs), which is weakly sorted ascending by default. Your key is the age and the value is the name. For printing the values just use iterators. std::multimap is chosen because there could potentially be employees with the same age.

Upvotes: 0

111111
111111

Reputation: 16168

I will point you in the right direction.

You should define struct to hold a Name and age pair, then make a comparison function.

Then you simply need to populate a vector of your structs then sort them using the sorting function provided by the standard library, iterate through the vector printing out the contents, you can define stream operator of the struct you made to simplify them.

http://www.cplusplus.com/doc/tutorial/structures/

http://en.cppreference.com/w/cpp/algorithm/sort

http://en.cppreference.com/w/cpp/container/vector

http://www.java2s.com/Code/Cpp/Overload/Overloadstreamoperator.htm

EDIT: and please for all that is good, use a std::string to hold the names.

http://www.cplusplus.com/reference/string/string/

using an char array like you are is pretty much deprecated.

I started writing this before the other solution, because I would rather see what C++ is capable of not what your lecture is feeding you.

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

struct employee
{
    employee(const std::string& name_, unsigned age_)
    : name(name_), age(age_) {}

    std::string name;
    unsigned age;
};

std::ostream& operator<<(std::ostream& os, const employee& e)
{
    os << e.name << "   " << e.age;
    return os;
}

bool comp_age(const employee& e1, const employee& e2)
{
    return e1.age<e2.age;
}


int main()
{
    std::vector<employee> employees;
    employees.reserve(5);

    for(unsigned i=0; i!=5; ++i)
    {
        std::string name;
        unsigned age;

        std::cout << "Name:" << std::flush;
        std::cin >> name;
        std::cout << "Age:" << std::flush;
        if(!std::cin >> age) 
        {
            std::cerr << "not an number" << std::endl;
            --i;
            continue;
        }
        //note you should catch any failure to parse to int here

        employees.push_back(employee(name, age));
    }

    std::sort(employees.begin(), employees.end(), comp_age);

    std::copy(  employees.begin(), employees.end(),
                std::ostream_iterator<employee>(std::cout, "\n"));

    return 0;
}

So this is an alternative, but PLEASE learn the concepts I list above which constitutes this example.

Upvotes: 2

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234614

You should start by changing void main to int main, because void main is not valid C++. Then you change the #includes to be real C++ header files not what you have:

#include <iostream>
#include <string>

Then you should give meaningful names to your variables. It's impossible to follow the code in a sea of as and bs.

Then you need to sort the employees by their age. You already have a sorting algorithm written there, so you only need to adapt it to swap all the employee data instead of swapping just their ages, while still doing the comparisons only on their age. This will be easier if instead of two separate arrays for the employee data, you kept instead a single array of a structure that holds both the name and age of the employee:

struct employee {
    std::string name;
    int age;
};

Once sorted, you can just loop through all the employees an print their names.

I believe there are a few more issues in the code, but they should be easy to fix with a good reference and some debugging time. If you have trouble, you can post a new question with what you've got so far, and explain what is stumping you.

This would all be much better if you just used the C++ standard library, but unfortunately some teachers decide to teach Crippled++ to their students instead of proper C++ :(

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

// treat an employee as a single unit
struct employee {
    std::string name;
    int age;
};

// a comparison function to compare two employees by their age
// should return true if the first one is younger than the second one
// this will be used for sorting later
bool is_younger(employee const& l, employee const& r) {
    return l.age < r.age;
}

int main()
{
    // a vector with 5 employees
    std::vector<employee> employees(5);
    for(j=0;j<=5;++j)
    {
        std::cout << "Enter name of Employee: "
        // read the entire name at once 
        getline(std::cin, employees[j].name);

        std::cout << "Enter Employee's age:";
        // read the age
        std::cin >> employees[j].age;
    }

    // sort all the employees using the comparison function written above
    std::sort(employees.begin(), employees.end(), is_younger);

    std::cout<<"Name of Employee \t\t Age \n";
    for(j=0;j<5;++j)
    {
        std::cout << employees[j].name;
        std::cout << "\t\t" << employees[j].age << "\n";
    }
}

Upvotes: 1

Related Questions