Some help for school homework.

One of the homework questions I have is this.

4. Write a function named Exchange that swaps the values of the two double parameters passed and returns the value of the larger of the two parameters. The parameters should be passed by reference.

What I'm wondering is am I suppose to return or swap the variables differently? I really don't quite understand this question at all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 int main()
{

	double Exchange(double &,double &);
	double value1;
	double value2;

	cin >> value1 >> value2;

	Exchange(value1, value2);

	if (value1 >= value2)
	{
		cout << value1;
	}
	else
	{
		cout << value2;
	}



}

double Exchange (double &num1,double &num2)
{
	double swap;
	double swap2;

	swap = num1;
	swap2 = num2;
	num1 = swap2;
	num2 = swap;
	return(0.0);
}
closed account (48T7M4Gy)
The way I understand the question is Exchange swaps the two numbers first and then returns the larger of the two. So the if statement should be in the Exchange not main()
How would I just return the highest value? As far as I'm aware it replaces both value1 and value2 because they are referenced.
You need to adjust Exchange() so it returns the larger of the two values, as currently it is returning 0.0 always. The swapping works just fine, you just don't return the larger value.
It returns both values swapped, could you give me an example of what you are talking about?
closed account (48T7M4Gy)
OK, because num1 and num2 are passed by reference if you cout << both value1 and value2 in main() then you will see they have been swapped.

The next step is Exchange, by your design returns a double and this is where you can return the larger of the two values.
a) in main() declare double largerOrSome Name= Exchange(...)
and
b) in Exchange(...)
leave the swap code as is and put in a bit extra:-

if x is greater than y
then return x
otherwise return y

Over to you to work out the details. :-)
@TC: It does not "return" the values swapped; yes, the values are swapped, but if you look at line 34 you are returning 0.0. Please note that "returning" a value from a function has a specific, technical meaning, and does not informally refer to what a function does.
closed account (48T7M4Gy)
TC?
Okay, that's where I got a little confused, so would this be correct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int main()
{

	double Exchange(double &,double &);
	double value1;
	double value2;

	cin >> value1 >> value2;

	Exchange(value1, value2);

}

double Exchange (double &num1,double &num2)
{
	double swap;
	double swap2;

	swap = num1;
	swap2 = num2;
	num1 = swap2;
	num2 = swap;

	if (num1 > num2)
	{
		return (num1);
	}
	else
	{
		return (num2);
	}
}
closed account (48T7M4Gy)
Yep, that's ok as far as it goes but you need to go two steps further and 1) use the return value for example by writing double xyz = Exchange(...) with cout << xyz, and also 2) displaying value1 & value2, all in main(). That way you should be able to see what your function has done.
I think I got it, thank you very much.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

int main()
{

	double Exchange(double &,double &);
	double value1;
	double value2;
	double bigger;

	cin >> value1 >> value2;

	bigger = Exchange(value1, value2);

	

	cout << bigger;

}

double Exchange (double &num1,double &num2)
{
	double swap;
	double swap2;

	swap = num1;
	swap2 = num2;
	num1 = swap2;
	num2 = swap;

	if (num1 > num2)
	{
		return (num1);
	}
	else
	{
		return (num2);
	}
}
closed account (48T7M4Gy)
Excellent.
Add cout << value1 << endl; and cout << value2 << endl; in main() to check whether the swap has been done. :)
Topic archived. No new replies allowed.