C
C#9mo ago
egg

Need help with some simple code

I'm a new coder and I need help with some really basic codes but I genuinely can not wrap my head around this. I need the user to input the number of people participating in a race, how much time it took them to complete their race and then for the program to say 1) how much time did it take for 1st place to finish the race + his order and 2) how much time it took for 2nd place to finish + his order (Yes I know I haven't done anything with sec and secOrder but that's because I don't know how to...)
No description
68 Replies
Pobiega
Pobiega9mo ago
think about it logically. When would it make sense to update the "second best" variables?
egg
eggOP9mo ago
i thought about doing something like examining all the numbers and their orders (excluding the biggest one) but idk how to do that or would it make sense to first update the 2nd one and only then seeing/updating the 1st place one?
Pobiega
Pobiega9mo ago
Thats certainly one way to do it! You'd need a list, or an array. But you can do this without either of those. lets walk through a potential scenario The very first racer - whatever time they have will be the best time, right?
egg
eggOP9mo ago
right
Pobiega
Pobiega9mo ago
But what about the second racer? When will their time be the best?
egg
eggOP9mo ago
they'll only be 1st place if sec > first
Pobiega
Pobiega9mo ago
I meant second as in i == 2, not second as in second best 🙂
egg
eggOP9mo ago
their time will be the best if their time is smaller than first
Pobiega
Pobiega9mo ago
okay, lets pretend it was what must we do then?
egg
eggOP9mo ago
then first = time and order = i
Pobiega
Pobiega9mo ago
But that person now has the second best time
egg
eggOP9mo ago
so that means that i need to move whoever was first to sec, right?
Pobiega
Pobiega9mo ago
in that case, yes essentially, for each time you inspect, there are 3 scenarios 1: it was the fastest time 2: it was the second fastest time 3: it was something else we dont care at all about the third scenario here, so we can ignore it can you write the code for detecting the first two scenarios? remember, they cant both be true at once.
egg
eggOP9mo ago
like this? but this doesnt feel right cause sec will get replaced with every new number that isnt the biggest one
No description
Pobiega
Pobiega9mo ago
nope, not correct lets walk through it the first if statement is correct, it checks if this is the new fastest time. The things you do IF it was true are wrong, however 🙂 but the second one is wrong, and its also running even if the first was true
egg
eggOP9mo ago
so the 2nd if is supposed to be an 'else if' and the things that will happen if the first IF is right are supposed to be sec and secOrder?
Pobiega
Pobiega9mo ago
else if correct and you need to update the second best before updating the new best
egg
eggOP9mo ago
so then why wouldnt this work?
No description
egg
eggOP9mo ago
or even something like this
No description
Pobiega
Pobiega9mo ago
lets look at the first one if the new time is the best time, update the second best time. stop. result: best time is never updated, second best time is actually best time. lets look at the second one same problem and the second if also has issues time > first means "if its worse than the best one" that will be true for everything but a new best time
egg
eggOP9mo ago
so lets stick with the first one, how do i make it update the best time?
Pobiega
Pobiega9mo ago
I think thats easy enough to figure out but your second condition is still wrong
egg
eggOP9mo ago
then what about this?
No description
Pobiega
Pobiega9mo ago
almost but time > sec means "time HIGHER than sec" thats hardly what we want, is it?
egg
eggOP9mo ago
is the problem the if statement? it gives me the same result whether its time<sec or time>sec
Pobiega
Pobiega9mo ago
I mean, the actions you take in the ifs are also wrong but for now I'm just trying to get you to fix your conditions
egg
eggOP9mo ago
okay then if i switch it to time<sec , would that mean that both if condition are correct?
Pobiega
Pobiega9mo ago
yes
MODiX
MODiX9mo ago
Pobiega
1: it was the fastest time 2: it was the second fastest time 3: it was something else
Quoted by
React with ❌ to remove this embed.
Pobiega
Pobiega9mo ago
we only care if the time is the new best time, or the new second best time
egg
eggOP9mo ago
so i did this, which correctly gives me the 2nd best time, but the best time and its order are reset to 9999 and 0
No description
Pobiega
Pobiega9mo ago
can you, with words, describe what each condition is checking, and what actions it takes when that condition is true
egg
eggOP9mo ago
the first condition checks the time and if its smaller than the 'first' number if its true, then the best time is stored in sec and so is its i order the 2nd condition checks if the number is smaller than 2nd place, in which case, if true, will replace the 'first number' with a number thats smaller than both the 'first' and 'sec' number ( not good )
Pobiega
Pobiega9mo ago
no First condition:
if the time is faster than the current best time... store that time as the second best time store that position as the second best order
this is not correct if the time is the fastest, why are you saving it as the second best time?
egg
eggOP9mo ago
so this?
No description
egg
eggOP9mo ago
but then the else if is never gonna happen so it cant be right so there needs to be something about both the best and 2nd best times in the first if condition, right?
Pobiega
Pobiega9mo ago
yes!
egg
eggOP9mo ago
it feels like the else if should be fine but something isnt right up top
No description
Pobiega
Pobiega9mo ago
you are so close currently, you are updating both first and second´to the same value thats not correct if its the new fastest time, the current fastest time will be the new second fastest time
egg
eggOP9mo ago
so the problem is somewhere up top?
Pobiega
Pobiega9mo ago
yup
egg
eggOP9mo ago
either in first=time or sec=time
Pobiega
Pobiega9mo ago
Try to imagine this situation you are standing at the finish line, and you are giving out medals as people come in if their time is beaten, you take the medal and move it the third person comes in and beats the new record so you must give him the first place medal what do you do with the silver medal?
egg
eggOP9mo ago
the silver medal moves to whichever one was first before the 3rd person came
Pobiega
Pobiega9mo ago
exactly is your code currently doing that? its giving gold AND silver to the 3rd person
egg
eggOP9mo ago
so the problem is in sec=time wait holy shit i have cooked (?) because firstly it needs to updat ethe 2nd one
Pobiega
Pobiega9mo ago
go on..
egg
eggOP9mo ago
No description
Pobiega
Pobiega9mo ago
you are so close the times are fixed but the orders are not
egg
eggOP9mo ago
so just switch the order and secOrder but it gives me the same result wait no ???
Pobiega
Pobiega9mo ago
look at the values you are assigning
egg
eggOP9mo ago
wait wdym order as in order and secOrders ? or orders of operation
Pobiega
Pobiega9mo ago
the first one
egg
eggOP9mo ago
but they look fine no?
Pobiega
Pobiega9mo ago
no?
egg
eggOP9mo ago
No description
Pobiega
Pobiega9mo ago
order = i secOrder = i you are giving them both the new value
egg
eggOP9mo ago
but what other value could i give them
Pobiega
Pobiega9mo ago
are you serious?
egg
eggOP9mo ago
yes, genuinely
Pobiega
Pobiega9mo ago
secOrder = order; before you update order just like you did with the time, you need to do the same with the order the position and time of the best/second best are two parts of one whole if we modelled this better with an object instead of two separate variables, this would be more obvious
egg
eggOP9mo ago
so this?
No description
Pobiega
Pobiega9mo ago
pretty much So uh... This took about 20 times longer than I had expected it to, if I'm honest
egg
eggOP9mo ago
what matters is the result :D
Pobiega
Pobiega9mo ago
I get the feeling you are not really familiar or comfortable with logical thinking?
egg
eggOP9mo ago
no not really
Pobiega
Pobiega9mo ago
You will need to practice this, a lot.
egg
eggOP9mo ago
i know, but thank you still
Want results from more Discord servers?
Add your server