0

I'm trying to show a ranking list of my array qt, which contains 5 numbers.

int i, j;
int qt[5] = {10,20,10,50,20};
int tempqt;
for (i=0; i<5; i++)
{
    for(j=(i+1); j<5; j++)
    {
        if (qt[i] >= qt[j])
        {
            tempqt = qt[i];
            qt[i] = qt[j];
            qt[j] = tempqt;
        }
    }
}

for(i=0; i<5; i++)
{
    cout << i+1 << ".number: " << qt[i] << endl;
}

normally, the 2 for-loops sort my array and the last for-loop displays my array ordered, so it looks like this:

  • 1.number: 10
  • 2.number: 10
  • 3.number: 20
  • 4.number: 20
  • 5.number: 50

But I want to display the numbers with the same value as the same ranking position, so like this:

  • 1.number: 10
  • 1.number: 10
  • 2.number: 20
  • 2.number: 20
  • 3.number: 50

3 Answers 3

2

The idea is to increase rank counter when meet different value in qt array.

i = 0;
int rank = 1, val = qt[i];
cout << rank << ".number: " << qt[i] << endl;

for(i=1; i<5; i++)
{
    if (qt[i] != val) {
        ++rank;
        val = qt[i];
    }
    cout << rank << ".number: " << qt[i] << endl;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Use std::sort to sort the array -- you won't get anywhere until the array is sorted.

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
   int qt[5] = { 10, 20, 10, 50, 20 };
   sort(qt, qt + 5);
   int count = 1;
   for (int i = 0; i < 5; ++i)
   {
       if (i > 0)
       {
          if (qt[i] != qt[i - 1])
             ++count;
       }
       cout << count << ".number: " << qt[i] << endl;
    }
}

Here is another solution using a map. This is more "lazy" in that there is no real "check if number already seen" logic involved. Just add numbers to a map, and print out the results in a loop.

If there are no memory constraints (you will need to create a map of the numbers, of course), and/or you need the array to remain stable (not sorted), then this could be an alternative.

#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int qt[5] = { 10, 20, 10, 50, 20 };
    std::map<int, int> IntMap;

    // add entries to map, adding to a counter each time
    for (int i = 0; i < 5; ++i)
       IntMap[qt[i]]++;

    // output the results.
    int count = 1;
    for (auto it = IntMap.begin(); it != IntMap.end(); ++it, ++count)
    {
        for (int i = 0; i < it->second; ++i)
            cout << count << ".number: " << it->first << endl;
    }
}

The map already sorts, so that's taken care of. Then the map is set up to count the number of times each number shows up, so that's taken care of. The only thing left is to write a loop that just goes through the map and prints the information.

See it here: http://ideone.com/q08SeX

1 Comment

Thanks for sharing 2 solutions :)
1

I'd rather use a do while loop:

int p = 1, x = 0;
do 
{
    cout << p << ".number: " << qt[x++] << endl;
    if (x < 5 && qt[x] != qt[x-1])
        p++;
} while (x < 5);

4 Comments

this is UB when x = 4 at the start of the loop
the loop is now infinite
My mistake. I've used << qt[x++] << name[x++] later (to display a number and a name of a person). Didn't know that x++ actually increases the x value within the [].
@Rudi Actually, x++ increases x sometime before the next sequence point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.