Introduction to Computer Science - Exercise 8

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


Structs

1.

Code:

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

using namespace std;

const int MAX_NAME = 20;
const int MIN_GOOD_GRADE = 80;

struct student {
    char name[MAX_NAME];
    int id;
    double averageGrade;
};
typedef struct student STUDENT;

double average(STUDENT students[], int size);
void printGoodStudents(STUDENT students[], int size);

double average(STUDENT students[], int size) {
    double sum = 0;
    for (int i = 0; i < size; i++) {
        sum += students[i].averageGrade;
    }
    return sum / size;
}

void printGoodStudents(STUDENT students[], int size) {
    STUDENT student;
    for (int i = 0; i < size; i++) {
        student = students[i];
        if (student.averageGrade > MIN_GOOD_GRADE) {
            cout << student.name << endl;
        }
    }
}

int main()
{
    const int NUM_SUDENTS = 5;
    STUDENT students[NUM_SUDENTS] = {};
    STUDENT *student;
    cout << "Please enter student information: " << endl;
    for (int i = 0; i < NUM_SUDENTS; i++) {
        student = &students[i];
        cin >> student->id >> student->averageGrade >> student->name;
    }

    cout << endl;
    cout << "The class average is: " << average(students, NUM_SUDENTS) <<
        endl;
    cout << "The good students are: " << endl;
    printGoodStudents(students, NUM_SUDENTS);
}

Outputs:

Please enter student information: 
1 100 Saggi
2 20 Somone
3 81 Feelgood
4 40 Beck
5 99 AlMost

The class average is: 68
The good students are: 
Saggi
Feelgood
AlMost

3.

Code:

/* polynom.cpp
 *
 * Saggi Mizrahi 09 December 2013
 */
#include <iostream>

using namespace std;

struct array {
    int* arr;
    int size;
};
typedef struct array ARRAY;

void sortByLength(ARRAY* pyramid, int size);
void swap(ARRAY* arr, int a, int b);
void freeArray(ARRAY &arr);
void printArray(const ARRAY &arr);

void freeArray(ARRAY &arr) {
    delete []arr.arr;
    arr.arr = NULL;
    arr.size = 0;
}

void printArray(const ARRAY &arr) {
    cout << "[";
    if (arr.size > 0) {
        cout << arr.arr[0];
    }
    for (int i = 1; i < arr.size; i++) {
        cout << ", " << arr.arr[i];
    }
    cout << "]";
}

void swap(ARRAY* arr, int a, int b) {
    ARRAY tmp = arr[a];
    arr[a] = arr[b];
    arr[b] = tmp;
}

void sortByLength(ARRAY* pyramid, int size) {
    int i = 0;
    ARRAY* current;
    while (i < size) {
        current = &pyramid[i];
        if (current->size == (i + 1)) {
            i++;
        } else {
            swap(pyramid, i, current->size - 1);
        }
    }
}

int main()
{
    const int NUM_ARRAYS = 5;
    ARRAY arrays[NUM_ARRAYS] = {};
    ARRAY *array;
    cout << "Please enter array information: " << endl;
    for (int i = 0; i < NUM_ARRAYS; i++) {
        array = &arrays[i];
        cin >> array->size;
        array->arr = new int[array->size];
        for (int j = 0; j < array->size; j++) {
            cin >> array->arr[j];
        }
    }

    sortByLength(arrays, NUM_ARRAYS);
    cout << endl;
    cout << "The sorted array is: " << endl;
    for (int i = 0; i < NUM_ARRAYS; i++) {
        printArray(arrays[i]);
        freeArray(arrays[i]);
        cout << endl;
    }
}

Outputs:

Please enter array information: 
2    1 2
3    1 1 1
1    9
5    5 4 3 2 1
4    3 3 3 3

The sorted array is: 
[9]
[1, 2]
[1, 1, 1]
[3, 3, 3, 3]
[5, 4, 3, 2, 1]

4.

Code:

/* polynom.cpp
 *
 * Saggi Mizrahi 09 December 2013
 */
#include <iostream>

using namespace std;

struct monom {
    int coefficient;
    int power;
};
typedef struct monom MONOM;

void realloc(MONOM*&poly, int currSize, int newSize);
void normalize(MONOM* poly, int &size);
void swap(MONOM* poly, int a, int b);
int abs(int i);
void printMonom(const MONOM &m);

void swap(MONOM* poly, int a, int b) {
    MONOM tmp = poly[a];
    poly[a] = poly[b];
    poly[b] = tmp;
}

void copyPoly(MONOM* dst, const MONOM* src, int n) {
    for (int i = 0; i < n; i++) {
        dst[i] = src[i];
    }
}

void realloc(MONOM*& poly, int currSize, int newSize) {
    MONOM* oldPtr = poly;
    poly = new MONOM[newSize];
    copyPoly(poly, oldPtr, currSize);
    delete [] oldPtr;
}

MONOM* readPolynom(int &n) {
    n = 0;
    int capacity = 2;
    MONOM tmp = {0, 0};
    MONOM* result = new MONOM[capacity];
    cin >> tmp.coefficient >> tmp.power;
    while (tmp.power > -1) {
        if (n == capacity) {
            capacity *= 2;
            realloc(result, n, capacity);
        }
        result[n] = tmp;
        n++;
        cin >> tmp.coefficient >> tmp.power;
    }
    normalize(result, n);
    return result;
}

void sort(MONOM* poly, int size) {
    int n = size - 1;
    MONOM* current;
    MONOM* next;
    bool hasSwapped = true;
    for (int i = 0; i < n && hasSwapped; i++) {
        hasSwapped = false;
        for (int j = 0; j < (n - i); j++) {
            current = &poly[j];
            next = &poly[j + 1];
            if (current->power < next->power) {
                hasSwapped = true;
                swap(poly, j, j + 1);
            }
        }
    }
}

void compact(MONOM* poly, int &size) {
    MONOM* current = poly;
    MONOM* next = current + 1;
    MONOM* end = poly + size;
    while (next < end) {
        if (next->power == current->power) {
            current->coefficient += next->coefficient;
            next->coefficient = 0;
            size--;
            next++;
        } else if (current->coefficient == 0) {
            current->power = next->power;
        } else {
            current++;
            if (current == next) {
                next++;
            } else {
                size++;
            }
        }
    }
}

void normalize(MONOM* poly, int &size) {
    sort(poly, size);
    compact(poly, size);
}

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

void mulMonom(const MONOM &a, const MONOM &b, MONOM &out) {
    out.power = a.power + b.power;
    out.coefficient = a.coefficient * b.coefficient;
}

void deriveMonom(MONOM &m) {
    m.coefficient *= m.power;
    m.power--;
}

void derivePoly(MONOM* poly, int &size) {
    MONOM* end = poly + size;
    for(MONOM* m = poly; m < end; m++) {
        if (m->power == 0) {
            size--;
        } else {
            deriveMonom(*m);
        }
    }
}

void printMonom(const MONOM &m) {
    int abscoef;
    if (m.coefficient < 0) {
        cout << "- ";
    }
    abscoef = abs(m.coefficient);
    if (abscoef > 1) {
        cout << abscoef;
    }
    if (m.power > 0) {
        cout << "x";
    }
    if (m.power > 1) {
        cout << "^" << m.power;
    }
}
void printPoly(const MONOM* poly, int n) {
    if (n > 0) {
        printMonom(poly[0]);
    }
    for (int i = 1; i < n; i++) {
        cout <<" ";
        if (poly[i].coefficient >= 0) {
            cout <<"+ ";
        }
        printMonom(poly[i]);
    }
}

MONOM* sumPoly(const MONOM* a, int an, const MONOM* b, int bn, int &n) {
    n = an + bn;
    MONOM* result = new MONOM[n];
    copyPoly(result, a, an);
    copyPoly(result + an, b, bn);
    normalize(result, n);
    return result;
}

MONOM* mulPoly(const MONOM* a, int an, const MONOM* b, int bn, int &n) {
    n = an * bn;
    MONOM* result = new MONOM[n];
    MONOM* current = result;
    for (int i = 0; i < an; i++) {
        for (int j = 0; j < bn; j++) {
            mulMonom(a[i], b[j], *current);
            current++;
        }
    }
    normalize(result, n);
    return result;
}

int main()
{
    int an;
    int bn;
    int sumn;
    int muln;
    cout << "enter polynom: ";
    MONOM* polyA = readPolynom(an);
    MONOM* polyB = new MONOM[an];
    MONOM* sum;
    MONOM* mul;

    bn = an;
    copyPoly(polyB, polyA, an);
    cout << "Input: "; printPoly(polyA, an); cout << endl;
    derivePoly(polyB, bn);
    cout << "Drivative: "; printPoly(polyB, bn); cout << endl;
    sum = sumPoly(polyA, an, polyB, bn, sumn);
    cout << "Sum: "; printPoly(sum, sumn); cout << endl;
    mul = mulPoly(polyA, an, polyB, bn, muln);
    cout << "Multiply: "; printPoly(mul, muln); cout << endl;
    delete [] polyA;
    delete [] polyB;
    delete [] sum;
    delete [] mul;
}

Outputs:

enter polynom: 2 5 4 3 1 2 0 -1
Input: 2x^5 + 4x^3 + x^2
Drivative: 10x^4 + 12x^2 + 2x
Sum: 2x^5 + 10x^4 + 4x^3 + 13x^2 + 2x
Multiply: 20x^9 + 64x^7 + 14x^6 + 48x^5 + 20x^4 + 2x^3

enter polynom: 1 2 1 2 1 2 3 1 7 0 0 -1
Input: 3x^2 + 3x + 7
Drivative: 6x + 3
Sum: 3x^2 + 9x + 10
Multiply: 18x^3 + 27x^2 + 51x + 21

enter polynom: 1 2 4 3 2 5 4 2 0 -1
Input: 2x^5 + 4x^3 + 5x^2
Drivative: 10x^4 + 12x^2 + 10x
Sum: 2x^5 + 10x^4 + 4x^3 + 17x^2 + 10x
Multiply: 20x^9 + 64x^7 + 70x^6 + 48x^5 + 100x^4 + 50x^3

enter polynom: 1 2 2 5 2 5 -4 5 4 2 0 -1
Input: 5x^2
Drivative: 10x
Sum: 5x^2 + 10x
Multiply: 50x^3