Need help with homework
Hiya, I've been trying to solve an exercise for a bit but it's boggling me.
The exercise is:
Write a program that accepts a 3 digit integer (100 for exmaple) and swaps the numbers in the ones and hundreds
For example: 523 -> 325
i only know up to string, and the question requires me to solve it in a way a beginner would.
109 Replies
How do you think it can be solved?
I'm really not sure, i was thinking of using the string function but then again i have no idea how to assemble them together into one number.
Yes, turning the number into a string would be a good first step
Then, you can use the fact that a string is really just a spicy array of chars, so you can access its elements like you would elements of an array
Lastly, you can use string interpolation, for example, to put those chars back into a string, in a different order
Okay, give me a minute to try working on this.
Would this be possible .. ?
The teacher is really vague with his teaching and he hasn't taught us interpolation (yet, i'd assume.)
Int.Parse will throw an error if what you're inputting isn't an integer.
Oops, yeah i forgot to add that the teacher requires us to have it be an integer,
So it's okay.
I understand that, and not to complicate things but a programmer should always try to take into account how things can fail. I might be overreaching and this might not be part of this training.
Also, this resembles C# but only vaguely
^
Did you actually try to write and run this code?
Yeah, i understand.
Just that we haven't really gotten hands-down interactive yet, still on paper.
Eh.. i have no idea how to, he's only taught us static void main and no classes or anything.
Well you don't need classes here
Just variables, maybe a check for the value to be in range like in your pseudocode
Just turn that pseudocode into actual code, try to run it, see what's wrong
You will never, ever, not in a million years learn programming if you don't actually run your code
What i more mean is that even with code that runs properly, i have no idea how to make it run.
he's legit just taught us the basics of code, but not the basics of actually running it on a computer.
I used to do python last year, i did run my codes but i genuinely have no idea how to run them now with the switch to c#.
What OS do you use?
Windows, 11
Download Visual Studio 2022 then
This will be the easiest way
Which workload is the exact one for C#?
This perhaps?
No, this is UWP
You want the, uh, classic applications with C#?
This one
I'm really not sure, just something to run the code.
Okay, thank you.
Well thank you for ensuring the OP won't learn anything
Spoonfeeding is worse than not answering
Actually, is this code even relevant?
I don't even understand it.
Didn't learn any of that yet.
That would be another issue, yeah
I'll write some comments when I have time, just a bit busy right now.
Yeah, don't
It's okay, I can just remove it.
Problem solved.
Did not intend to fuck up someones day.
Should've realized this is one of those "walk before you can run" ordeals.
Okay, i've got it installed.
Now do i create a new project, orr..?
Yes
or you can search for templates and find "Console App"
Alright, so
fixed it up a bit, now how would i be able to run it?
This is almost valid C#, it should run...ish
F5 runs code in VS, IIRC
Or the green play arrow button
Almost looks like python to me.
"int a,b," correct me if I'm wrong but I don't think you can declare empty nullables as non nullable
and not even like this
Fairly self-explanatory errors
Work on 1 by 1 error
I really don't know, i'm confused.
Int
does not exist
Because C# is case-sensitive
It has an int
, but not an Int
Mhm, i noticed that a bit ago, did fix it but
Now it's saying Int just doesn't exist.
Then, you declare
a
and b
as int
variables, but later you re-declare them as string
You canot redeclare variablesAh
Lastly, you can index a string with
[]
, but not integersdown to two errors now... i really don't know how to continue from now.
== for equality
Oh.
Oops-
My badd
I like that you figured out .Length
i THINK i solved it
no
b
will be a sum of digits^
Ah, right.. it doesn't run addition though without it being an int.
How.. would i solve that?
$interpolation
String interpolation is the preferred way of building strings in C#. It is easier to read than concatenation.
For example:
can be written as:
Any way you can post your entire code?
That was all of it.
Here's a suggestion that might help.
When you paste it in here, type '''cs at the beginning.
That's because
char
has an implicit conversion to int
ive.
done this a few times for python, but ive
forgotton how to do it again
$codegif
ah, right the new line
thank you
it's okay, you don't have to include the cs but
it turns it into this
So, again, use string interpolation
So that you get a string, not an integer out of it
would then for example
work..?
ive never done interpolation, just mentioning once more.
You can introduce more variables here, sure
It's not needed though
can just be
does that mean you can just immediately drop for example, a[2]+a[1] into the interpolation?
Yeah
I now realize I misread the original assignment.
Did you figure this out with the interpolation?
Currently trying
..it runs, but i'm not sure if it's correct.
i'll check, one sec.
You're still summing the chars
Each should be inserted separately into the string
{a[2]}{a[1]}{a[0]}
^
var c = $"{a[2]}{a[1]}{a[0]}";
The question is whether you understand why this produces the output you're looking for.
A string is just a List<Char>. If you select a single letter from your string you will get back something of the type Char.
Er, okay i've changed it now.
But how would i print it to just check if its correct?
Go head
Since c isn't considered as a variable
Console.WriteLine()
c
is a variable
But only exists within the scope of the if
's braces
$scopesthing a
is available in scope A
and scope B
thing b
is available only in scope B
Ooh, i see.
What if the number is above 999? Should that also be ignored?
I'd recommend you look towards the top, in the assignment
It says "accept a 3 digit integer" so that's vague
Like I learned the hard way going into this, for him it's a "learn before you can walk" ordeal so it's not one where we take into account for user error. We assume a 3 digit input
Says the guy that complained about Int.Parse being used
This should be right, testing the code results in what i need.
Yep, LGTM
Looks good.
Did I not say I learned something?
You can consolidate that
into
too
Okay, thank you all for the help! :D
This was one hell of a question.. super vague too. (In terms of explanation, since the teacher barely taught us anything.)
I suggest you look into making this even better, perhaps by making it reset itself. It gives experience and I'm sure you'd be complimented on the effort
I could give you some ideas but that's obviously only if you are bothered to do that 😄
At this point, no 😭
I've had enough of this question
Understandable
But the code you wrote is clear, right?
Before that I suggest you change the variable names to something more expressive.
Sure it's not strictly needed in short assignments like this, but it can really help solving those.
Hold on, this code is not valid though
Did you run it at all?
@asianfxilure
Yeah, it works.
Why would it not be valid?
var c = ($"{a[2]}{a[1]}{a[0]}");
How would this work?Sure, the parentheses around the string are useless
But it does work
Angius
REPL Result: Success
Result: string
Compile: 471.199ms | Execution: 32.474ms | React with ❌ to remove this embed.
Oh, I'm sorry. I assumed it made a new string, but I was testing with an old verison of .NET and I think it was not supported there
Carry on
You can also do it without a string.
To compile C# code in Discord, use
!eval
Example:
Please don't try breaking the REPL service. Also, remember to do so in #bot-spam!Murten
REPL Result: Success
Result: int
Compile: 419.442ms | Execution: 40.008ms | React with ❌ to remove this embed.