C
C#2mo ago
Staynk

✅ Spent almost an hour trying to independently get this right and it’s not clicking for me.

no flame pls
No description
No description
114 Replies
ero
ero2mo ago
and what have you tried?
Staynk
StaynkOP2mo ago
I tried a while loop while(floors >=0); Console.WriteLine(floors++); I just don’t know how to get it in increments of 5
Staynk
StaynkOP2mo ago
No description
Staynk
StaynkOP2mo ago
This is the furthest I’ve gotten
Salman
Salman2mo ago
You need to be banned Guess why
Staynk
StaynkOP2mo ago
Bro I’m trying It’s hard man
Salman
Salman2mo ago
You still using convert after what I told ya
Staynk
StaynkOP2mo ago
No the app did that for me The app gives you a foundation to build from I didn’t code that.
leowest
leowest2mo ago
if you have nothing constructive to say to him then just dont. Every person learns in a different way and pace, be mindful pls.
Salman
Salman2mo ago
You missed the plot my guy I was kidding
leowest
leowest2mo ago
there is no plot, just dont say thing u shouldnt in help threads.
Salman
Salman2mo ago
I was referring to his previous thread
leowest
leowest2mo ago
u can keep those on #chat
Staynk
StaynkOP2mo ago
he helped me earlier Salmon just a chill guy
leowest
leowest2mo ago
I know he is, but help threads are meant to be a more serious place instead of throwing punches anyway... on to your question
Staynk
StaynkOP2mo ago
Is the while loop the wrong approach ? I have an idea of what to do just don’t know how
leowest
leowest2mo ago
you can use the remainder operator
Salman
Salman2mo ago
Do you see a pattern in the question?
Staynk
StaynkOP2mo ago
while floors < 0 Floors + 5
leowest
leowest2mo ago
no the while loop is fine, I meant as the way to identify the floors
JuPaHe64
JuPaHe642mo ago
more like currentFloor < floors
Salman
Salman2mo ago
Question says on every 5th floor so you need to determine if the floor is divisible by 5 , now it's on you how to figure that
JuPaHe64
JuPaHe642mo ago
cause floors will never be below 0 unless you have bad input with that change that code should work well you would of course increment currentFloor instead of floors
Staynk
StaynkOP2mo ago
Hmm okay I haven’t exactly learned to use division in code like he suggested but I get the idea
leowest
leowest2mo ago
basically if N divided by 5 is 0 it means its divisible by 5 so it means we are in one of those floors and that's what the remainder operator does I assume you have learned about if else conditions?
Staynk
StaynkOP2mo ago
Yes
leowest
leowest2mo ago
so essentially u would do
if (condition) print floor
if (condition) print floor
where your condition is the evaluation of the remainder I know its more code but I also suggest you to use the bracers to define your code blocks so u dont accidently run into problems i.e.:
while (condition)
{ // start statement block
// so u know the code in here is inside this scope
} // end statement block
while (condition)
{ // start statement block
// so u know the code in here is inside this scope
} // end statement block
and no mistake things like
while (condition)
Console.WriteLine(i);
Console.WriteLine(i+2);
while (condition)
Console.WriteLine(i);
Console.WriteLine(i+2);
where the 2nd console.writeline does not belong to the loop.
Staynk
StaynkOP2mo ago
I’ve had that issue before and was not understanding why. It’s easy to use the Microsoft visual script This platform on the phone is horrible
leowest
leowest2mo ago
you mean Visual Studio yes its a lot easier
Staynk
StaynkOP2mo ago
No description
Staynk
StaynkOP2mo ago
Holy shit I’m getting some where It just needs to be capped Input was 9 and output was 5 and 10
leowest
leowest2mo ago
yep that is where u will need the condition and how to identify the floors
Staynk
StaynkOP2mo ago
Can you explain the condition again Sorry I didn’t understand
Salman
Salman2mo ago
In this method, you actually don't need to check remainder
Staynk
StaynkOP2mo ago
Could I use an if break ? If (floors > res) break;
leowest
leowest2mo ago
break will, break you out of loop, switches etc so if u break it will exit the loop
Staynk
StaynkOP2mo ago
That’s what I want though right Because input given was 9 and I got an output of 5 and 10 It needs to break before I outputting 10
Salman
Salman2mo ago
yes, only increment if it's in the bounds
Staynk
StaynkOP2mo ago
Now it says my code is wrong
Staynk
StaynkOP2mo ago
No description
Staynk
StaynkOP2mo ago
It won’t run
leowest
leowest2mo ago
because you put the { before the loop
Salman
Salman2mo ago
You missed the brackets around while as Leo said
leowest
leowest2mo ago
the code is
while(condition)
{
// code
if (condition)
{
// code
}
// code
}
while(condition)
{
// code
if (condition)
{
// code
}
// code
}
Staynk
StaynkOP2mo ago
Oh I see
Spades
Spades2mo ago
using System; public class Program { public static void Main(string[] args) { int i = 0; Console.WriteLine ("Enter Floors: "); int floors = Convert.ToInt32(Console.ReadLine());
while(i < floors) { Console.WriteLine ("You are now in "+ floors +" floors"); break; i++; } } }
Staynk
StaynkOP2mo ago
No description
Staynk
StaynkOP2mo ago
That is what they wanted I cheated It makes sense when I see it I just couldn’t figure it out This stuff is tough man and the app is a horrible place to learn
leowest
leowest2mo ago
so this is why remainder is the optimal way to do this, im not good at explaining math so sorry about that... In c#, N % 5 == 0 where N is the floor, is equivalent to testing the left and right side to match 0. The expression is a - (a / b) * b which in your code would translate to floor - (floor / 5) * 5 For example: 17 % 5 is 2 so its not equal to 0 - 17 divided by 5 is 3, - 3 multiplied by 5 is 15 - 17 - 15 is 2 - so not divisible by 5 Ah but why its 3 and not 3.4 because we are doing integer division so the decimal places are left out. Another example: 16 % 5 is 1 so its not equal to 0 - 16 divided by 5 is 3, - 3 multiplied by 5 is 15 - 16 - 15 is 1 - so not divisible by 5 Now an example that hits the spot: 15 % 5 is 0 so its what we expect - 15 divided by 5 is 3, - 3 multiplied by 5 is 15 - 15 - 15 is 0 - so this number is divisible by 5 so all we need to test is
if (floor % 5 == 0)
{
Console.WriteLine($"We are on the {floor} floor");
}
if (floor % 5 == 0)
{
Console.WriteLine($"We are on the {floor} floor");
}
Staynk
StaynkOP2mo ago
well what happens when the input isn’t divisible by 5 ?
leowest
leowest2mo ago
it won't match 0 so it will not enter the condition so nothing gets printed
Staynk
StaynkOP2mo ago
So the building has 17 floors It wants to see there will be a restaurant on the 5th, 10th, and 15th floor Or example You have 23 floors
leowest
leowest2mo ago
it will print only the floors where there are restaurants
Staynk
StaynkOP2mo ago
Im still not tracking
leowest
leowest2mo ago
ok so we are looking for restaurants on the following floors 5,10,15,20 in the event of a 23 floors building correct?
Staynk
StaynkOP2mo ago
Yeah
leowest
leowest2mo ago
this is more of a mathematical question then programming so we start our loop we are at floor 23 23 - (23 / 5) * 5 23 / 5 = 4 4 * 5 = 20 23 - 20 = 3 3 is not 0 so this floor does not have a restaurant then our loop decrements by 1 22 - (22 / 5) * 5 22 / 5 = 4 4 * 5 = 20 22 - 20 = 2 2 is not 0 so this floor also doent have a restaurant then our loop decrements by 1 let's skip 21 and go to 20 just for the sake of simplifying going down every floor
Staynk
StaynkOP2mo ago
I see
leowest
leowest2mo ago
20 / 5 = 4 4 * 5 = 20 20 - 20 = 0 so this floor have a restaurant floor 15 15 / 5 = 3 3*5 = 15 15-15 = 0 so this floor also have a restaurant so its literally math
Staynk
StaynkOP2mo ago
Yeah yeah That’s an interesting way to code it
leowest
leowest2mo ago
the remainder is doing all that work for u
Staynk
StaynkOP2mo ago
you say that is a better way to do it ?
MODiX
MODiX2mo ago
leowest
REPL Result: Success
var floors = 17;
while (floors > 0)
{
if (floors % 5 == 0)
{
Console.WriteLine($"We have a restaurant on {floors}");
}
else
{
Console.WriteLine($"No restaurant on {floors}");
}
floors--;
}
var floors = 17;
while (floors > 0)
{
if (floors % 5 == 0)
{
Console.WriteLine($"We have a restaurant on {floors}");
}
else
{
Console.WriteLine($"No restaurant on {floors}");
}
floors--;
}
Console Output
No restaurant on 17
No restaurant on 16
We have a restaurant on 15
No restaurant on 14
No restaurant on 13
No restaurant on 12
No restaurant on 11
We have a restaurant on 10
No restaurant on 9
No restaurant on 8
No restaurant on 7
No restaurant on 6
We have a restaurant on 5
No restaurant on 4
No restaurant on 3
No restaurant on 2
No restaurant on 1
No restaurant on 17
No restaurant on 16
We have a restaurant on 15
No restaurant on 14
No restaurant on 13
No restaurant on 12
No restaurant on 11
We have a restaurant on 10
No restaurant on 9
No restaurant on 8
No restaurant on 7
No restaurant on 6
We have a restaurant on 5
No restaurant on 4
No restaurant on 3
No restaurant on 2
No restaurant on 1
Compile: 475.192ms | Execution: 42.523ms | React with ❌ to remove this embed.
leowest
leowest2mo ago
im saying its the most optimal way to do it
Staynk
StaynkOP2mo ago
Interesting
leowest
leowest2mo ago
a lot of the stuff ur doing right now arent much of coding but more of mathematical
Staynk
StaynkOP2mo ago
Is that standard when learning ?
leowest
leowest2mo ago
or rather solvable with math
Staynk
StaynkOP2mo ago
May I ask how you learned C#
leowest
leowest2mo ago
when learning arithmetic operators sure to some extent I've learned a few language before reaching c# been coding for many years... understanding remainder can be a bit difficult but its simple math.
Staynk
StaynkOP2mo ago
I understand the code you did
leowest
leowest2mo ago
and all of what I said above is explained on the documentation for it
No description
Staynk
StaynkOP2mo ago
I understand when reading it but to brainstorm and think of it myself is just not there yet
leowest
leowest2mo ago
the other way u could do which is simpler would be by counting to 5 but then u would need to start from the bottom and reset the counter every time u reach 5 that is the most simplestic way
if (counter == N)
{
//reset counter
//print
}
if (counter == N)
{
//reset counter
//print
}
Staynk
StaynkOP2mo ago
I appreciate your help. You seem to be well educated on this stuff Are you an employed programmer ?
leowest
leowest2mo ago
most people u will meet here are contractors or employed in some way, some aspiring to or even hobbyist programmers this community is massive, including people that work at Microsoft are in this discord
Staynk
StaynkOP2mo ago
Thats crazy to think about I could be receiving help by a GTA 6 dev haha
leowest
leowest2mo ago
well u would need to be in a c++ discord I guess 😛 but for gta mods yeah
Staynk
StaynkOP2mo ago
Well I look forward to learning. I’ve been playing around with unity when im at home. I follow beginner level game dev guides
leowest
leowest2mo ago
yeah my advice to you is, dont worry about unity until u learn the fundamentals of c# using console apps it will make your life 100x easier on unity side a very easy going video I like to recommend is https://learn.microsoft.com/en-us/shows/csharp-fundamentals-for-absolute-beginners/ its very slow paced, its a bit outdated but still good and the person explains every little bit very detailed
Staynk
StaynkOP2mo ago
I will be watching that I’m using the sololearn app
leowest
leowest2mo ago
think it will help u understand alot of things never heard of it, what is the site?
Staynk
StaynkOP2mo ago
It’s like duolingo for coding lol It’s a mobile application
leowest
leowest2mo ago
but they have a site I assume?
Staynk
StaynkOP2mo ago
I’m sure lemme see
leowest
leowest2mo ago
do you not have a pc?
Staynk
StaynkOP2mo ago
Sololearn: Learn to Code
Join Now to learn the basics or advance your existing skills
Staynk
StaynkOP2mo ago
I have a pc for home I have a lot of free time at work so I use the mobile app while I’m here to learn.
leowest
leowest2mo ago
I see, I cannot imagine learning or coding on mobile
Staynk
StaynkOP2mo ago
Some call it dedication Hahaha
leowest
leowest2mo ago
I mean if it works for u... but having a proper IDE makes a world of difference for learning
Staynk
StaynkOP2mo ago
Yeah I have the Visual Studio and all installed at home. I’ve been learning there too I paid for a C# fundamentals course online and the instructor created code games. Like rock paper scissors and bulls and cows
leowest
leowest2mo ago
bulls and cows? never heard of that one paid where udemy or something?
Staynk
StaynkOP2mo ago
Yes Udemy was it
leowest
leowest2mo ago
I see, I am not much of a fan due to all the overselling they do with poor quality hope the teacher and course u got are different
Staynk
StaynkOP2mo ago
Yeah it appears the Microsoft link is just about the same material he covered lol
leowest
leowest2mo ago
yeah I mean the fundamentals dont change much and they are all over the internet
Staynk
StaynkOP2mo ago
Maybe I need repetition with it for it to truly stick.
leowest
leowest2mo ago
the difference would be the teacher actually explaining things to u on a 1 on 1 or properly having all the material u actually need without forcing u to search around your self to understand things yes repetition is good but dont do repetition aim less try doing some projects with some goal even if a console text based app
Staynk
StaynkOP2mo ago
Up until now I’ve been following YouTube guides and copying their code only understanding half of it I’ll never learn that way
leowest
leowest2mo ago
well if you can break it down and understand what it is actually doing its one thing but if u just copy and paste without even understanding it then yes
Staynk
StaynkOP2mo ago
sometimes I can follow other times I’m lost
leowest
leowest2mo ago
that's why learning the fundamentals help, it introduces you to all the basics from using the ide, to what your code is, what code blocks are what scopes are and how they influence your code, what methods are, variables, types, loops, etc
Staynk
StaynkOP2mo ago
yeah there’s just so much
leowest
leowest2mo ago
yes you wont learn a language in 24hrs
Staynk
StaynkOP2mo ago
I honestly think learning Spanish is easier 😂
leowest
leowest2mo ago
it takes time once u learn the fundamentals try doing a more complete project like the rock paper scissors or hangman or even a bank app in console it will show u where ur lacking etc and what u need to review etc
Staynk
StaynkOP2mo ago
Yeah that’s a challenge
leowest
leowest2mo ago
if u can learn most of the topics in that link I posted above it will be easy anyway good luck to u, if u have more questions just open a new thread $close if any one flames or bully or anything u can just call a mod people here are very friendly and more than whiling to intervene if needed.
MODiX
MODiX2mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Staynk
StaynkOP2mo ago
thanks again
Salman
Salman2mo ago
It's not the most optimal way, it goes through each element and check if it's divisible by 5 or not while the other method is more optimal in which you start from 0 and increment 5 unless you go out of the bounds . Because the task is to only print out the multiples of 5
leowest
leowest2mo ago
the most optimal way the way he was doing it with a while loop decrementing the value, yes. I did say there was other ways to do it, that would be simpler for him to understand You're free to provide your own solution.
Ducki
Ducki2mo ago
cant this be done with a simple % operator? for loop to increment by one every time and when i % 5 == 0 print i? and for loop ends when i > 10

Did you find this page helpful?