C
C#14mo ago
asianfxilure

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