Course Materials

Mockup Final Solutions 2

Question 1

1.a

5

x is passed by value, so addOne works on a copy. Incrementing the copy does not change a in main, which stays 5.

1.b

123

The condition i++ < 3 compares the current value of i and then increments it. So the loop checks 0 < 3 (i becomes 1, prints 1), then 1 < 3 (i becomes 2, prints 2), then 2 < 3 (i becomes 3, prints 3). Finally 3 < 3 is false, i becomes 4, and the loop ends.

1.c

5

a[1] is 2, so a[a[1]] = 5 is the same as a[2] = 5. The program then prints a[2], which is 5.

1.d

101 200

p points to arr[0]. The statement *p = *p + 1 changes the value stored at arr[0] from 100 to 101, and arr[1] is untouched.

1.e

Undefined behavior (prints 10 followed by a garbage value).

The format string "%d %d" expects two integers, but only one argument (x) is given. printf still tries to read a second value, so it prints whatever happens to be in memory at that position.

1.f

D1

x = 2 does not match any case, so execution jumps to default and prints D. There is no break after default, so fall-through continues into case 1, which prints 1. Then the break ends the switch. (The position of default does not matter; it only runs when no case matches.)

1.g

2.0

a and b are both integers, so a / b is integer division: 5 / 2 = 2. The result 2 is then converted to float and printed with one decimal place.

Question 2

void longestPrefix(char *str1, char *str2, char *result) {
    // Copy characters while both strings still match
    while (*str1 != '\0' && *str1 == *str2) {
        *result = *str1;
        str1++;
        str2++;
        result++;
    }

    // Terminate the result string
    *result = '\0';
}
void longestPrefix(char *str1, char *str2, char *result) {
    // Copy characters while both strings still match
    while (*str1 != '\0' && *str1 == *str2) {
        *result = *str1;
        str1++;
        str2++;
        result++;
    }

    // Terminate the result string
    *result = '\0';
}

Question 3

int minChanges(char *str) {
    if (str[0] == '\0') return 0;

    int changes = 0;
    int count = 1;  // length of the current run of identical characters

    for (int i = 1; str[i] != '\0'; i++) {
        if (str[i] == str[i - 1]) {
            count++;
        } else {
            // A run of length L needs L / 3 changes
            changes += count / 3;
            count = 1;
        }
    }
    changes += count / 3;  // the last run

    return changes;
}
int minChanges(char *str) {
    if (str[0] == '\0') return 0;

    int changes = 0;
    int count = 1;  // length of the current run of identical characters

    for (int i = 1; str[i] != '\0'; i++) {
        if (str[i] == str[i - 1]) {
            count++;
        } else {
            // A run of length L needs L / 3 changes
            changes += count / 3;
            count = 1;
        }
    }
    changes += count / 3;  // the last run

    return changes;
}

Question 4

void partitionZero(int arr[], int size) {
    int left = 0;           // next position for a negative number
    int right = size - 1;   // next position for a positive number
    int i = 0;

    // Three-way partition (Dutch National Flag) with 0 as the pivot.
    // The 0 is never moved past a negative or a positive, so it ends up between the two groups.
    while (i <= right) {
        if (arr[i] < 0) {
            // Swap with the left side, then move both forward
            int temp = arr[i];
            arr[i] = arr[left];
            arr[left] = temp;
            left++;
            i++;
        } else if (arr[i] > 0) {
            // Swap with the right side; do not move i, the swapped-in value is unchecked
            int temp = arr[i];
            arr[i] = arr[right];
            arr[right] = temp;
            right--;
        } else {
            // It is the 0: leave it and move on
            i++;
        }
    }
}
void partitionZero(int arr[], int size) {
    int left = 0;           // next position for a negative number
    int right = size - 1;   // next position for a positive number
    int i = 0;

    // Three-way partition (Dutch National Flag) with 0 as the pivot.
    // The 0 is never moved past a negative or a positive, so it ends up between the two groups.
    while (i <= right) {
        if (arr[i] < 0) {
            // Swap with the left side, then move both forward
            int temp = arr[i];
            arr[i] = arr[left];
            arr[left] = temp;
            left++;
            i++;
        } else if (arr[i] > 0) {
            // Swap with the right side; do not move i, the swapped-in value is unchecked
            int temp = arr[i];
            arr[i] = arr[right];
            arr[right] = temp;
            right--;
        } else {
            // It is the 0: leave it and move on
            i++;
        }
    }
}

Question 5

int maxMergedInterval(int a[], int b[], int size) {
    int maxSize = -1;

    for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
            // Overlap test
            int maxStart = (a[i] > a[j]) ? a[i] : a[j];
            int minEnd = (b[i] < b[j]) ? b[i] : b[j];

            if (maxStart <= minEnd) {
                // Merge the two intervals and measure the result
                int minStart = (a[i] < a[j]) ? a[i] : a[j];
                int maxEnd = (b[i] > b[j]) ? b[i] : b[j];

                int mergedSize = maxEnd - minStart;
                if (mergedSize > maxSize) {
                    maxSize = mergedSize;
                }
            }
        }
    }

    return maxSize;
}
int maxMergedInterval(int a[], int b[], int size) {
    int maxSize = -1;

    for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
            // Overlap test
            int maxStart = (a[i] > a[j]) ? a[i] : a[j];
            int minEnd = (b[i] < b[j]) ? b[i] : b[j];

            if (maxStart <= minEnd) {
                // Merge the two intervals and measure the result
                int minStart = (a[i] < a[j]) ? a[i] : a[j];
                int maxEnd = (b[i] > b[j]) ? b[i] : b[j];

                int mergedSize = maxEnd - minStart;
                if (mergedSize > maxSize) {
                    maxSize = mergedSize;
                }
            }
        }
    }

    return maxSize;
}