m5stack EuroMillions APP help

Hello.
I'm desenvolving an APP for EuroMillions Lottery in my m5stack and I have some troubles with it and I hope someone will help me.
I sort n times a number between 1 and 50 (the Euromillions Lottery range of numbers). Every time a number is sorted, an array with 50 elements is incremented at the number sorted position. If 5 is sorted, then MatrixNumber[5]=MatrixNumber[5]+1. After n times I will seek the MatrixNumber[] and search for the biggest number there and the corresponding position where I found it and save the position in another array (FinalNumber[]). After that I zeroe the position so that it only be only one maximum values for all the array. (are you lost? I hope not). After all is done I get an array with 5 numbers (the most sorted by function "random(50)+1". I do the ssame stuff for the "Stars" part of the sorting.
Problem ONE: I can't SORT the array from lesser to bigger. I've done everything and it just don't sort.
Problem TWO: In "option" I use a number that can be "2" to "+2". That number will be added the the "sorted number" comparing his value. For example, variable "Option1" value will be added if a number is sorted between 1 and 6 and "Option2" value will be added to numkber sorted if the number is between 7 and 12, ... and so on (1..6,7..12,13..18,... are the lines on the lottery form). If I change the number to any row (row 1 to 8) beside value "00", the sorting is giving me almost always the same numbers. If I put the rows to position value of "00" then it's random again.
Very weird.
I copied the source code in pastebin because it's kind of big and I want to share the code anyway (but not with weird stuff on it).
https://pastebin.com/LBpKVPcv
Can anyone help? 
Hello,
I have looked at your programm and I really don't understand what you are trying to do :
Storing and retrieving datas from eprom ? Displaying battery levels ? Randomly reading keypresses ? Generating negative numbers (with signed char) ? Mixing char, integers and long integers ? Some weird sorting scheme ?So what is the goal of your app ? You are only talking about some weird sorting problems where nobody understands what you want to do.
For each line in the euromillions lottery you have to enter 5 different numbers between 1 and 50, and 2 different numbers for the stars between 1 and 12.
Probably you want to generate the 5 numbers and the 2 stars with a random number generator and display it on your m5Stack lcd. So your programm would look like:
 Setup : Start the (pseudo) random number generator (seed)
 On a keypress: Generate 5 numbers and 2 stars:
 generate 5 numbers between 1 and 50; as long as 2 numbers are equal replace it with a new random number; sort the numbers low to high eg. by bubble sort; display the numbers;
 generate 2 (star)numbers between 1 and 12; as long as the two numbers are equal replace one with a new random number; if number 2 < number 1 switch the numbers; display the numbers on the lcd.
If you are new to programming with arduino and C++ instead of trying to copy and paste some demo codes, try to programm your app with blockly/(Micropython) : flow.m5stack.com .
There you can generate random numbers, lists with 5 and 2 elements and do the sorting with the logic elements and display the results (as labels) on your M5Stack lcd,Good luck!
crami25 
@crami25 Hello. Thank you for replying. The system of the 2..+2 is simple. A number random is sorted: 5 for exemple. I play EuroMillion with that number and 4 more sorted. The next week the numbers sorted are displayed and I realise that "6" has been sorted instead of "5" (the number I played the week before. Simce numbers from 1 to 6 are in the row 1 of the EuroMillion form, I go to OPTIONS and select "+1" in row 1. NOW, if a number is sorted like "3" for exemple, the program is showing me 3+(+1)=4. You get the idea? All rows have that tweak (all but not last row 49..50). Those "tweaks numbers" are saved in eeprom so everytime I start the app the tweak numbers are as I want them. THE BATERY, since I was using the m5stack for this app I thought that it will be nice to see HOW MUCH battery is remaining. KEYPRESSED are not random. I wait for keypress and depending WHERE in the program I AM (I mean what SUB is active). I haven't managed to save negative values as byte (signed char in this case) in eprom so I convert the values of the ROW TWEAKS from 2 to +2 into a simple 1 to 5 system and do the inverse when Reading eprom. The sorting is Bubble sort but doesn't seem to work and THAT is a weird problema. I hope I have dissipate all your doubs so you can try to help. Thanks

I still don't understand what you want to do ....maybe you could explain it in your native language.
You are always speaking of sorting and you are using the verb for different purposes:
sorting : sort 5 numbers so the lowest is displayed first and the highest last (5,7,28,35,47)
tip : the 5 numbers you want to fill into the euromillion form
draw: the 5 numbers which are drawn in the euromillion lotteryyou probably use sorting for all three different arrays of values
I also don't understand your tweak 2..+2 system: The numbers drawn have nothing to do with the rows in the euromillion form. If the lowest number you tip is a 5 and the lowest number drawn is 15 your tweak according your explanation would be 10 ???
The type of the euromillions forms probably depends on the country you are playing:
In Spain, Germany the first row has the numbers 15 (not 6) and in France 110 !In my opinion the rows in the form have nothing to do with the the numbers drawn and and the numbers you have tipped: You have to tip 5 different numbers from 150. The rows of the form is only a convenient way displaying the numbers (150 in ten rows of 5, 150 in five rows of 10, 150 in seven rows of six and a last row of the remaining 49 and 50). This country dependend formscheme is only used to be read by the scanner entering your tip into the euromillions lottery.
Crami25

@crami25 Hello. Thanks. Sorta was used for TIP. 2 ... +2 will be dificult to explain. It only make sense if the number DRAWN if at lenght of 2 or 2 of the number TIPPED. Forget that. What puzzle me (and you can only see it if you put the program in your m5stack) if when all options are "00" numbers are drawn randmly but if you change any of the options to 2 ... 2, the numbers are repeating. It's weird. Can you put the program in your m5 and explain the weird behaviour? Thanks

@blackphoenixpt
Sorry, it will take some time for me to figure out, what your code is supposed to do and what it is doing.
Good practice is to write the code with comments where you are explaining what the different variables are and what the different sections are supposed to do. In old days writing code where you jumped in and out in different sections spaghetti code (you do that when you press a key A,B,C on your M5Stack).In Arduino you have to cut your code into different sections and the look, if the sections are doing what they are supposed to. In most programs in arduino you insert a "serial.println(x,y,z)" at the end of each section. Before running your program you open a terminal on the com line your ESP32 is connected to. That way for each code snipped you can check, if x,y,z are the values the program should furnish.
By the way, as I understand from your code, your supposed random numbers are no random numbers any more:
Starting you set all your MatrixNumber(150) to 1; then you generate 25000 times a random variable x (Temp1); you modify the variable x with your options (2,1,0,1,2) (=Temp2) and adjust the values of new x (Temp2) to 150; then you increment MatrixNumber(x) by 1 (25000 times); as I see it, your MatrixNumber(150) should then be 25000/50= ~500. So you are testing the randomness oft he random number generator instead generating random numbers ! In mathematics it's called the Monte Carlo Method: If you play roulette 25000 times, statistically all numbers drawn should be equal. I several numbers are drawn more frequent than they should be statistically, there must be a flaw in the roulette spinning machine. Players analyzing this behavior of the roulette spinning machine can then set their chips on these numbers trying using it to their advatage breaking the bank.
crami25

@crami25 Hello. After analysing the code do you see any flaw? WHY does the code draw random numbers if the tweaks are 00 and numbers are almot the same drawn if tweaks are different than 00? That puzzles me. Any thought on that? Have you upload the prog to your m5 and saw the strange behaviour? Thank for your help.

I loaded your code into arduino (1.8.12) and tried to compile it.
The verifying of your code stopped at line 248 where you have an
exit; statement. So the code you submitted above cannot be the code running on your M5Stack.
The right syntax of your code should be exit(0);
Do you really want that ? see
https://forum.arduino.cc/index.php?topic=216073.0
On the arduino, calling exit(0) really isn't a useful thing to do.
On Arduino, the statement exit(0) translates to
cli(); // disable interrupts
while(1); // loop forever
So it basically stops your program running, but leaves your CPU running in an infinite loop ! So your programm gets stuck in your getEuromillionOptions(); subroutine until you switch it off. 
@crami25 Hello. Strange the "exit" it should be "break" and it should be NOTHING. I don't know WHY I put the exit there. Just remove it.. Sorry for that mistake. The idea is exiting the sub. Have you found anything else weird? What about the random numbers drawing scheme? It's weird that with no "tweaking" numbers are drawn "random" values and with "tweaking" they repeat themselves. You can only see this if you upload the source to an m5stack. Thank you

@crami25 Hello again. I have redone all the sorting/tipping/drawing stuff. And now the code works as I wanted it. Code is not optimized for size but seems to work. I posted the new code here https://pastebin.com/fq7AVKrG
NOW the "random" numbers are "random" even with "tweaks" actives. Thanks for your support and help. Any sugestions/remarks are welcome as always.