When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
119 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
@woven sundial
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.
can you please format your code first
```cpp
int main() {}
```
int main() {}
double x, double& y, int i, int& j
````x` and `i` are copies. `y` and `j` are references
yes that i know
so if you modify x inside Mystery the value of the original x is unchanged
oh wait youre saying that isn't happening
ah right
what i mean to say is that for instance
x = 1.0;
y = 2.0;
i = 3;
j = 4;
Mystery (y,x,j,i);
this can also be represented like this also right?
Mystery (2.0, 1.0, 4, 3)
no, you can't take a mutable reference to a literal
no i mean the values are passed like this right
in a purely value-wise sense then yes you're correct
in your case
in the main :
x = 1.0;
y = 2.0;
i = 3;
j = 4;
in the function :
y = 1.0;
x = 2.0;
j = 3;
i = 4;
after the process :
x = 1.0
y = 1.0
i = 8
j = 6
you "return" j and y and put the value on x and i so result is :
x = 1.0;
y = 2.0;
i = 6;
j = 4;
;compile
#include <iostream>
using namespace std;
void Mystery(double x, double& y, int i, int& j);
int main() {
double x;
double y;
int i;
int j;
x = 1.0;
y = 2.0;
i = 3;
j = 4;
Mystery (y,x,j,i);
cout << x << y << i << j;
return 1;
}
void Mystery(double x, double& y, int i, int& j) {
x = y;
y = x;
i = 2 / i;
j = 2 / j;
}
1204
you "return" j and y and put the value on x and i so result is :
x = 1.0;
y = 2.0;
i = 6;
j = 4;
this part i dont understand
how did it becomes this
the question is like this lol
sure you don'tmean Myster(x, y, i, j)
what can i do?
the answer isn't 1 1 8 6 though idk how you managed to get that
this code results in 1 2 0 4
the original formatting has stars for multiplication
but discord makes it italic text i guess
ohhh
hence why I asked them to actually format their code
stars not divides
!f
I cant understand how the answer is 1 2 6 4 when i tried dry running it in function itself it gives 1 1 86 how does it change from 1 1 86 to 1 2 6 4, isn't i pass by value. Please help
#include <iostream>
using namespace std;
void Mystery(double x, double& y, int i, int& j);
int main() {
double x;
double y;
int i;
int j;
x = 1.0;
y = 2.0;
i = 3;
j = 4;
Mystery(y, x, j, i);
cout << x << y << i << j;
return 1;
}
void Mystery(double x, double& y, int i, int& j) {
x = y;
y = x;
i = 2 * i;
j = 2 * j;
}
;compile
1264
;compile
#include <iostream>
using namespace std;
void Mystery(double x, double& y, int i, int& j);
int main() {
double x;
double y;
int i;
int j;
x = 1.0;
y = 2.0;
i = 3;
j = 4;
Mystery (y,x,j,i);
cout << x << y << i << j;
return 1;
}
void Mystery(double x, double& y, int i, int& j) {
x = y;
y = x;
i = 2 * i;
j = 2 * j;
}
1264
lol
lmao
wow
void Mystery(double x, double& y, int i, int& j) {
x = y;
y = x;
i = 2 / i;
j = 2 / j;
}
if i put the values 2.0, 1.0 , 4 ,3 i get 1186
do you know how to use a debugger?
no
because that sounds like something you need really bad right now
wait is it / or is it *
how to use it
then learn to use one
where can i use one
okay well this works from top to bottom
what's your IDE
codeblocks
well great it has one builtin as far as I remember
Mystery(a, b, c, d)
void Mystery(double e, double& f, double g, double& h)
your function is like that, the a goes to e the b to f ect,
in your function y goes to x, x goes to y ect
can you click on the left of line numbers next to your file to put a red dot?
2.0, 1.0, 4, 3
x = y;
````1.0, 1.0, 4, 3`
```cpp
y = x;
````1.0, 1.0, 4, 3`
```cpp
i = 2 * i;
````1.0, 1.0, 8, 3`
```cpp
j = 2 * j;
````1.0, 1.0, 8, 6`
yh
yh
yh thats how i got 1 1 8 6
yes but again
the names are a different order
the original has y, x, j, i in the call
and only y and j are references in the function
great, those are breakpoints. now when your run your code using codeblocks, the execution will pause when it reaches a breakpoint, and you can inspect your variable's values before resuming
ok
debugging is INVALUABLE
you need to learn to debug
there is no way around it, you cannot be serious as a programmer if you don't learn how to use a debugger
ok i'll take note of that tysm
@woven sundial this is very important tounderstand too. Functions arguments are positional not named
ok concerning this
int foo(int a);
int b = 0;
foo(b);//positional, not named
a is just the internal name for that parameter that foo uses
all that anyone else cares about is the order of parameters
ok but in this part
void Mystery(double x, double& y, int i, int& j) {
x = y;
y = x;
i = 2i;
j = 2j;
}
when the values are passed isn't supposed to be x = 1.0, y = 1.0, i = 8, j = 6?
sorry to insist but you can actually see what happens and how if you just debug your code
idk what you mean
it's way simpler than asking strangers on the internet what your stuff does
the internal values at the end of the call are 1 1 8 6
but those don't map directly to x, y, i, j inside main
then
it might get more clear if we use a b c d instead of using 2 sets of x y i j
int main() {
// ...
double a = 1.0;
double b = 2.0;
int c = 3;
int d = 4;
Mystery(b, a, d, c);
// ...
}
void Mystery(double x, double& y, int i, int& j);
finally
lol we did the same thing
but yeah if it was that way you would get 1 1 6 8
but its not
i added & to the other 2 btw it's not his code
you get 1186 here?
this is just going to be confusing
i cant understand
having too many different versions of the code doesn't help
okay lets just go to my simple example
its the same code
but I renamed the variables inside main so we can differentiate them
b -> x, a -> y, d -> i, c -> j
then you go through and end up with the 1 1 8 6 like we had before
buuuut
only y and j are references
so the only values inside main that actually change are the ones that were originally passed to those
which are a and c
now a was 1.0 anyway so its still 1.0. but c was 3 and now its 6
b and d remain unchanged
giving us 1 2 6 4
also because its passed as Mystery(b, a, d, c) its even more confusing
man ty for being patient with me
because you would expect them to go `Mystery(a, b, c, d)
but its not
so yeah
keep an eye out for that stuff
tysm
!solved