functions

Hi,
So I have an assignment to do and it goes as follows.

Write a simple program which reads in 2 integers, a & b, from cin.
print out the values they entered, a & b
Pass a & b *by reference* to a function which will *swap* their values
Return back to main() and print out a & b again, showing that their values have been swaped.

and this is what I have so far...

#include <iostream>
using namespace std;

void duplicate (int& x, int& y)
{
x*=b/a;
y*=a/b;
}

int main ()
{
int a, b;
cout << "Enter first number:";
cin >> a;
cout << "Enter second number:";
cin >> b;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
duplicate (a, b);
cout << "a= " << a << "', b= " << b;
return 0;
}

Any help or advice would be great.

Thanks
I don't think you got the meaning of swapping...
it means that after the call b should have the value of a and vice versa.


a and b aren't declared in duplicate.
First, you should give your functions meaningful names, not misleading ones. Here's how you could write that function:

1
2
3
4
5
6
void swap_values(int& x, int& y)
{
	int temp = x;
	x = y;
	y = temp;
}

You should notice a few things you were getting wrong. A reference acts exactly like the original object. There's no need to dereference it (and, to dereference a pointer, you say *ptr, not ptr*). Also, a function has no way of knowing about non-local, non-global variables (I'm ignoring class members for now).

Finally, what were you thinking by assigning some variable to b/a or a/b? That's certainly not swapping!
Last edited on
Yeah, I was confused, I was figuring that using a division of the numbers would swap their values, hence the a/b b/a stuff. And thank you guys for the help. Here is what I have now,

#include <iostream>
using namespace std;

void swap_values (int& x, int& y)
{
int temp = x;
x = y;
y = temp;
}

int main ()
{
int x, y;
cout << "Enter first number: ";
cin >> x;
cout << "Enter second number: ";
cin >> y;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
return 0;
}

Im still a little confused as to how the the swapped values get printed
Well, um, they aren't, as you don't ever swap them in that code, and don't ever print them after the swap. Unless I'm misunderstanding you somewhere.
The assignment states:
Write a simple program which reads in 2 integers, a & b, from cin.
print out the values they entered, a & b
Pass a & b *by reference* to a function which will *swap* their values
Return back to main() and print out a & b again, showing that their values have been swapped.

So im confused on the last step
Pass a & b *by reference* to a function which will *swap* their values

You're missing this step. And you don't need to rename a and b to x and y. Those names are local to swap_values().
Last edited on
Need to call the swap_values function and print out x and y again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main ()
{
  int x, y;
  cout << "Enter first number: ";
  cin >> x;
  cout << "Enter second number: ";
  cin >> y;
  cout << "x = " << x << endl;
  cout << "y = " << y << endl;

  // ADD THE FOLLOWING 3 LINES
  swap_values( x, y );  
  cout << "x = " << x << endl;
  cout << "y = " << y << endl;

  return 0;
}
Topic archived. No new replies allowed.