Okay, I need my output to be numbered.
I know the index code works but idk how to use that.

However, i also need to know how to do the if/else statement,
so that if the output is "0", then they dont get an index, they just recieve "0"

Dani AI

Generated

Short answer: separate zeros, sort the non-zero entries by score (highest first), then assign ranks 1..N to that sorted list while leaving zero-score entries with rank 0. This follows 's suggestion and extends 's indexing idea into a full ranking routine.

Example (C++): put this after you have your scores in a vector (names optional).

#include <vector>
#include <algorithm>
#include <numeric> // iota

// scores[i] is the score for item i
std::vector<int> scores = { 72, 0, 95, 72, 10, 0 };

// collect indices with non-zero scores
std::vector<int> nonzero;
for (int i = 0; i < (int)scores.size(); ++i)
    if (scores[i] > 0) nonzero.push_back(i);

// sort indices by score descending (use stable_sort if you want original order for ties)
std::sort(nonzero.begin(), nonzero.end(),
          [&scores](int a, int b){ return scores[a] > scores[b]; });

// assign ranks: ties get the same rank (standard competition ranking: 1,2,2,4)
std::vector<int> rank(scores.size(), 0); // defaults to 0 for zero scores
for (int pos = 0; pos < (int)nonzero.size(); ++pos) {
    int idx = nonzero[pos];
    if (pos > 0 && scores[idx] == scores[nonzero[pos-1]])
        rank[idx] = rank[nonzero[pos-1]]; // same rank as previous
    else
        rank[idx] = pos + 1; // rank starts at 1 for highest score
}

Notes: zeros remain rank 0 automatically. If you prefer "dense" ranks (1,2,2,3) change the else branch to increment a separate denseRank counter instead of using pos+1. Use stable_sort when equal scores should keep their original relative order.

Recommended Answers

All 6 Replies

There's absolutely no context to your question, and as such, it's nonsensical.

Member Avatar for Member #46692

Presumably you mean you have something like:

string array[]={"boo","hoo","shoo"};

And you as you print it you want to print their indexes, i.e

[1] boo
[2] hoo
[3] shoo

In that case you can do

for (int i = 0; i < max; i++ )
{
cout << "[" << i+1 << "]" <<array <<endl;
}

But like narue said it is hard to tell what you mean.

Ok.

I have scores as my output,
and I want to descendingly number them down to 1.
Like as if I were ranking them.

However, if the score is "0",
I want them to be ranked as "0".

Make sense?

ok. How do I do the "if" statement for the people who received "0". I want to rank them as "0" instead of 1-20.

Member Avatar for Member #46692

Can you post your code so far plz.

>Make sense?
Yes, and I'm reasonably sure I've described how to do this to you before. Anyway, it makes more sense in this case to separate the scores that are zero into a separate list. This trivializes the rankings because you can just take the number of non-zero scores and rank them down from that number, then fill out the zero scores with "0".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.