The code sorts 3 numbers in ascending order. But, swap
function has already been invoked by reference. Why must also sort
function be invoked by reference ? My question is that swap function has already been invoked by reference. So, why also need sort function by reference ? I'm confused. Secondly, cout << endl,
doesn't give any error so, I've pressed wrongly the comma. How come ?
#include <iostream>
using namespace std;
void swap ( int& a, int& b );
void sort ( int a, int b, int c );
int main() {
int num1, num2, num3;
cout << "Enter first number => ";
cin >> num1;
cout << "Enter second number => ";
cin >> num2;
cout << "Enter third number => ";
cin >> num3;
cout << endl,
cout << "Before sorting numbers\n" << num1
<< " " << num2 << " " << num3 << endl;
sort( num1, num2, num3 );
cout << "After sorting numbers\n" << num1
<< " " << num2 << " " << num3 << endl;
return 0;
}
void swap ( int& a, int& b ) {
int temp = a;
a = b;
b = temp;
}
void sort ( int a, int b, int c ) {
//void sort ( int& a, int& b, int& c )
if (a > b)
swap(a, b);
if (a > c)
swap(a, c);
if (b > c)
swap(b, c);
}