//
// Created by Fred Park on 4/28/17.
//

/*
* File: SwapIntegers.cpp
* ----------------------
* This program illustrates the use of call by reference to exchange
* the values of two integers.
*/

#include <iostream>
#include "simpio.h"
using namespace std;

/* Function prototype */
//void swap(int & x, int & y);
//void swap(int *px, int *py); // if pointers are explicit
void swap(int * &px, int * &py); // swap pointers by reference

/* Main program */
int main() {
    int n1 = getInteger("Enter n1: ");
    int n2 = getInteger("Enter n2: ");
    int *p1 = &n1;
    int *p2 = &n2;
    cout << "The range is " << *p1 << " to " << *p2 << "." << endl;
    //cout << &n1 << endl;
    //if (n1 > n2) swap(n1, n2);
    //if (n1 > n2) swap(&n1, &n2); // if you made pointers explicit

    if (n1 > n2) swap(p1, p2); // swap pointers
    cout << "The range is " << *p1 << " to " << *p2 << "." << endl; // output values pointed to by dereferencing
    //cout << "The range is " << n1 << " to " << n2 << "." << endl;
    return 0;
}

/*
* Function: swap
* Usage: swap(x, y);
* ------------------
* Exchanges the values of x and y. The arguments are passed by
* reference and can therefore be modified.
*/
//void swap(int & x, int & y) {
//    int tmp = x;
//    x = y;
//    y = tmp;
//}

//


//swap by making the pointers explicit
//void swap(int *px, int *py) {
//    int tmp = *px;
//    *px = *py;
//    *py = tmp;
//}

//swap pointers by reference
void swap(int * &px, int * &py) {
    int *tmp = px;
    px = py;
    py = tmp;
}