C
C#3mo 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
Pobiega3mo ago
think about it logically. When would it make sense to update the "second best" variables?
egg
egg3mo 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
Pobiega3mo 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
egg3mo ago
right
Pobiega
Pobiega3mo ago
But what about the second racer? When will their time be the best?
egg
egg3mo ago
they'll only be 1st place if sec > first
Pobiega
Pobiega3mo ago
I meant second as in i == 2, not second as in second best 🙂
egg
egg3mo ago
their time will be the best if their time is smaller than first
Pobiega
Pobiega3mo ago
okay, lets pretend it was what must we do then?
egg
egg3mo ago
then first = time and order = i
Pobiega
Pobiega3mo ago
But that person now has the second best time
egg
egg3mo ago
so that means that i need to move whoever was first to sec, right?
Pobiega
Pobiega3mo 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
egg3mo 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
Pobiega3mo 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
egg3mo 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
Pobiega3mo ago
else if correct and you need to update the second best before updating the new best
egg
egg3mo ago
so then why wouldnt this work?
No description
egg
egg3mo ago
or even something like this
No description
Pobiega
Pobiega3mo 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
egg3mo ago
so lets stick with the first one, how do i make it update the best time?
Pobiega
Pobiega3mo ago
I think thats easy enough to figure out but your second condition is still wrong
egg
egg3mo ago
then what about this?
No description
Pobiega
Pobiega3mo ago
almost but time > sec means "time HIGHER than sec" thats hardly what we want, is it?
egg
egg3mo ago
is the problem the if statement? it gives me the same result whether its time<sec or time>sec
Pobiega
Pobiega3mo 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
egg3mo ago
okay then if i switch it to time<sec , would that mean that both if condition are correct?
Pobiega
Pobiega3mo ago
yes
MODiX
MODiX3mo 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
Pobiega3mo ago
we only care if the time is the new best time, or the new second best time
egg
egg3mo 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
Pobiega3mo ago
can you, with words, describe what each condition is checking, and what actions it takes when that condition is true
egg
egg3mo 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
Pobiega3mo 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
egg3mo ago
so this?
No description
egg
egg3mo 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
Pobiega3mo ago
yes!
egg
egg3mo ago
it feels like the else if should be fine but something isnt right up top
No description
Pobiega
Pobiega3mo 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
egg3mo ago
so the problem is somewhere up top?
Pobiega
Pobiega3mo ago
yup
egg
egg3mo ago
either in first=time or sec=time
Pobiega
Pobiega3mo 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
egg3mo ago
the silver medal moves to whichever one was first before the 3rd person came
Pobiega
Pobiega3mo ago
exactly is your code currently doing that? its giving gold AND silver to the 3rd person
egg
egg3mo ago
so the problem is in sec=time wait holy shit i have cooked (?) because firstly it needs to updat ethe 2nd one
Pobiega
Pobiega3mo ago
go on..
egg
egg3mo ago
No description
Pobiega
Pobiega3mo ago
you are so close the times are fixed but the orders are not
egg
egg3mo ago
so just switch the order and secOrder but it gives me the same result wait no ???
Pobiega
Pobiega3mo ago
look at the values you are assigning
egg
egg3mo ago
wait wdym order as in order and secOrders ? or orders of operation
Pobiega
Pobiega3mo ago
the first one
egg
egg3mo ago
but they look fine no?
Pobiega
Pobiega3mo ago
no?
egg
egg3mo ago
No description
Pobiega
Pobiega3mo ago
order = i secOrder = i you are giving them both the new value
egg
egg3mo ago
but what other value could i give them
Pobiega
Pobiega3mo ago
are you serious?
egg
egg3mo ago
yes, genuinely
Pobiega
Pobiega3mo 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
egg3mo ago
so this?
No description
Pobiega
Pobiega3mo ago
pretty much So uh... This took about 20 times longer than I had expected it to, if I'm honest
egg
egg3mo ago
what matters is the result :D
Pobiega
Pobiega3mo ago
I get the feeling you are not really familiar or comfortable with logical thinking?
egg
egg3mo ago
no not really
Pobiega
Pobiega3mo ago
You will need to practice this, a lot.
egg
egg3mo ago
i know, but thank you still
Want results from more Discord servers?
Add your server
More Posts
✅ Wndproc is not receiving any messages when window is not focusedHI! I am trying to handle messages in WPF using WndProc like this: ```csharp _messageWindowHandle = Annotate Props for Entity FrameworkWhich annotations do exist and how do I import them? I especially need UNIQUE contraints for the colWhy is my program showing my else statement when the response is valid?Hello, I'm currently in a college course for C# and I'm learning loops right now. I'm doing an assig✅ How To Get Notification Of When User Has Scrolled Down To End - Avalonia```xml <Border> <Grid RowDefinitions="Auto, *"> <Label Classes="Title" Content="{Binding TitleLaunch browser after build/debug container in RiderTittle, any idea how? or is it impossible? Any button or something to click to open?✅ "debug executable 'location' specified in the [project] debug profile does not exist"hello, i'm an absolute beginner with c# and am trying to work on a project for school. whenever i reriot games lol apiso i have an object that has all the summoner data. i.e: name level rank etc.. but after i edit that✅ Not Quite Understanding Model Usage```cs public class UserModel { public int Id {get; set;} public string FirstName {get;set;} pu✅ **Hello, could you help me understand `&`, `&&`, `|`, `||` in C# with this code example:**```csharp int a = 4, b = 3, c = 2, d = 1; if (a / b > 1 & c - d < 1 | a % 2 == 0) label1.Text =I want to some advicesHello, im a 3rd grade computer engineering student. I want to be a .net core back-end developer. I h