Home > Software design >  How can I put the indexs in descending order in C without using std?
How can I put the indexs in descending order in C without using std?

Time:12-03

The results contain an index like 0,1,2,3,4,5,6,7 and these are the number of the players and I use cin to get their points like 10,0,2,0,9,0,23,0. I would like to cout the number of the players in descending order when their point are not 0. the result must be 6,0,4 2. I only can make the points in descending order. How can I make this?

#include <iostream>

using namespace std;

int main ()
{ int results[8];

    for (int i=0; i<N;i  )
    { 
        cin >> results[i];
    } 
        
    int change;
    for( int i = 0; i < M-1; i   )
    {
        for( int j = i 1; j < M; j   )
        {
            if( results[i] < results[j] )
            {
                change = results[i];
                results[i] = results[j];
                results[j] = change;
            }

         }
    }
return 0;
}

CodePudding user response:

You make your life 10 times easier if you create a structure to hold the player id and score:

struct Player
{
    int id;
    int score;
};

Then all you need to do is sort and skip scores of 0 in the output:

int main()
{
    std::vector<Player> players{
        {0, 10},
        {1, 0},
        {2, 2},
        {3, 0},
        {4, 9},
        {5, 0},
        {6, 23},
        {7, 0}
    };

    std::sort(players.begin(), players.end(),
             [] (auto p1, auto p2) { return p1.score > p2.score; });

    for (const auto& player : players)
    {
        if (player.score != 0)
            std::cout << player.id << " ";
    }

    std::cout << std::endl;
}
6 0 4 2 

CodePudding user response:

If you can't use std (other than cin and cout) this turns pretty much into C code:

  • Read each input, filtering out zero values.
  • For non-zero inputs, walk the results array, use the index in the results array to access the value in the inputs array, and stop if that value is smaller than the current input.
  • Keep that results index, j, as it will be the position where you will have to place the current input's index.
  • Move all elements from that index until the end of the results array up one position (this is, towards the end of the array).
  • Write the current input's index, i, at results[j].

[Demo]

#include <iostream>  // cin, cout

int main ()
{
    int inputs[8];
    int results[8];
    size_t results_size{0};

    for (int i = 0; i < 8; i  )
    {
        std::cin >> inputs[i];

        int current_input{inputs[i]};

        // Nothing to do with inputs equal to zero
        if (current_input == 0)
        {
            continue;
        }

        // Kind of upper bound in results
        int j{0};
        for (; j < results_size and current_input <= inputs[results[j]]; j  );

        // Make room for new index in results
        for (int k = results_size; k > j; k--)
        {
            results[k] = results[k-1];
        }

        results[j] = i;
        results_size  ;
    }
    
    for (int i = 0; i < results_size; i  )
    {
        std::cout << results[i] << " ";
    }
    std::cout << "\n";

    return 0;
}

  • Related