double_swap function

I'm supposed to write a function named double_swap that takes two doubles as arguments and interchanges the values that are stored in those arguments. The function should return no value, so I know it needs to be written as a void. An example I was given is as follows:

If this code fragment is executed,

int main()
{
double x = 4.8, y = 0.7;
double_swap(x,y);
cout<<"x="<<x<<"y="<<y;
}

The output will be:
x=0.7 y=4.8


This is the code I came up with, but it won't compile. Can someone point out my errors? Thanks in advance.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

void double_swap()
{
        double x
        double y
        x = y;
        y = x;
}

int main()
{
        double x = 4.2;
        double y = 1.9;
        double_swap(x,y);
        cout<<"x = "<<x<<"y = "<<y<<endl;
}

The function needs some parameters.
Passed by reference.
see the examples in the tutorial.
http://www.cplusplus.com/doc/tutorial/functions/
Look in particular at the example named duplicate
Last edited on
I added parameters, and the code compiles and runs now, but I still can't get the values to swap. Any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

void double_swap(double x, double y)
{
        x = y;
        y = x;
}

int main()
{
        double x = 4.2, y = 1.9;
        double_swap(x,y);
        cout<<"x = "<<x<<" y = "<<y<<endl;
}
The parameters need to be passed by reference.
See the example in the tutorial:
1
2
3
4
5
6
void duplicate (int& a, int& b, int& c)
{
  a*=2;
  b*=2;
  c*=2;
}

http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.