requesting assistance with homework problem

I am a beginner to c++ and am needing some assistance/suggestions on a procedure I have to write called void sort2(int& a, int& b) that swaps the values of a and b if a > b otherwise a & b remain unchanged. I have re-coded this program 4 times and I got it to work last re-code, but with undesired results. Any ideas or suggestions would be helpful. I have researched every topic I could think of in order to work this out on my own but to no avail. I also get an error: The variable 'b' is being used without being initialized and it has been. Figured it out already!!! Here is my code:

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
39
40
41
42
43
44
45
46
47
48
49
50
// This program swaps the values of two integers a and b 
// if a > b and otherwise leaves a and b unchanged.

#include <iostream>

using namespace std;

/** 
   Prints the values of two integers a and b.
   @param a an integer
   @param b another integer
   @return swaps the values of a and b if a is greater than b
   @return otherwise leaves a and b unchanged
*/
void sort2(int& a, int& b)    // swaps the values of a and b if a > b, otherwise a and b remain unchanged.
{
	int temp;
    if (a > b)
		temp = b;
	else if
		(a < b)
		temp = a;
}

int main(void)
{

//Declare variables

int a = 5;
int b = 6;
int c = 9;
int d = 11;
sort2(a, b);  // a is still a, b is still b
sort2(c, d);  // c is now d, d is now c  

cout << " " << a << " " << b <<"\n";  
cout << " " << c << " " << d << "\n";
swap (c, d);

cout << a << " " << b << " " << c << " " << d << " " << "\n";

//Displays message to user to capture screen shot of window
//prior to closing program

system("PAUSE");

return 0;

}
Last edited on
Topic archived. No new replies allowed.