The loop below is the best algorithm/syntax I could come up with to establish
which player has the greatest score out of any number of 4 players. Is there an
easier way to write this? Thanks for any response!
Explanation in case of any confusion: for each [if/ else if] each player's score
is matched up with the other 3 players score. ex:
if player 1's score...say 1500 minus player 2's score...say 1200 is greater than 0...then player 1's score is the highest....
I like your method, but it doesn't account for someone to get a negative score, which may not be possible in your project, and there is no way I can tell to see who is second highest, or third and so on...
The one below is a very simple way to do it, if there was more than 4 players I would look at a loop.
In a single game, Player1 is always high score until compared to player 2 and so forth. In a tie, player one's score is pushed down, you could do it the other way and say the first to get high score stays on top. The code below assumes there may already be a high score recorded. If not, just set highscore1 = player1 every time. Then see if 2 3 or 4 is higher.
#include <iostream>
using namespace std;
int main ()
{
int i = 0;
int HighScore1 = 0;
int HighScore2 = 0;
int player1 = 109;
int player2 = 108;
int player3 = 107;
int player4 = 106;
if (player1 >= HighScore1)
{HighScore2 = HighScore1;
HighScore1 = (player1);}
else if (player1 >= HighScore2)
{HighScore2 = (player1);}
if (player2 >= HighScore1)
{HighScore2 = HighScore1;
HighScore1 = (player2);}
else if (player2 >= HighScore2)
{HighScore2 = (player2);}
if (player3 >= HighScore1)
{HighScore2 = HighScore1;
HighScore1 = (player3);}
else if (player3 >= HighScore2)
{HighScore2 = (player3);}
if (player4 >= HighScore1)
{HighScore2 = HighScore1;
HighScore1 = (player4);}
else if (player4 >= HighScore2)
{HighScore2 = (player4);}
To just get highscore, just change player1 to high score;
highscore1 = player1
if player2 > highscore1 highscore1 = player2
else if player3 > highscore1 highscore1 = player3
else if player4 > highscore1 highscore1 = player4
#include <iostream>
using namespace std;
int main ()
{
int HighScore = 0;
int player1 = 109;
int player2 = 109;
int player3 = 111;
int player4 = 106;
Pretend you had a list of ten thousand different numbers written on a piece of paper and THE MAN* was making you go through the entire list (of TEN THOUSAND numbers) to figure out which number is the largest. How would you do it as a human being? Would you make any pen marks anywhere?
I think you would. As you go down the list you would keep a mark next to the largest number you've seen thus far, and for each number you look at you would compare it to the most recent number with a mark next to it. If the number you're looking at is larger, you would then mark that number as the new max.
This is the basic algorithm to find the max value in a list. All you need is a loop, an if statement, and a variable that holds the max value. That variable should be initialized to 0 though so that you have a number to compare against when you compare your first number in the list to the max.
*or woman
EDIT: You should probably also have a variable that knows which player has max value. Like int playerWithHighScore.