-6

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);
}
  • 5
    How would `num2` and `num3` change in `main()` if you dont pass them by reference? – NathanOliver Jul 07 '15 at 12:51
  • You do know that there exists a [`sort`](http://en.cppreference.com/w/cpp/algorithm/sort) and a [`swap`](http://en.cppreference.com/w/cpp/algorithm/swap) function in the `std` namespace? By having `using namespace std` in your code, your own function can cause clashes with the standard functions, especially the `swap` function in this specific case. – Some programmer dude Jul 07 '15 at 12:57
  • Also, you have two different and unrelated questions, and for that you should post two different questions. – Some programmer dude Jul 07 '15 at 12:58
  • That's just the [comma operator](http://stackoverflow.com/questions/52550/what-does-the-comma-operator-do-in-c). It'll evaluate `cout << endl`, throw away the result, then evaluate the other `cout` expression. – TartanLlama Jul 07 '15 at 12:58

1 Answers1

-1

When you call your sort function, three new variables are created in the scope of that function. These variables are copies of the three variables you pass it in your main function. Those three variables are going to be passed into the swap function, which takes two integer addresses. The variable address that you're passing it is only scoped to within the sort function. You could probably use pointers to keep everything more organized.

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 ) {

    if (*a > *b)
        swap(a, b);

    if (*a > *c)
        swap(a, c);

    if (*b > *c)
        swap(b, c);
}

I didn't actually have a chance to compile this, but I will later today to see if it works.

Seth
  • 43
  • 1
  • 6
  • 1
    That won't work because you are passing `int` to a function expecting `int*`. Why use pointers instead of just using references for both functions? – TartanLlama Jul 07 '15 at 13:04
  • 1
    This code has serious problems. Inside `main` the variables `num1`, `num2`, `num3` are _uninitialized pointers_. If you're lucky the program will crash when you try to dereference them. The `swap` function has no effect because you're swapping passed-by-value parameters around instead of the values they point to. – Blastfurnace Jul 07 '15 at 13:42