#Can anybody help me with this code?

119 messages · Page 1 of 1 (latest)

vernal phoenixBOT
#

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 Do Not Delete Posts!

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.

supple wraith
#

can you please format your code first

vernal phoenixBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
ionic cargo
#
double x, double& y, int i, int& j
````x` and `i` are copies. `y` and `j` are references
woven sundial
#

yes that i know

ionic cargo
#

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

woven sundial
#

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)

supple wraith
#

no, you can't take a mutable reference to a literal

woven sundial
#

no i mean the values are passed like this right

supple wraith
#

in a purely value-wise sense then yes you're correct

compact marsh
#

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;

ionic cargo
#

;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;
}
hushed masonBOT
#
Program Output
1204
woven sundial
#

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

ionic cargo
#

Mystery (y,x,j,i); wtf

#

why are you mixing up the order

woven sundial
#

the question is like this lol

ionic cargo
#

sure you don'tmean Myster(x, y, i, j)

woven sundial
#

what can i do?

ionic cargo
#

the answer isn't 1 1 8 6 though idk how you managed to get that

ionic cargo
supple wraith
#

the original formatting has stars for multiplication

#

but discord makes it italic text i guess

ionic cargo
#

ohhh

supple wraith
#

hence why I asked them to actually format their code

ionic cargo
#

stars not divides

flint moon
#

!f

vernal phoenixBOT
#

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;
}
Erik_Slam
hushed masonBOT
#
Program Output
1264
ionic cargo
#

;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;
}
hushed masonBOT
#
Program Output
1264
supple wraith
#

lol

compact marsh
#

lmao

ionic cargo
#

wow

woven sundial
#

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

supple wraith
#

do you know how to use a debugger?

woven sundial
#

no

supple wraith
#

because that sounds like something you need really bad right now

ionic cargo
woven sundial
#

how to use it

supple wraith
woven sundial
woven sundial
ionic cargo
supple wraith
#

what's your IDE

woven sundial
supple wraith
#

well great it has one builtin as far as I remember

compact marsh
#
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

supple wraith
ionic cargo
#

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`
woven sundial
#

yh

woven sundial
ionic cargo
#

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

supple wraith
# woven sundial yh

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

supple wraith
#

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

woven sundial
ionic cargo
ionic cargo
#
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

woven sundial
#

ok but in this part
void Mystery(double x, double& y, int i, int& j) {
x = y;
y = x;
i = 2i;
j = 2
j;

}
when the values are passed isn't supposed to be x = 1.0, y = 1.0, i = 8, j = 6?

supple wraith
#

sorry to insist but you can actually see what happens and how if you just debug your code

supple wraith
#

it's way simpler than asking strangers on the internet what your stuff does

ionic cargo
#

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

woven sundial
#

then

ionic cargo
#

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);
compact marsh
#

finally

ionic cargo
#

lol we did the same thing

#

but yeah if it was that way you would get 1 1 6 8

#

but its not

compact marsh
#

i added & to the other 2 btw it's not his code

ionic cargo
#

yeah I noticed

#

thats why I said its no that

woven sundial
#

you get 1186 here?

ionic cargo
#

this is just going to be confusing

woven sundial
#

i cant understand

ionic cargo
#

having too many different versions of the code doesn't help

ionic cargo
#

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

woven sundial
#

ahhhhhh

#

ok

#

yh

#

i get it now

#

yh

ionic cargo
#

also because its passed as Mystery(b, a, d, c) its even more confusing

woven sundial
#

man ty for being patient with me

ionic cargo
#

because you would expect them to go `Mystery(a, b, c, d)

#

but its not

#

so yeah

#

keep an eye out for that stuff

woven sundial
#

tysm

ionic cargo
#

and remember that locals are local

#

:D

woven sundial
#

!solved