Lab 4. Solutions

Task 1. Swap

#include <iostream>
using namespace std;

void swap(int &x, int &y);

int main() {
    int a = 5, b = 10;
    cout << "a = " << a << " b = " << b << endl;
    swap(a, b);
    cout << "a = " << a << " b = " << b << endl;
    swap(a, b);
    cout << "a = " << a << " b = " << b << endl;
}

void swap(int &x, int &y) {
    int t = x;
    x = y;
    y = t;
}

Task 2. Simplifying fractions

#include <iostream>
#include <cstdlib>
using namespace std;

void simplify(int &n, int &d);

int main() {
    srand(time(NULL));
    for (int i=0; i<20; ++i) {
        int n = rand() % 401 - 200;
        int d = rand() % 401 - 200;
        cout << n << " / " << d << "   \t  ->  ";
        simplify(n,d);
        cout << n << " / " << d << endl;
    }
}

void simplify(int &n, int &d) {
    for (int i = abs(d); i > 0; i--) {
        if (n % i == 0 && d % i == 0) {
            n = n / i;
            d = d / i;
            break;
        }
    }
}

Task 3. Sorting three integers

#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;

void swap(int &x, int &y);

void sort(int &a, int &b, int &c);

int main() {
    for(int i = 0; i < 1000000; ++i) {
        int a = rand()%100;
        int b = rand()%100;
        int c = rand()%100;
        sort(a,b,c);

        if ( !(a <= b && b <= c) )
            cout
                    << a << " "
                    << b << " "
                    << c << " "
                    << "failure" << endl;
    }
}

void swap(int &x, int &y) {
    int t = x;
    x = y;
    y = t;
}

void sort(int &a, int &b, int &c) {
    // First, make so that 'a' stores the smallest value
    if (b < a) swap(a,b);
    if (c < a) swap(a,c);
    // Now, 'a' contains the smallest value.
    // If b <= c we are done, otherwise, swap b and c:
    if (c < b) swap(b,c);

    assert(a <= b && b <= c);
}

Task 4. Bonus: Reverse an integer

The trick is to extract the first (most) significant digit (we get it in the variable first), and the rest of the number (variable rest).

For example, when we compute reverse(1234), first == 1, and rest == 234.

Once we have first and rest, we recursively call reverse(rest), and attach the first digit at the end:

answer = reverse(rest) * 10 + first 

reverse(rest) == reverse(234) == 432, and we “attach” 1 at the end, obtaining 4321.

#include <iostream>
#include <cassert>
using namespace std;

// reverses the digits of an integer
// assumes that the argument is a positive integer
int reverse(int x);

int main() {
    cout << reverse(23456) << endl;
    cout << reverse(12) << endl;
    cout << reverse(1) << endl;
    cout << reverse(0) << endl;
}

int reverse(int x) {
    assert(x > 0);

    if (x < 10) {
        return x;
    }
    else {
        // find the first (most significant) digit
        int first = x;
        int dec = 1;
        while (first / 10 > 0) {
            first /= 10;
            dec *= 10;
        }
        // the rest of the number x
        // (everything except the first digit)
        int rest = x % dec;

        // reverse the rest and add the first digit at the end
        return reverse(rest) * 10 + first;
    }
}