C
C#11mo 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
Kouhai11mo 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
Jace11mo ago
yea i noticed that but from there i dont know what to do.. ;/
Kouhai
Kouhai11mo 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
Jace11mo ago
pour cup A into cup B
Kouhai
Kouhai11mo ago
But cup A and cup B are full
Jace
Jace11mo 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
Kouhai11mo ago
Exactly, so how many additional cups would you need now?
Jace
Jace11mo ago
i would need to additional cups ok i feel like i get it ;o 4 cups in total 4 var
Kouhai
Kouhai11mo ago
Not quite
Jace
Jace11mo ago
ah shiiieet
Kouhai
Kouhai11mo ago
Why do you need 4 cups?
Jace
Jace11mo ago
to make the exchange between them nah ?
Pobiega
Pobiega11mo 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
Jace11mo ago
oh wait its 3 cups ? cup a into empty
Pobiega
Pobiega11mo ago
yup!
Jace
Jace11mo ago
then i just have to put the cup b into the cup a then cup b into cup a ? right ?
Pobiega
Pobiega11mo ago
yup me, I would pour in a vacuum with force and distance so each cup is empty before catching the others liquid. 😄
Jace
Jace11mo ago
oh my ! yay !!!
Pobiega
Pobiega11mo ago
(aka the tuple switch) 😄 but the third variable is the "classic" way of solving this
Jace
Jace11mo 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
Kouhai11mo 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
Jace11mo 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
Kouhai11mo 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
Jace11mo 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
Kouhai11mo 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
Jace11mo ago
alr so then it will be like 5 = z 8 =5 z = 8 right ?
Kouhai
Kouhai11mo 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
Jace11mo ago
x = empty
Kouhai
Kouhai11mo ago
Right, does it make much sense?
Jace
Jace11mo 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
Kouhai11mo 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
Jace11mo ago
nah xddd answer is 0 for both
Kouhai
Kouhai11mo 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
Jace11mo 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
Kouhai11mo 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
Jace11mo ago
hmm ok so those are basivally, the way to initialize the variables ? x = liquid c y= liquid b etc ?
Kouhai
Kouhai11mo ago
Yes a variable needs to be set to something to be initialized
Pobiega
Pobiega11mo ago
Uhm The error is that the order of operations is wrong You need to move something into z before using it
Jace
Jace11mo ago
ok great, it worked !! 🙌
Jace
Jace11mo ago
Jace
Jace11mo ago
ty 😉
Accord
Accord11mo 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.