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

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!

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
}
```

Do more with

EXPERT OFFICE^{®} is a registered trademark of EXPERTS EXCHANGE^{®}

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.

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?

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

it like sort by ANSWER (bigger number on the top) + TIME (smaller number on top) - whatever, I don't remember the SQL to recommend

You really don't need to make it this complex. You just need to work out who answered the most questions per second. :)

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;

}

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.

http://www.cplusplus.com/reference/stl/vector/

http://www.cplusplus.com/reference/stl/stack/

## Premium Content

You need an Expert Office subscription to comment.Start Free Trial