Rank Array

I'm trying to make a function to rank all the values in an array of doubles.

For example, I'd like [0.003,0.001,0.002] to be returned as [1,3,2].

I'm having trouble figuring out where to get started.

I've tried this, but it doesn't work and I know it isn't the most efficient way of thinking about it:

1
2
3
4
5
6
7
8
	for(int i=0;i<tableLength;i++){
		rank=0;
		for(int z=0;z<sizeof(table);z++){
			if(table[z]<table[i])
				rank++;
		}
	result[i]=rank;
	return result;


Can anyone point me in the right direction?
closed account (4Gb4jE8b)
Now this is a pretty complex idea, but i can see the usefulness, so i'll try and help as best as possible. Next post, show the initialization values of table, rank and result.

You're going to have compare each individual number, so is the number loaded from a document or do you enter the numbers or are the numbers part of the code itself?

if it's incode, it'll be pretty easy, instead of having them in an array, have them each individual doubles and compare using a series of
bool checker{if(x>y){return true;}}
(sorry it's all one line, but i'm typing this directly here, and don't feel like formatting)
to create an order and follow it up with the appropriate cout statements.

if you enter the numbers, store them in a individual double again and follow the same steps.

And if what i fear worst is true, and it's loaded from a document, then the previous method is incompatible (at least to my knowledge), and something similar to what you're doing will have to be done.

As for why this specific program isn't working; what's the output you're getting? It seems like rank should be a trait of the individual numbers... but again without the data type of rank, i can't say.
The entire program is pretty large, but I do pull these numbers out of a fixed-width text document.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
            getline(inFile,line,'\n');
            if(line.size()==91){
                name=line.substr(0,16);
                hits=atoi(line.substr(32,3).c_str());
                ab=atof(line.substr(28,3).c_str());
                runs=atof(line.substr(48,3).c_str());
                hrs=atof(line.substr(44,3).c_str());
                rbi=atof(line.substr(52,3).c_str());
                sb=atof(line.substr(64,3).c_str());
                avg=hits/ab;
                if(name.compare(prevname)!=0){
                    i++;
                    name_ref[i]=name;
                    runs_rnk[i]=runs;
                    hrs_rnk[i]=hrs;
                    rbi_rnk[i]=rbi;
                    sb_rnk[i]=sb;
                    avg_rnk[i]=avg;


(and yes, this is baseball). Now I want to take the arrays (such as runs_rnk[i]) which has about 800 entries, and each entry inside it ranked.

I know the code I wrote was vastly inefficient, but could that kind of logic work?

closed account (4Gb4jE8b)
because i'm not seeing enough information myself, go to http://www.filedropper.com/ and upload it and post your link to your cpp in question and any personally written header files necessary to compile your cpp.
There's enough information.

All I want to do is take an array such as runs_rnk which looks like [400,200,300...] to be returned ranked as [1,3,2...].

I'm guessing there is a quick solution that would work here, I don't really mind if it's inefficient.
closed account (4Gb4jE8b)
if there is a quick solution, i honestly don't know it.
I solved it myself. This seems to work pretty well:

1
2
3
4
5
6
7
8
9
10
11
12
int* rank(double table[],int tableLength){
    int result[tableLength];
	for(int i=0;i<tableLength;i++){
		int rnk=0;
		for(int z=0;z<tableLength;z++){
			if(table[z]<table[i])
				rnk++;
		}
    result[i]=rnk;
	}
    return result;
}


Basically, it iterates through each item in the table, and then iterates through the table again and counts which values are greater than it.
Topic archived. No new replies allowed.