You should try the binary sort algorithm.
Bubble sort works great, but it needs to check the whole array again and again.
I know that for something like this there's no need for improvement, but, it could be interesting to take a look at.
Anyways,
If you start a sort algorithm It will change the place of the scores, but it won't take the position with it. Here's an example of it:
So let's say you have an array like this:
{12, 30, 30, 120, 49, 56, 23, ...} with 12 being the score of the 1st game you read and 23 the score of the 7th game.
If you sort the scores from high to low you'll have something like this:
{120, 56, 49, 30, 30, 23, 12, ...}
So now, the score of the 1st game is 120 and that's
not correct, that's
not what you want to do.
(Correct me if I'm wrong, but that's what I understood from your posts)
What you would like to do is:
- Read 12 scores and store them into some array
- Check the array for highest score and store the
position of the highest score into another array, and the value of the highest score into another one.
- Check the array for 2nd highest score ...
- Check the array for 3rd highest score ...
You don't have to use any sort algorithm for that as you want to know
who or better said,
which game has the highest score. Is it the 2nd score you read or the 11th one?
I'd see something more like this :
Code: Select all
char i = 0, j = 0, dummy_var = 0;
char read_scores[12] = {0};
char highest_score[3] = {0};
char position[3] = {0};
// read scores and store them in read_scores array
for (i = 0; i < 3; i++) // loop 3 times to get the position and value of the 3 highest scores
{
for (j = 0; j < 12; j++) // loop 12 times to check the whole array for the highest score
{
// check if the score on position 'j' in the read_score array is higher than the value of the dummy variable
if (read_scores[j] > dummy_var)
{
position[i] = j; // if so store the value of 'j' (the position) into the position array
dummy_var = read_scores[j]; // store the value of the score into the dummy_var
}
}
highest_score[i] = read_scores[position[i]]; // store the value of the highest score into the highest_score array
read_scores[position[i]] = 0; //delete the value of the highest score so you won't read the same position twice
dummy_var = 0; // reset the dummy var
}
So what do you want to do with that ? Send to the PC, or print it on a LCD screen ?
I'll show you how to write on the LCD.
The functions I'll use will not be correct, but you'll understand it to program that into your flowcode file.
Btw, every piece of code I have written until now can be done without any C-block icon (or whatever it's called), so it should be possible to simulate everything
.
Code: Select all
for (i = 0; i < 3; i++)
{
LCD_ClearScreen(); // clear the screen
LCD_SetCursor(0, 0); // set the LCD cursor on x=0 and y=0
LCD_PrintString("Game : "); // write the string "Game : " on the LCD screen
LCD_PrintNumber(position[i]); // write the number of the game
LCD_SetCursor(0, 1); // set the LCD cursor on x=0 and y=1
LCD_PrintString("Score : "); // write the string "Score : " on the LCD screen
LCD_PrintNumber(highest_score[i]); // write the highest score of that game
// wait 10 second before doing anything else, so you have the time to read who won and with how much points
delay_s(10);
//You could do this differently but, ok, that's the (easy) way I'd do it.
}
So this piece of code will write on the LCD screen something like this (it's an example):
Game : 4
Score : 125
That's it. No bubble sort algorithm. Just basic if-statements with for loops and some variables and arrays
.
Rgds,
Nicolas L. F.