Home > other >  How can I sort two arrays in descending order?
How can I sort two arrays in descending order?

Time:02-21

I am trying to write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10).

I need to modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.

Example:

Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes

I have been able to sort the number of pancakes into descending order, however I am struggling to assign the correct Person to their number of pancakes.

Here is my code so far:

int main()
{
    int person[10];
    int i;
    int input;
    int n = sizeof(person) / sizeof(person[0]);

    // store the number entered by the user in each array element
    for(i = 0; i < 10;   i)
    {
        cout << "How many pancakes did Person " << i   1 << " eat? ";
        cin >> person[i];
    }

    cout << endl;
    cout << endl;

    sort(person, person   n, greater<int>()); // sorts array in descending order. "greater()" puts larger numbers first
    
    for(i = 0; i < n; i  )
    {
        cout << "Person " << i   1 << " ate " << person[i] << " pancakes." << endl;
    }

    return 0;
}

Any help would be greatly appreciated!

CodePudding user response:

You can use a std::vector of std::pair to store the Person index and its corresponding value. Then, you can use a comparator function to sort by values.

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    vector<pair<int, int>> person(10);

    // store the number entered by the user in each array element
    for(int i = 0; i < person.size();   i)
    {
        person[i].first = i   1;
        cout << "How many pancakes did Person " << i   1 << " eat? ";
        cin >> person[i].second;
    }

    cout << endl;
    cout << endl;

    auto compare = [&](const pair<int, int>& a, const pair<int, int>& b)
    {
        return a.second > b.second;
    };

    sort(person.begin(), person.end(), compare); 

    for(int i = 0; i < person.size(); i  )
    {
        cout << "Person " << person[i].first << " ate " << person[i].second << " pancakes." << endl;
    }

    return 0;
}

CodePudding user response:

You can take advantage of the sorting of the std::map.

int main()
{
    constexpr int person_num = 10;

    multimap<int, int, greater<>> pancakes_person_pairs;

    for (int i = 0; i < person_num;   i)
    {
        int cur_person = i   1;
        cout << "How many pancakes did Person " << cur_person << " eat? ";
        unsigned int cur_pancakes;
        cin >> cur_pancakes;
        pancakes_person_pairs.insert(make_pair(cur_pancakes, cur_person));
    }

    cout << endl << endl;

    for (const auto& pancakes_person_pair : pancakes_person_pairs)
    {
        cout << "Person " << pancakes_person_pair.second << " ate " << pancakes_person_pair.first << " pancakes." << endl;
    }

    return 0;
}
  • Related