C
C#8mo ago
Asian 𓃴

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
Angius
Angius8mo ago
How do you think it can be solved?
Asian 𓃴
Asian 𓃴8mo ago
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.
Angius
Angius8mo ago
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
Asian 𓃴
Asian 𓃴8mo ago
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 a,b;
a = Int.Parse(Console.ReadLine);
if a>99 && a<999;
string b;
string a;
b = st[2]+st[1]+st[0]
int a,b;
a = Int.Parse(Console.ReadLine);
if a>99 && a<999;
string b;
string a;
b = st[2]+st[1]+st[0]
3R1C
3R1C8mo ago
Int.Parse will throw an error if what you're inputting isn't an integer.
Asian 𓃴
Asian 𓃴8mo ago
Oops, yeah i forgot to add that the teacher requires us to have it be an integer, So it's okay.
3R1C
3R1C8mo ago
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.
Angius
Angius8mo ago
Also, this resembles C# but only vaguely
3R1C
3R1C8mo ago
^
Angius
Angius8mo ago
Did you actually try to write and run this code?
Asian 𓃴
Asian 𓃴8mo ago
Yeah, i understand. Just that we haven't really gotten hands-down interactive yet, still on paper.
Angius
Angius8mo ago
WHAT
Asian 𓃴
Asian 𓃴8mo ago
Eh.. i have no idea how to, he's only taught us static void main and no classes or anything.
Angius
Angius8mo ago
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
Asian 𓃴
Asian 𓃴8mo ago
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#.
Angius
Angius8mo ago
What OS do you use?
Asian 𓃴
Asian 𓃴8mo ago
Windows, 11
Angius
Angius8mo ago
Download Visual Studio 2022 then This will be the easiest way
Asian 𓃴
Asian 𓃴8mo ago
Which workload is the exact one for C#?
Asian 𓃴
Asian 𓃴8mo ago
This perhaps?
No description
Angius
Angius8mo ago
No, this is UWP You want the, uh, classic applications with C#?
Angius
Angius8mo ago
This one
No description
Asian 𓃴
Asian 𓃴8mo ago
I'm really not sure, just something to run the code. Okay, thank you.
Angius
Angius8mo ago
Well thank you for ensuring the OP won't learn anything Spoonfeeding is worse than not answering Actually, is this code even relevant?
Asian 𓃴
Asian 𓃴8mo ago
I don't even understand it. Didn't learn any of that yet.
Angius
Angius8mo ago
That would be another issue, yeah
3R1C
3R1C8mo ago
I'll write some comments when I have time, just a bit busy right now.
Angius
Angius8mo ago
Yeah, don't
3R1C
3R1C8mo ago
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.
Asian 𓃴
Asian 𓃴8mo ago
Okay, i've got it installed. Now do i create a new project, orr..?
3R1C
3R1C8mo ago
Yes
3R1C
3R1C8mo ago
No description
3R1C
3R1C8mo ago
No description
3R1C
3R1C8mo ago
or you can search for templates and find "Console App"
Asian 𓃴
Asian 𓃴8mo ago
Alright, so
int a, b;
a = Int.Parse(Console.ReadLine);
if (a > 99 && a < 999);
string b;
string a;
b = a[2] + a[1] + a[0];
int a, b;
a = Int.Parse(Console.ReadLine);
if (a > 99 && a < 999);
string b;
string a;
b = a[2] + a[1] + a[0];
fixed it up a bit, now how would i be able to run it?
Angius
Angius8mo ago
This is almost valid C#, it should run...ish F5 runs code in VS, IIRC Or the green play arrow button
3R1C
3R1C8mo ago
Almost looks like python to me.
Asian 𓃴
Asian 𓃴8mo ago
No description
3R1C
3R1C8mo ago
"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
Angius
Angius8mo ago
Fairly self-explanatory errors
3R1C
3R1C8mo ago
Work on 1 by 1 error
Asian 𓃴
Asian 𓃴8mo ago
I really don't know, i'm confused.
Angius
Angius8mo ago
Int does not exist Because C# is case-sensitive It has an int, but not an Int
Asian 𓃴
Asian 𓃴8mo ago
Mhm, i noticed that a bit ago, did fix it but Now it's saying Int just doesn't exist.
Angius
Angius8mo ago
Then, you declare a and b as int variables, but later you re-declare them as string You canot redeclare variables
Asian 𓃴
Asian 𓃴8mo ago
Ah
Angius
Angius8mo ago
Lastly, you can index a string with [], but not integers
Asian 𓃴
Asian 𓃴8mo ago
string a;
a = Console.ReadLine();
if (a.Length = 3);
{
a = a[2] + a[1] + a[0];
}
string a;
a = Console.ReadLine();
if (a.Length = 3);
{
a = a[2] + a[1] + a[0];
}
No description
Asian 𓃴
Asian 𓃴8mo ago
down to two errors now... i really don't know how to continue from now.
Angius
Angius8mo ago
== for equality
Asian 𓃴
Asian 𓃴8mo ago
Oh. Oops- My badd
3R1C
3R1C8mo ago
I like that you figured out .Length
Asian 𓃴
Asian 𓃴8mo ago
i THINK i solved it
string a;
int b;
a = Console.ReadLine();
if (a.Length == 3);
{
b = a[2] + a[1] + a[0];
}
string a;
int b;
a = Console.ReadLine();
if (a.Length == 3);
{
b = a[2] + a[1] + a[0];
}
3R1C
3R1C8mo ago
no
Angius
Angius8mo ago
b will be a sum of digits
3R1C
3R1C8mo ago
^
Asian 𓃴
Asian 𓃴8mo ago
Ah, right.. it doesn't run addition though without it being an int. How.. would i solve that?
Angius
Angius8mo ago
$interpolation
MODiX
MODiX8mo ago
String interpolation is the preferred way of building strings in C#. It is easier to read than concatenation. For example:
var foo = 1;
var bar = 2;
Console.WriteLine("foo is equal to: " + foo + " and bar is equal to: " + bar);
var foo = 1;
var bar = 2;
Console.WriteLine("foo is equal to: " + foo + " and bar is equal to: " + bar);
can be written as:
var foo = 1;
var bar = 2;
Console.WriteLine($"foo is equal to: {foo} and bar is equal to: {bar}");
var foo = 1;
var bar = 2;
Console.WriteLine($"foo is equal to: {foo} and bar is equal to: {bar}");
3R1C
3R1C8mo ago
Any way you can post your entire code?
Asian 𓃴
Asian 𓃴8mo ago
That was all of it.
3R1C
3R1C8mo ago
Here's a suggestion that might help. When you paste it in here, type '''cs at the beginning.
Angius
Angius8mo ago
That's because char has an implicit conversion to int
Asian 𓃴
Asian 𓃴8mo ago
string a;
int b;
a = Console.ReadLine();
if (a.Length == 3);
{
b = a[2] + a[1] + a[0];
}
string a;
int b;
a = Console.ReadLine();
if (a.Length == 3);
{
b = a[2] + a[1] + a[0];
}
ive. done this a few times for python, but ive forgotton how to do it again
Angius
Angius8mo ago
$codegif
Asian 𓃴
Asian 𓃴8mo ago
ah, right the new line thank you
3R1C
3R1C8mo ago
it's okay, you don't have to include the cs but it turns it into this
string a;
int b;
a = Console.ReadLine();
if (a.Length == 3);
{
b = a[2] + a[1] + a[0];
}
string a;
int b;
a = Console.ReadLine();
if (a.Length == 3);
{
b = a[2] + a[1] + a[0];
}
Angius
Angius8mo ago
So, again, use string interpolation So that you get a string, not an integer out of it
Asian 𓃴
Asian 𓃴8mo ago
would then for example
var ones = a[2]
var tens = a[1]
var hundreds = a[0]
var ones = a[2]
var tens = a[1]
var hundreds = a[0]
work..? ive never done interpolation, just mentioning once more.
Angius
Angius8mo ago
You can introduce more variables here, sure It's not needed though
var x = 2 + 2;
var str = $"Value is {x}";
var x = 2 + 2;
var str = $"Value is {x}";
can just be
var str = $"Value is {2 + 2}";
var str = $"Value is {2 + 2}";
Asian 𓃴
Asian 𓃴8mo ago
does that mean you can just immediately drop for example, a[2]+a[1] into the interpolation?
Angius
Angius8mo ago
Yeah
3R1C
3R1C8mo ago
I now realize I misread the original assignment. Did you figure this out with the interpolation?
Asian 𓃴
Asian 𓃴8mo ago
Currently trying
string a;
int b;
a = Console.ReadLine();
if (a.Length == 3);
{
var c = ($"Value is {a[2] + a[1] + a[0]}");
}
string a;
int b;
a = Console.ReadLine();
if (a.Length == 3);
{
var c = ($"Value is {a[2] + a[1] + a[0]}");
}
..it runs, but i'm not sure if it's correct. i'll check, one sec.
Angius
Angius8mo ago
You're still summing the chars Each should be inserted separately into the string {a[2]}{a[1]}{a[0]}
3R1C
3R1C8mo ago
^ var c = $"{a[2]}{a[1]}{a[0]}"; The question is whether you understand why this produces the output you're looking for.
Joschi
Joschi8mo ago
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.
Asian 𓃴
Asian 𓃴8mo ago
Er, okay i've changed it now. But how would i print it to just check if its correct?
3R1C
3R1C8mo ago
Go head
Asian 𓃴
Asian 𓃴8mo ago
Since c isn't considered as a variable
Angius
Angius8mo ago
Console.WriteLine() c is a variable But only exists within the scope of the if's braces $scopes
MODiX
MODiX8mo ago
scope A {
thing a;
scope B {
thing b;
}
}
scope A {
thing a;
scope B {
thing b;
}
}
thing a is available in scope A and scope B thing b is available only in scope B
Asian 𓃴
Asian 𓃴8mo ago
Ooh, i see.
FusedQyou
FusedQyou8mo ago
What if the number is above 999? Should that also be ignored?
3R1C
3R1C8mo ago
I'd recommend you look towards the top, in the assignment
FusedQyou
FusedQyou8mo ago
It says "accept a 3 digit integer" so that's vague
3R1C
3R1C8mo ago
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
FusedQyou
FusedQyou8mo ago
Says the guy that complained about Int.Parse being used
Asian 𓃴
Asian 𓃴8mo ago
string a;
a = Console.ReadLine();
if (a.Length == 3);
{
var c = ($"{a[2]}{a[1]}{a[0]}");
Console.WriteLine(c);
}
string a;
a = Console.ReadLine();
if (a.Length == 3);
{
var c = ($"{a[2]}{a[1]}{a[0]}");
Console.WriteLine(c);
}
This should be right, testing the code results in what i need.
Angius
Angius8mo ago
Yep, LGTM
FusedQyou
FusedQyou8mo ago
Looks good.
3R1C
3R1C8mo ago
Did I not say I learned something?
Angius
Angius8mo ago
You can consolidate that
string a;
a = Console.ReadLine();
string a;
a = Console.ReadLine();
into
string a = Console.ReadLine();
string a = Console.ReadLine();
too
Asian 𓃴
Asian 𓃴8mo ago
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.)
FusedQyou
FusedQyou8mo ago
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 😄
Asian 𓃴
Asian 𓃴8mo ago
At this point, no 😭 I've had enough of this question
FusedQyou
FusedQyou8mo ago
Understandable But the code you wrote is clear, right?
Joschi
Joschi8mo ago
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.
//Don't
string a = Console.ReadLine();

// Do
string input = Console.ReadLine();
//Don't
string a = Console.ReadLine();

// Do
string input = Console.ReadLine();
FusedQyou
FusedQyou8mo ago
Hold on, this code is not valid though Did you run it at all? @asianfxilure
Asian 𓃴
Asian 𓃴8mo ago
Yeah, it works.
Angius
Angius8mo ago
Why would it not be valid?
FusedQyou
FusedQyou8mo ago
var c = ($"{a[2]}{a[1]}{a[0]}"); How would this work?
Angius
Angius8mo ago
Sure, the parentheses around the string are useless But it does work
MODiX
MODiX8mo ago
Angius
REPL Result: Success
var x = ("Hello World!");
x
var x = ("Hello World!");
x
Result: string
Hello World!
Hello World!
Compile: 471.199ms | Execution: 32.474ms | React with ❌ to remove this embed.
FusedQyou
FusedQyou8mo ago
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
Murten
Murten8mo ago
You can also do it without a string.
MODiX
MODiX8mo ago
To compile C# code in Discord, use !eval Example:
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
Please don't try breaking the REPL service. Also, remember to do so in #bot-spam!
MODiX
MODiX8mo ago
Murten
REPL Result: Success
var x = 12345;
var y = 0;
while (x > 0)
{
y = y * 10 + x % 10;
x /= 10;
}
y
var x = 12345;
var y = 0;
while (x > 0)
{
y = y * 10 + x % 10;
x /= 10;
}
y
Result: int
54321
54321
Compile: 419.442ms | Execution: 40.008ms | React with ❌ to remove this embed.
Want results from more Discord servers?
Add your server
More Posts