Introduction to Computer Science - Exercise 4

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


Loops

5.

Code:

/* parity.cpp
 * This program calculates the parity of an ID.
 *
 * Saggi Mizrahi 05 November 2013
 */
#include <iostream>

using namespace std;

/* Calculate the parity of an IDS
 * Input: An ID without leading 0 or parity.
 * Output: ID including parity.
 */
int main()
{
    // declaration of variables
    int ID = -1;
    int digit = -1;
    int digitMultiple = -1;
    int parity = -1;

    int multiplier = 2;
    int digitSum = 0;

    // Get user input

    cout << "please enter your ID: ";
    cin >> ID;

    // Calculate
    for(int tmp = ID; tmp > 0; tmp /= 10) {
        digit = tmp % 10;
        digitMultiple = digit * multiplier;

        // Sum up the digits of the multiple
        for (int tmp = digitMultiple; tmp > 0; tmp /= 10) {
            digitSum += tmp % 10;
        }

        // flip the multiplier
        multiplier = multiplier == 1 ? 2 : 1;

    }

    parity = 10 - (digitSum % 10);

    // outputting the result
    cout << ID << parity << endl;
}

Outputs:

Check given example:

please enter your ID: 56287
562876

Check my ID:

please enter your ID: 3249312
32493124

6.

Code:

/* equasion.cpp
 * This program finds the number of solutions for Ax+By+Cz=D where
 * -N/2 <= x, y, z <= N/2.
 *
 * Saggi Mizrahi 12 November 2013
 */
#include <iostream>

using namespace std;

/* Find the number of solutions for Ax+By+Cz=D and print it out.
 * Input: 5 number A B C D and N.
 * Output: Summery of the input the number of solution and an example of a
 * solution.
 */
int main()
{
    // declaration of variables
    int a = -1;
    int b = -1;
    int c = -1;
    int d = -1;
    int n = -1;

    int min = 0;
    int max = 0;
    int remainder = 0;
    int numSolutions = 0;

    int correctX = 0;
    int correctY = 0;
    int correctZ = 0;

    // Get user input

    cout << "please enter 5 numbers: ";
    cin >> a >> b >> c >> d >> n;

    // Calculate range

    min = -1 * (n / 2);
    max = n / 2;

    // Find solutions
    for (int x = min; x <= max; x++) {
        for (int y = min; y <= max; y++) {
            remainder = d;
            remainder -= a * x;
            remainder -= b * y;
            if ((remainder % c) == 0) {
                remainder /= c;
                if (remainder >= min && remainder <=max) {
                    numSolutions++;
                    correctX = x;
                    correctY = y;
                    correctZ = remainder;
                }
            }
        }
    }

    // outputting the result
    cout << "There are " << numSolutions << " solutions for " <<
        "(" << a << " * x) + (" << b << " * y) + (" << c <<
        " * z) = " << d << endl;
    cout << "Where: " << min << " <= x, y, z <= " << max << endl;
    cout << "One such solution is (" << correctX << ", " << correctY <<
        ", " << correctZ << ")" << endl;
}

Outputs:

Check given example:

please enter 5 numbers: 2 -3 -1 5 4
There are 6 solutions for (2 * x) + (-3 * y) + (-1 * z) = 5
Where: -2 <= x, y, z <= 2
One such solution is (2, 0, -1)

please enter 5 numbers: 4 -18 -1 30 10
There are 6 solutions for (4 * x) + (-18 * y) + (-1 * z) = 30
Where: -5 <= x, y, z <= 5
One such solution is (4, -1, 4)

please enter 5 numbers: 4 -18 -1 30 100
There are 573 solutions for (4 * x) + (-18 * y) + (-1 * z) = 30
Where: -50 <= x, y, z <= 50
One such solution is (50, 12, -46)

please enter 5 numbers: 4 -18 -1 30 1000
There are 55723 solutions for (4 * x) + (-18 * y) + (-1 * z) = 30
Where: -500 <= x, y, z <= 500
One such solution is (500, 137, -496)

7א.

Code:

/* romansA.cpp
 * This program prints out an input number in roman numerals.
 *
 * Saggi Mizrahi 12 November 2013
 */
#include <iostream>

using namespace std;

/* Print a number in roman numerals.
 * Input: A number.
 * Output: The number in roman numerals.
 */
int main()
{
    // declaration of variables
    int number = -1;

    // Get user input

    cout << "please enter a number: ";
    cin >> number;

    // Print out number
    while(number >= 1000) {
        cout << "M";
        number -= 1000;
    }

    while(number >= 500) {
        cout << "D";
        number -= 500;
    }

    while(number >= 100) {
        cout << "C";
        number -= 100;
    }

    while(number >= 50) {
        cout << "L";
        number -= 50;
    }

    while(number >= 10) {
        cout << "X";
        number -= 10;
    }

    while(number >= 5) {
        cout << "V";
        number -= 5;
    }

    while(number >= 5) {
        cout << "V";
        number -= 5;
    }

    while(number > 0) {
        cout << "I";
        number--;
    }

    cout << endl;
}

Outputs:

please enter a number: 9
VIIII

please enter a number: 10
X

please enter a number: 2013
MMXIII

please enter a number: 1986
MDCCCCLXXXVI

7ב.

Code:

/* romansB.cpp
 * This program prints out an input roman number in arabaic numerals.
 *
 * Saggi Mizrahi 12 November 2013
 */
#include <iostream>

using namespace std;

/* Print a roman number in arabaic numerals.
 * Input: A roman number.
 * Output: The roman number in arabaic numerals.
 */
int main()
{
    // decleration of constants
    const int M_VALUE = 1000;
    const int D_VALUE = 500;
    const int C_VALUE = 100;
    const int L_VALUE = 50;
    const int X_VALUE = 10;
    const int V_VALUE = 5;
    const int I_VALUE = 1;

    const char EOI = '\n';

    // declaration of variables
    char c;
    char lastChar = 'E';
    int result = 0;
    int lastValue = M_VALUE;
    int value = -1;
    int singletonCount = 0;
    int quadroCount = 0;

    // Get user input

    cout << "please enter a number: ";
    while ((c = cin.get()) != EOI) {
        if (lastChar != c) {
            singletonCount = 0;
            quadroCount = 0;
        }
        switch (c) {
            case 'M':
                value = M_VALUE;
                break;
            case 'D':
                singletonCount++;
                value = D_VALUE;
                break;
            case 'C':
                quadroCount++;
                value = C_VALUE;
                break;
            case 'L':
                singletonCount++;
                value = L_VALUE;
                break;
            case 'X':
                quadroCount++;
                value = X_VALUE;
                break;
            case 'V':
                singletonCount++;
                value = V_VALUE;
                break;
            case 'I':
                quadroCount++;
                value = I_VALUE;
                break;
            default:
                cout << "Invalid input, unknown value for '" <<
                    c << "'" << endl;
                return -1;
        }

        if (value > lastValue) {
            cout << "Invalid input, '" << c << "' after '" <<
                lastChar << "'" << endl;
            return -1;
        };

        if (singletonCount > 1) {
            cout << "Invalid input, '" << c <<
                "' can only appear once" << endl;
            return -1;
        }

        if (quadroCount > 4) {
            cout << "Invalid input, '" << c <<
                "' can only appear up to four times" << endl;
            return -1;
        }

        result += value;

        lastValue = value;
        lastChar = c;
    }

    // Print out result
    cout << result << endl;
    return 0;
}

Outputs:

please enter a number: VIII
8

please enter a number: X
10

please enter a number: XM
Invalid input, 'M' after 'X'

please enter a number: LL
Invalid input, 'L' can only appear once

please enter a number: XXXXXX
Invalid input, 'X' can only appear up to four times

Functions

1.

a)

main: k = a
f: k = a
main: k = a

b)

main: k = a
f: k = a
main: k = 3

c)

main: k = a
g: k = a
g: k = 7
main: k = a

d)

main: k = a
g: k = a
g: k = 7
main: k = 7

e)

return 0 
return 1 

f)

h1: k = 12
h1: k = 0
return 1 
return 1

g)

expected primary-expression before int

2.

Code:

/* maxmin.cpp
 * This program prints out the largest and smallest of 3 numbers
 *
 * Saggi Mizrahi 12 November 2013
 */
#include <iostream>

using namespace std;

double maximum(double a, double b, double c);
double minimum(double a, double b, double c);

double maximum(double a, double b, double c) {
    if (a > b) {
        b = a;
    }
    if (b > c) {
        return b;
    } else {
        return c;
    }
}

double minimum(double a, double b, double c) {
    return -1 * maximum(-1 * a, -1 * b, -1 * c);
}

/* Prints out the largest and smallest of 3 number.
 * Input: 3 numbers.
 * Output: the largest and smallest value entered.
 */
int main()
{
    // declaration of variables
    double a, b, c;

    // Get user input

    cout << "please enter 3 numbers: ";
    cin >> a >> b >> c;

    cout << "min: " << minimum(a, b, c) << ", max: " << maximum(a, b, c) <<
        endl;
    return 0;
}