Introduction to Computer Science - Exercise 11

Name: Saggi Mizrahi • ID: 032493124 • Group: מעוף א'Date: December 30, 2013


Recursion

10.

Code:

/* 11boom.cpp
 *
 * Saggi Mizrahi 30 December 2013
 */
#include <iostream>

using namespace std;

int sumNum(unsigned int num);
unsigned int abs(int num);
bool dividesBy11(unsigned int num);

unsigned int abs(int num) {
    return num < 0 ? -num : num;
}

int sumNum(unsigned int num) {
    int digit1, digit2;
    if (num < 10) {
        return num;
    } else {
        digit1 = num % 10;
        num /= 10;
        digit2 = num % 10;
        return digit1 - digit2 + sumNum(num / 10);
    }
}

bool dividesBy11(unsigned int num) {
    if (num < 10) {
        return num == 0;
    } else {
        return dividesBy11(abs(sumNum(num)));
    }
}

// prints out if the input file divides by 11
// input: a number
// output: whether it divides by 11 without a remainder
int main()
{
    int n;
    cout << "enter a number: ";
    cin >> n;
    cout << "The number does ";
    if (!dividesBy11(n)) {
        cout << "not ";
    }
    cout << "divide by 11" << endl;
}

Outputs:

enter a number: 40
The number does not divide by 11

enter a number: 121
The number does divide by 11

enter a number: 11
The number does divide by 11

11.

Code:

/* coord.cpp
 *
 * Saggi Mizrahi 30 December 2013
 */
#include <iostream>

using namespace std;

int networkNum(unsigned int x, unsigned int y);

int networkNum(unsigned int x, unsigned int y) {
    int num = 0;
    if (x > 0) {
        num += networkNum(x - 1, y);
    }
    if (y > 0) {
        num += networkNum(x, y - 1);
    }

    if (x == 0 && y == 0) {
        return 1;
    } else {
        return num;
    }
}

// prints the number of networks a coordinate has
// input: two integers
// output: the number of networks a coordinate has
int main()
{
    unsigned int x, y;
    cout << "enter a coordinate: ";
    cin >> x >> y;
    cout << networkNum(x, y);
}

Outputs:

enter a coordinate: 2 1
3

enter a coordinate: 9 9
48620

enter a coordinate: 0 0
1

Pointers

6.

Code:

/* threescompany.cpp
 *
 * Saggi Mizrahi 04 December 2013
 */
#include <iostream>

using namespace std;

// ** <heapsort> ** //

// method decleration
void swap(int** a, int i1, int i2);
void heapsort(int** a, int n);
void heapify(int** a, int n);
void siftDown(int** a, int i, int n);
void siftUp(int** a, int i);


// swap 2 integers pointers in an array
void swap(int** a, int i1, int i2) {
    int* tmp = a[i1];
    a[i1] = a[i2];
    a[i2] = tmp;
}

// reorder misplaced child in heap
void siftUp(int** a, int i) {
    // stop if reached root of heap
    if (i == 0) {
        return;
    }

    int parentIdx = (i - 1) / 2;
    if (*a[i] > *a[parentIdx]) {
        swap(a, parentIdx, i);
        siftUp(a, parentIdx);
    }
}

// reorder misplaced parent in heap
void siftDown(int** a, int i, int n) {
    // stop if node has no children
    if (((i + 1) * 2) >= n) {
        return;
    }

    int son1Idx = (i * 2) + 1;
    int son2Idx = son1Idx + 1;
    int sonIdx = *a[son1Idx] > *a[son2Idx] ? son1Idx : son2Idx;
    if (*a[sonIdx] > *a[i]) {
        swap(a, sonIdx, i);
        siftDown(a, sonIdx, n);
    }
}

// Turn a random array in to a heap
void heapify(int** a, int n) {
    for (int i = 1; i < n; i++) {
        siftUp(a, i);
    }
}

// Sort an array using heapsort
void heapsort(int** a, int n) {
    heapify(a, n);

    // Sort the heap
    for (int i = 0; i < n; i++) {
        swap(a, 0, n - i - 1);
        siftDown(a, 0, n - i - 2);
    }
}

// ** </heapsort> ** //

// Print repeated pointed values and their indeces in relation to offset.
void printRepeatedItems(int** a, int n, int k, int* offset) {
    int** cache = new int*[k];
    int cacheIdx = 0;
    int c = -1;
    bool printed = false;
    for (int i = 0; i < n; i++) {
        if (*a[i] != c) {
            if (printed) {
                cout << endl;
                printed = false;
            }
            c = *a[i];
            cache[0] = 0;
            cacheIdx = 0;
        }
        if (cacheIdx < k) {
            cache[cacheIdx] = a[i];
            cacheIdx++;
        }
        if (cacheIdx == k) {
            printed = true;
            cout << c << ": ";
            cout << cache[0] - offset;
            for (int j = 1; j < k; j++) {
                cout << ", ";
                cout << cache[j] - offset;
            }
            cacheIdx++;
        } else if (cacheIdx > k) {
            cout << ", " << a[i] - offset;
        }
    }
    if (printed) {
        cout << endl;
    }
    delete [] cache;
}

void pointerArr(int* b[], int a[], int n) {
    for (int i = 0; i < n; i++) {
        b[i] = &a[i];
    }
}

const int SUCCESS = 0;
const int ALLOCATION_ERROR = -1;

/* reallocates an array.
 * a must be heap allocated.
 * returns 0 on success.
*/
int realloc(int* &a, int size, int new_size) {
    int* old = a;
    a = new int[new_size];
    if (!a) {
        a = old;
        return ALLOCATION_ERROR;
    }
    for (int i = 0; i < size; i++) {
        a[i] = old[i];
    }

    delete [] old;
    return 0;
}

int readPositiveNumbers(int* &a, int &size, int &capacity) {
    capacity = 2;
    size = 0;
    int num;
    int rc;
    a = new int[size];
    cin >> num;
    while (num != -1) {
        if (size >= capacity) {
            if ((rc = realloc(a, capacity, capacity * 2)) != SUCCESS) {
                delete [] a;
                return rc;
            }
            capacity *= 2;
        }
        a[size] = num;
        size++;
        cin >> num;
    }
    return 0;
}

int main()
{
    int* a;
    int n;
    int capacity;
    int** b;
    int rc;

    cout << "please enter numbers: ";
    if ((rc = readPositiveNumbers(a, n, capacity)) != SUCCESS) {
        return rc;
    }

    b = new int*[n];
    if (!b) {
        return ALLOCATION_ERROR;
    }
    pointerArr(b, a, n);
    heapsort(b, n);
    printRepeatedItems(b, n, 3, a);

    delete [] a;
    delete [] b;
    return 0;
}

Outputs:

please enter numbers: 2 3 4 2 2 5 2 4 3 4 2 -1
2: 6, 3, 0, 4, 10
4: 9, 7, 2

please enter numbers: 2 3 4 2 2 5 2 4 3 4 2 9 123 123 123 1289 3 1 1 1 32 1 1024 123 -1
1: 19, 21, 17, 18
2: 0, 6, 10, 3, 4
3: 8, 1, 16
4: 7, 9, 2
123: 12, 23, 14, 13

Arrays

2.

Code:

/* appear.cpp
 *
 * Saggi Mizrahi 30 December 2013
 */
#include <iostream>

using namespace std;

const char EOT = '\0';

struct appear {
    char ch;
    int n_appearances;
};
typedef struct appear APPEAR;

void appearances(char* str, APPEAR appear_arr[], int &n);

void appearances(char* str, APPEAR appear_arr[], int &n) {
    if (*str == EOT) {
        n = 0;
    } else {
        appearances(str + 1, appear_arr, n);
        for (int i = 0; i < n; i++) {
            if (appear_arr[i].ch == *str) {
                appear_arr[i].n_appearances++;
                return;
            }
        }
        appear_arr[n].ch = *str;
        appear_arr[n].n_appearances = 1;
        n++;
    }
}

// prints out the amount of times each letter appears in the input string
// input: a string
// output: the amount of times each letter appears in the input string
int main()
{
    const int SIZE = 100;
    char str[SIZE];
    int n;
    APPEAR res[SIZE];
    cout << "enter a string: ";
    cin.getline(str, SIZE);

    appearances(str, res, n);

    for (int i = 0; i < n; i++) {
        cout << res[i].ch << ":" << res[i].n_appearances << " ";
    }
    cout << endl;
}

Outputs:

enter a string: x3y#xy5y5
5:2 y:3 x:2 #:1 3:1 

enter a string: Hello_World
d:1 l:3 r:1 o:2 W:1 _:1 e:1 H:1 

enter a string: strings_are_fun
n:2 u:1 f:1 _:2 e:1 r:2 a:1 s:2 g:1 i:1 t:1