Programming Class Bonus: SUCCESS!

bracketslash

Registered User
Joined
Feb 6, 2013
Messages
224
So, I finally finished a bonus problem for programming.

My task was to write a function which calculates the number of zeroes and ones in the binary representation of a given unsigned char, print out said ratio, write a function which takes an unsigned char as an input and produces a mapping that orders by number of zeroes first and then lexographically if there is a tie, and then write an inverse function for the previous function. DAY: COMPLETE.

Code:
#include <iostream>
#include <algorithm>

using namespace std;

// http://stackoverflow.com/posts/2548374/revisions
// returns string representation of binary number from integer
string decToBin(int number) {
    string result = "";

    do {
        if ( (number & 1 ) == 0 )
            result += "0";
        else
            result += "1";
        number >>= 1;
    } while ( number );
    reverse(result.begin(), result.end());
    if ( result.length() < 8 )
        result = string(8 - result.length(), '0') + result;
    return result;
}

// written by me :-D
// returns how many occurrences of FLAG there are in binary string
unsigned int num(string binary, int FLAG) { // 0 for zeroes, 1 for ones
    int count = 0;
    for ( int i=0; i<binary.size(); i++ ) {
        if ( FLAG == 0 )
            if ( binary[i] == '0' )
                count++;
        if ( FLAG == 1 )
            if ( binary[i] == '1' )
                count++;
    }
    return count;
}

// written by me :-D
// dumps the ratio zero:one for 0-255
void dumpRange() {
    for ( int i=0; i<256; i++ )
        cout << num(decToBin(i), 0) << ":" << num(decToBin(i), 1) << endl;
}

// written by me :-D
// sorts integer list
void bubble_sort(int *list, int size) {
    bool sorted = false;
    while ( sorted != true ) {
        sorted = true;
        for ( int i=0; i<size-1; i++ ) {
            if ( list[i] > list[i+1] ) {
                sorted = false;
                int tmp;
                tmp = list[i+1];
                list[i+1] = list[i];
                list[i] = tmp;
            }
        }
    }
}

// written by me :-D
// maps a number to its ranking, by most zeroes first and then lexographically
int map(unsigned int NUMBER) {
    int *sorted = new int[256];
    for ( int i=0; i<256; i++ )
        sorted[i] = 0;
    int counter=0;
    for ( unsigned int z=8; z>=0; z-- )
        for ( unsigned int i=0; i<256; i++ )
            if ( num(decToBin(i), 0) == z ) {
                if ( i == NUMBER ) {
                    delete [] sorted;
                    return counter;
                }
                counter++;
            }
    delete [] sorted;
    return 0;
}

// written by me :-D
// maps a ranking to the lowest number it corresponds to
int inverse_map(unsigned int NUMBER) {
    int *sorted = new int[256];
    for ( int i=0; i<256; i++ )
        sorted[i] = 0;
    int counter=0;
    for ( int z=8; z >=0; z-- )
        for ( int i=0; i<256; i++ )
            if ( num(decToBin(i), 0) == z ) {
                if ( counter == NUMBER ) {
                    delete [] sorted;
                    return i;
                }
                counter++;

            }
    delete [] sorted;
    return 0;
}

int main()
{
    cout << map(128) << endl; // returns 8
    cout << inverse_map(8) << endl; // returns 128
    return 0;
}
 
Back
Top