C
C#16mo ago
Jace

✅ Cant resolve this problem

What im trying to do is : Exchange the value of each variable. Example, if x = 3 and y = 10 and we run the program, x will be 10 and y will be 3. The problem is that when im running the code, x is the same number as y.... i cant figure out how to make this code work.. double x; double y; Console.WriteLine("enter x"); x = double.Parse(Console.ReadLine()); Console.WriteLine("enter y"); y = double.Parse(Console.ReadLine()); x = y; y = x; Console.Write("new x :"); Console.WriteLine(x); Console.Write("new y value :"); Console.WriteLine(y);
42 Replies
Kouhai
Kouhai16mo ago
Well let's go step by step here
x = y; // Set x's value to y's value, now x and y share the same value
y = x; // Set y's value to x's value, now y and x share the same value
x = y; // Set x's value to y's value, now x and y share the same value
y = x; // Set y's value to x's value, now y and x share the same value
That won't work, right?
Jace
JaceOP16mo ago
yea i noticed that but from there i dont know what to do.. ;/
Kouhai
Kouhai16mo ago
Well if you have two cups of a liquid and you want to move from cup A to cup B, what would you?
Jace
JaceOP16mo ago
pour cup A into cup B
Kouhai
Kouhai16mo ago
But cup A and cup B are full
Jace
JaceOP16mo ago
hmm so put cup A in another cup that isnt filled, do the same with cupB ? then put A into B and B in A ?
Kouhai
Kouhai16mo ago
Exactly, so how many additional cups would you need now?
Jace
JaceOP16mo ago
i would need to additional cups ok i feel like i get it ;o 4 cups in total 4 var
Kouhai
Kouhai16mo ago
Not quite
Jace
JaceOP16mo ago
ah shiiieet
Kouhai
Kouhai16mo ago
Why do you need 4 cups?
Jace
JaceOP16mo ago
to make the exchange between them nah ?
Pobiega
Pobiega16mo ago
think about how you'd do this in real life and with the smallest number of cups without making a mess on your kitchen floor 😄
Jace
JaceOP16mo ago
oh wait its 3 cups ? cup a into empty
Pobiega
Pobiega16mo ago
yup!
Jace
JaceOP16mo ago
then i just have to put the cup b into the cup a then cup b into cup a ? right ?
Pobiega
Pobiega16mo ago
yup me, I would pour in a vacuum with force and distance so each cup is empty before catching the others liquid. 😄
Jace
JaceOP16mo ago
oh my ! yay !!!
Pobiega
Pobiega16mo ago
(aka the tuple switch) 😄 but the third variable is the "classic" way of solving this
Jace
JaceOP16mo ago
dam, ty btw guys hellow, x and y seems to work well, but the new one doesnt ( in the error output it says the z variable is not assigned ). heres the new code : double x; double y; double z; Console.WriteLine("enter x"); x = double.Parse(Console.ReadLine()); Console.WriteLine("enter y"); y = double.Parse(Console.ReadLine()); x = z; y = x; z = y; Console.Write("new x :"); Console.WriteLine(z); Console.Write("new y value :"); Console.WriteLine(y);
Kouhai
Kouhai16mo ago
double x; // 1
double y; // 2
double z; // 3

Console.WriteLine("enter x"); // 4
x = double.Parse(Console.ReadLine()); // 5
Console.WriteLine("enter y"); // 6
y = double.Parse(Console.ReadLine()); // 7

x = z; // 8
y = x; // 9
z = y; // 10
double x; // 1
double y; // 2
double z; // 3

Console.WriteLine("enter x"); // 4
x = double.Parse(Console.ReadLine()); // 5
Console.WriteLine("enter y"); // 6
y = double.Parse(Console.ReadLine()); // 7

x = z; // 8
y = x; // 9
z = y; // 10
Can you describe what each line does?
Jace
JaceOP16mo ago
first line to third line are the variables, 4th line asks the person to enter a value for x. 5th line reads the line and places it into x's value. same for 6-7 line( for y ). 8 -9 are more tricky o-o. i would say it goes like this : line 8 = x becomes z. line 9 : y becomes x. line 10 z becomes y.
Kouhai
Kouhai16mo ago
Okay great! Lines 1 to 3 declare variables, these variables currently aren't initialized Line 5 reads input from the console and parses it and then sets x to the parsed value, this initializes x Line 7 does the same but instead initializes y Now both x and y are initialized Now comes line 8 You're trying to set x to the value in z but z is to this point still not initialized Do you see the problem? 😅
Jace
JaceOP16mo ago
ohhh yeaaa but how do i initialize z, without asking the person to enter a value for z ? ( ifk if im clear in my question ) like x and y
Kouhai
Kouhai16mo ago
Great question, let's think a bit about these three lines and let's suppose the user provided 5 for x and 8 for y.
x = z; // 8
y = x; // 9
z = y; // 10
x = z; // 8
y = x; // 9
z = y; // 10
Jace
JaceOP16mo ago
alr so then it will be like 5 = z 8 =5 z = 8 right ?
Kouhai
Kouhai16mo ago
Well let's go back to the cup analogy Cup X has liquid A Cup Y has lquid B Cup Z is empty What does this do? x = z
Jace
JaceOP16mo ago
x = empty
Kouhai
Kouhai16mo ago
Right, does it make much sense?
Jace
JaceOP16mo ago
what i understand from this, tell me if im wrong btw : z should be null or 0 ? false even maybe like empty legit
Kouhai
Kouhai16mo ago
Exactly, z needs to be initialized to something z's type is double, double is a value type (meaning it can't be null [there are workarounds]) so because it can't be null, you can set it to 0 But still This won't really work would it?
Jace
JaceOP16mo ago
nah xddd answer is 0 for both
Kouhai
Kouhai16mo ago
If we have Cup Z has Liquid C Then x = z Means x = liquid c So now we have Cup X => Liquid C Cup Y => Liquid B Cup Z => Liquid C This might look weird, as if we have copied Cup Z's content into Cup X, turns out that's exactly how it works When you do x = z Z is copied to X typos Smadge
Jace
JaceOP16mo ago
-reading- is liquid C, 'z' ? like in this : Cup X => Liquid C Cup Y => Liquid B Cup Z => Liquid C the liquid c and b are they Z ?
Kouhai
Kouhai16mo ago
No Cup Z only has liquid C in it, it doesn't know anything about liquid B In fact a cup can only hold one liquid, it can't hold more than that
Jace
JaceOP16mo ago
hmm ok so those are basivally, the way to initialize the variables ? x = liquid c y= liquid b etc ?
Kouhai
Kouhai16mo ago
Yes a variable needs to be set to something to be initialized
Pobiega
Pobiega16mo ago
Uhm The error is that the order of operations is wrong You need to move something into z before using it
Jace
JaceOP16mo ago
ok great, it worked !! 🙌
Jace
JaceOP16mo ago
Jace
JaceOP16mo ago
ty 😉
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server