Which variable is ranked higher

Type25
Type25 used Ask the Experts™
on
Here's my situation, i have four players, all four played are asked a series of questions and their score out of 3 is recorded as well as the time it took them to answer the questions.


int p1, p2, p3, p4;
int p1Time, p2Time, p3Time, p4Time;

// fill with example data
p1 = 2, p2 = 1, p3 = 2, p4 = 2;
p1Time = 4, p2Time = 10, p3Time = 6, p4Time = 4;

Basically i need to fill a new variable string with the name of the winner, for the moment just p1,p2,p3 or p4 will be fine.

The winner is the one who answered the most questions, if there is a tie then it should then work out who the winner is against how fast they answered (lower the better)

I started with the code below but then i got kind of stuck on the best way to work out the winner based on time taken.

Thanks!
if (p1 > p2 && p1 > p3 && p1 > p4)
	{
		winner = [NSString stringWithFormat:@"p1"];
	}
	else if (p2 > p1 && p2 > p3 && p2 > p4)
	{
		winner = [NSString stringWithFormat:@"p2"];
	}
	else if (p3 > p1 && p3 > p2 && p3 > p4)
	{
		winner = [NSString stringWithFormat:@"p3"];
	}
	else if (p4 > p1 && p4 > p2 && p4 > p3)
	{
		winner = [NSString stringWithFormat:@"p4"];
	}
	else
	{
		// there's a draw
		 
		
	}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
They aren't that unrelated. Either language is fine as the principal will be the same.

Thanks

Commented:
There would be tie in between P1 and P4 as their time is same too.

Commented:
Your code looks very strange.
I think, the simplest way (at least to understand) is to make a table with your players. I the first column put the player's identifiers, and in the second - their ranks and sort the table (second column). The player who answers with a maximal number of answers is the winner.
If there few players with the same number, make another table (or use the the first one) and add the time information Sort the table.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Author

Commented:
Ok the sample data wasn't the best example, let's just presume the time taken will always be different.

I can't use a table or any type of sql (easily).

I could use an array of dictionaries i guess.

Commented:
ok. so you can use other tricks to get the winner faster (if it makes sense). I do understand that the winner has more answers and less time.
For example you can replace the time values with an index (koefficient) - maximal value is 100, minimal is 1. And add these values to the player's answers. But you may get that the winner answered on 9 questions in 10 seconds but will have a player with 10 answers in 20 seconds. Who's the winner in this case?
 

Author

Commented:
The outright winner is always the person who has answered the most correct answers, so the player with 10 questions is the winner.

Time taken only comes into play if there is a tie.

My original logic works to find out the winner, but then i need to be more clever to work out the winner from the time taken as it, more than likely, won't include all four players, so there's no good running the same logic for that as player 3 & 4 for example might have answered less questions than 1 & 2 that have tied.

If that makes any sense.

So i'm going need some kind of big if statement i think.


Author

Commented:
See it get's quite tricky when the results are like this:

Player1 = 2
Player2 = 2

Player3 = 3
Player4 = 3

So the original logic i posted will work out it's a tie, the next bit of logic needs to workout who's tied and then the time taken between ONLY those highest tied scores (so in the above scenario, only player3 &4)

evilrixSenior Software Engineer (Avast)

Commented:
Why don't you just divide the score by time taken?

So if up1 scores 8 in 10 seconds and p2 scores 8 in 5 seconds...

p1: 8 / 10 = 0.8
p2: 8 / 5 = 1.6    *winner*

So p2 wins

Commented:
So you need to sort your players - more answers in the top of the table. the subindex will be the time, so if few players have the same answers, the one with less time should be on a higher position.
it like sort by ANSWER (bigger number on the top) + TIME (smaller number on top) - whatever, I don't remember the SQL to recommend

Author

Commented:
i can't use a sql table
evilrixSenior Software Engineer (Avast)

Commented:
>> i can't use a sql table
You really don't need to make it this complex. You just need to work out who answered the most questions per second. :)

Commented:
I think evilrix has answered.
I don't know why :) but I can explain my point in this way:

struct Player
{
   int questions;
   int time;
}
int Compare(Player* p1, Player* p2)
{
  if (p1->questions > p2.questions)
        return 1;
  if (p1->questions < p2.questions)
        return -1;
  if (p1->time < p2->time)
        return 1;
  if (p1->time > p2->time)
        return -1;
       
   return 0;
}
 

Author

Commented:
evilrix, i think the pennies dropped :)


Author

Commented:
evilrix, hang on, what about this:

p1: 2 questions in 15 seconds   (2/15=0.13)
p2: 3 questions in 60 seconds   (3/60=0.05)

Your logic says player 1 won when it was player 2 as they answered more questions
evilrixSenior Software Engineer (Avast)

Commented:
>> Your logic says player 1 won when it was player 2 as they answered more questions

You only apply the questions per second logic if 2 or more players scored the same. It only makes sense if they have the same score.

Author

Commented:
ok so how do i now which scores are highest tied?
Senior Software Engineer (Avast)
Commented:
Put the users into a vector (array) rather than 4 discrete variables. Iterate through the array and keep track of the current highest score. If the current user score is equal to then current highest then push that users ID onto a stack, if the current user score greater than current highest then empty the stack and push their ID onto it. Once you've iterated all the users those ID(s) on the stack will be the highest scoring. If there is more than one ID on the stack then those users had the same score, so apply the Q/sec logic. The users ID is their index in the array of users.

http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/stl/stack/
evilrixSenior Software Engineer (Avast)

Commented:
Just to note, it doesn't have to be a stack -- any container that can easily add and remove user IDs will do... I'd just use a stack cos it's the most convenient I think :)

Author

Commented:
Really wasn't that hard was it :)

Thanks evilrix
	NSMutableArray *arr = [[NSMutableArray alloc] init];
	
	int highestScore = 0;
	for (int i=0; i<[players count];i++)
	{
	
		PlayerProfile *p = [players objectAtIndex:i];
		if ([p.currentRoundScore intValue] > highestScore)
		{
			[arr removeAllObjects];
			[arr addObject:[players objectAtIndex:i]];
			highestScore = [p.currentRoundScore intValue];
		}
		else if ([p.currentRoundScore intValue] == highestScore)
		{
			[arr addObject:[players objectAtIndex:i]];
		}
		
	}
	
	[arr release];

Open in new window

evilrixSenior Software Engineer (Avast)

Commented:
>> Thanks evilrix
Anytime

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial