d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Basic C++ Question
Add Reply New Topic New Poll
Member
Posts: 2,664
Joined: Apr 29 2009
Gold: 255.00
Nov 14 2013 12:24pm
I have to explain why this swapping function does not work, I'm not exactly sure what's going on.

Here's the code... any syntax errors here aren't issues.

10. What is the output?

void doStuff(int,int);

int main()
}
int x = 1;
int y = 2;
doStuff(x,y);
cout << x << " " << y << endl;
{

void doStuff(int x, int y)
}
int z;
z = x;
x = y;
y = z;
{

the output of main is:
1 2
I'm not sure why the swapping isn't working.

Thanks for any help.
Member
Posts: 35,291
Joined: Aug 17 2004
Gold: 12,730.67
Nov 14 2013 12:33pm
Quote (smashT @ Nov 14 2013 10:24am)
I have to explain why this swapping function does not work, I'm not exactly sure what's going on.

Here's the code... any syntax errors here aren't issues.

10. What is the output?

void doStuff(int,int);

int main()
}
int x = 1;
int y = 2;
doStuff(x,y);
cout << x << " " << y << endl;
{

void doStuff(int x, int y)
}
int z;
z = x;
x = y;
y = z;
{

the output of main is:
1 2
I'm not sure why the swapping isn't working.

Thanks for any help.


Are you familiar with pointers and/or references?
Member
Posts: 2,664
Joined: Apr 29 2009
Gold: 255.00
Nov 14 2013 12:35pm
Quote (thundercock @ Nov 14 2013 10:33am)
Are you familiar with pointers and/or references?


Yes I am.

Are the x and y in doStuff going out of scope? It's just not clicking atm.

This post was edited by smashT on Nov 14 2013 12:40pm
Member
Posts: 35,291
Joined: Aug 17 2004
Gold: 12,730.67
Nov 14 2013 12:45pm
Quote (smashT @ Nov 14 2013 10:35am)
Yes I am.

Are the x and y in doStuff going out of scope? It's just not clicking atm.


Ok good. In your main, you are passing the values of x and y into your function. What you want, is to pass the reference itself. If you change your input parameters to &x and &y, you should be good.

Read chapters 7.1 through 7.4 (very quick) to give yourself a refresher.

http://www.learncpp.com/cpp-tutorial/71-function-parameters-and-arguments/
Member
Posts: 2,664
Joined: Apr 29 2009
Gold: 255.00
Nov 14 2013 12:50pm
Quote (thundercock @ Nov 14 2013 10:45am)
Ok good. In your main, you are passing the values of x and y into your function. What you want, is to pass the reference itself. If you change your input parameters to &x and &y, you should be good.

Read chapters 7.1 through 7.4 (very quick) to give yourself a refresher.

http://www.learncpp.com/cpp-tutorial/71-function-parameters-and-arguments/


Thanks for the link, I will read it.

Alright...

My understanding now is that the function doStuff in the code above is creating two new variables x and y with whatever values are input, which then go out of scope as soon as the function stops executing, and when I get back into main, I'm using the x and y declared in main.

If I change it to:

doStuff(int &x, int &y)

then I'm not longer creating new variables in doStuff, but rather referencing the same variables from main.

Does that sound right?

This post was edited by smashT on Nov 14 2013 01:00pm
Member
Posts: 35,291
Joined: Aug 17 2004
Gold: 12,730.67
Nov 14 2013 01:05pm
Quote (smashT @ Nov 14 2013 10:50am)
Thanks for the link, I will read it.

Alright...

My understanding now is that the function doStuff in the code above is creating two new variables x and y with whatever values are input, which then go out of scope as soon as the function stops executing, and when I get back into main, I'm using the x and y declared in main.

If I change it to:

doStuff(int &x, int &y)

then I'm not longer creating new variables in doStuff, but rather referencing the same variables from main.

Does that sound right?


Yep!
Member
Posts: 2,664
Joined: Apr 29 2009
Gold: 255.00
Nov 14 2013 01:10pm
Quote (thundercock @ Nov 14 2013 11:05am)
Yep!


Okay, thanks for the help!
Member
Posts: 2,664
Joined: Apr 29 2009
Gold: 255.00
Nov 14 2013 01:25pm
Alright, question 2 time... Similar situation, when I'm saying stuff[x] is that acting like a reference variable to whatever is stored in the xth spot of stuff?

11. What is the output?

void doStuff(int[], int, int);

int main()
{
int x = 1;
int y = 2;
int stuff[5];
for (int i = 0; i < 5; i++)
stuff[i] = i;
doStuff(stuff, x, y):
cout << stuff[x] << “ “ << stuff[y] << endl;
}

void doStuff(int stuff[], int x, int y)
{
int z = stuff[x];
stuff[x] = stuff[y];
stuff[[y] = z;
}

This post was edited by smashT on Nov 14 2013 01:26pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 14 2013 11:33pm
Quote (smashT @ Nov 14 2013 03:25pm)
Alright, question 2 time... Similar situation, when I'm saying stuff[x] is that acting like a reference variable to whatever is stored in the xth spot of stuff?

11. What is the output?

void doStuff(int[], int, int);

int main()
{
int x = 1;
int y = 2;
int stuff[5];
for (int i = 0; i < 5; i++)
    stuff[i] = i;
doStuff(stuff, x, y):
cout << stuff[x] << “ “ << stuff[y] << endl;
}

void doStuff(int stuff[], int x, int y)
{
  int z = stuff[x];
stuff[x] = stuff[y];
stuff[[y] = z;
}


first try it on paper. then run the code on the computer. if your expected results arent the correct results, figure out why. not sure why you're posting it here.
Go Back To Homework Help Topic List
Add Reply New Topic New Poll