✅ 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
Well let's go step by step
here
That won't work, right?
yea i noticed that but from there i dont know what to do..
;/
Well if you have two cups of a liquid and you want to move from cup A to cup B, what would you?
pour cup A into cup B
But cup A and cup B are full
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 ?
Exactly, so how many additional cups would you need now?
i would need to additional cups
ok i feel like i get it ;o
4 cups in total
4 var
Not quite
ah shiiieet
Why do you need 4 cups?
to make the exchange between them nah ?
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 😄
oh wait
its 3 cups ?
cup a into empty
yup!
then i just have to put the cup b into the cup a then cup b into cup a ? right ?
yup
me, I would pour in a vacuum with force and distance so each cup is empty before catching the others liquid. 😄
oh my ! yay !!!
(aka the tuple switch) 😄
but the third variable is the "classic" way of solving this
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);
Can you describe what each line does?
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.
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? 😅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
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.
alr
so then it will be like
5 = z
8 =5
z = 8
right ?
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
x = empty
Right, does it make much sense?
what i understand from this, tell me if im wrong btw : z should be null or 0 ?
false even maybe
like empty legit
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?
nah xddd
answer is 0
for both
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 -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 ?
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
hmm ok so those are basivally, the way to initialize the variables ?
x = liquid c
y= liquid b
etc ?
Yes a variable needs to be set to something to be initialized
Uhm
The error is that the order of operations is wrong
You need to move something into z before using it
ok great, it worked !! 🙌
ty 😉
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.