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:
// This program swaps the values of two integers a and b
// if a > b and otherwise leaves a and b unchanged.
#include <iostream>
usingnamespace 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;
elseif
(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;
}