Arithmetic

Click here to download HKUAL_arithmetic.h Library

#include <iostream>
#include <vector>
#include "HKUAL_arithmetic.h"
using namespace std;
using namespace HKUAL;

int main() {

    // Greatest Common Divisor
    cout << "Greatest Common Divisor:" << endl;
    cout << GCD( 2, 3 ) << endl;    // 1
    cout << GCD( 10, 15 ) << endl;  // 5

    cout << LCM( 2, 3 ) << endl;    // 6
    cout << LCM( 10, 15 ) << endl;  // 30
    cout << endl;

    // Extended Euclidean algorithm
    cout << "Extended Euclidean algorithm:" << endl;
    int x, y;
    cout << extendedGCD( 2, 3, x, y )
         << ' ' << x << ' ' << y << endl;
    cout << extendedGCD( 10, 15, x, y )
         << ' ' << x << ' ' << y << endl;
    cout << endl;

    // Majority problem
    cout << "Majority problem:" << endl;
    int myints[] = { 5, 1, 5, 2, 5, 3, 5, 4, 5 };
    int* ptr = getMajority( myints, myints + sizeof( myints ) / sizeof( int ) );
    if( ptr == myints + sizeof( myints ) / sizeof( int ) ) {
        cout << "No Such Value" << endl;
    }
    else {
        cout << "Majority is " << *ptr << endl;
    }

    string mystrs[] = { "e", "a", "e", "b", "e", "c", "e", "d", "e", "f" };
    vector<string> v( mystrs, mystrs + 10 );
    vector<string>::iterator itr = getMajority( v.begin(), v.end() );
    if( itr == v.end() ) {
        cout << "No Such Value" << endl;
    }
    else {
        cout << "Majority is " << *itr << endl;
    }
    cout << endl;

    return( 0 );
}
© The University of Hong Kong Algorithms Library - hkual@cs.hku.hk