////////////////////////////////////////////////////////////////////////////////
// QUESTION 1
////////////////////////////////////////////////////////////////////////////////

#include 
#include 

#define MAX_SIZE 100

// sum_between_negatives should return the sum of all values between the first
// negative number and the last negative number in the array.
// If there are fewer than two negative numbers, it should return 0.
int sum_between_negatives(int size, int array[MAX_SIZE]) {
    int first_negative = -1;
    int last_negative = -1;

    for (int i = 0; i < size; i++) {
        if (array[i] < 0) {
            if (first_negative == -1) {
                first_negative = i;
            }
            last_negative = i;
        }
    }

    int sum = 0;
    if (first_negative != last_negative) {
        for (int i = first_negative + 1; i < last_negative; i++) {
            sum += array[i];
        }
    }

    return sum;
}

// This is a simple main function which could be used
// to test your sum_between_negatives function.
// It will not be marked.
// Only your sum_between_negatives function will be marked.
int main(void) {
    int test_array[MAX_SIZE] = {3, -2, 4, 5, -1, 6, -3, 8};

    // If you modify the above array for your own testing, 
    // you may also need to modify the size variable below accordingly
    int size = 8;

    int result = sum_between_negatives(size, test_array);

    // Example Explanation:
    //    {3, -2, 4, 5, -1, 6, -3, 8}
    //           | 4 + 5 + -1 + 6 = 14 |
    //
    //    The first negative number is -2.
    //    The last negative number is -3.
    //    The sum of all values between them is 14.
    printf("%d\n", result);

    return 0;
}

////////////////////////////////////////////////////////////////////////////////
// QUESTION 2
////////////////////////////////////////////////////////////////////////////////

#include 

#define MAX_ROWS 100
#define NUM_COLS 6

// first_balanced_row returns the index of the first row
// with equal number of vowels and consonants. 
// If no rows are balanced, the function should return -1.
int first_balanced_row(int num_rows, char array[MAX_ROWS][NUM_COLS]) {

    int vowels = 0;
    int consonants = 0;

    for (int i = 0; i < num_rows; i++) {
        vowels = 0;
        consonants = 0;

        for (int j = 0; j < NUM_COLS; j++) {
            if (array[i][j] == 'a' || 
                array[i][j] == 'e' || 
                array[i][j] == 'i' || 
                array[i][j] == 'o' || 
                array[i][j] == 'u') {
                    vowels++;
                } else {
                    consonants++;
                }
        }

        if (vowels == consonants) {
            return i;
        }
    }

    return -1;
}

// This is a simple main function which could be used
// to test your first_all_positive_column function.
// It will not be marked.
// Only your first_all_positive_column function will be marked.
int main(void) {
    char test_array[MAX_ROWS][NUM_COLS] = {
        {'a', 'e', 'i', 'o', 'u', 'a'},
        {'z', 'a', 'x', 'i', 'p', 's'},
        {'t', 'p', 'd', 'a', 'e', 'i'}
    };

    // Note: if you change test_array, update the first argument below
    // to match the new number of rows.
    int result = first_balanced_row(3, test_array);

    // Example Explanation:

    // Row 0 contains 6 vowels and 0 consonants, so it is not balanced.

    // Row 1 contains 2 vowels ('a' and 'i') and
    // 4 consonants ('z', 'x', 'p' and 's'), so it is not balanced.

    // Row 2 contains 3 vowels ('a', 'e' and 'i') and
    // 3 consonants ('t', 'p' and 'd'), so it is balanced.

    // Therefore, the function should return 2.

    printf("%d\n", result);

    return 0;
}

////////////////////////////////////////////////////////////////////////////////
// QUESTION 3
////////////////////////////////////////////////////////////////////////////////

// This program should first scan in the leaving time. It should then scan in 3 
// wake up times along with their corresponding getting ready times into a 
// struct array and determine which days are late (has a wake time and getting 
// ready time later than the leaving time). 
//
// Unfortunately, this code contains a number of errors. 
// It's your job to fix them, good luck!

#include 

#define SIZE 3
#define HOURS_TO_MINUTES 60

struct day {
    int wake_time;   // in minutes since midnight
    int getting_ready_time;
};

// BUG: Using -> instead of . for all struct field access
int main(void) {

    int leave_hour;
    int leave_min;
    printf("Enter leaving time: ");
    scanf("%d %d", &leave_hour, &leave_min);

    int leave_time = leave_hour * HOURS_TO_MINUTES + leave_min;

    printf("Enter wake up time and time it took to get ready for 3 days: \n");

    struct day days[SIZE];
    for (int i = 0; i < SIZE; i++) {
        printf("Day %d\n", i + 1);
        
        int wake_up_hour;
        int wake_up_min;
        printf("Enter wake up time: ");
        scanf("%d %d", &wake_up_hour, &wake_up_min);
        days[i].wake_time = wake_up_hour * HOURS_TO_MINUTES + wake_up_min;
        printf("Enter how long it took to get ready (minutes): ");
        scanf("%d", &days[i].getting_ready_time);
    }

    printf("Checking if you were late...\n");

    int j = 0; // BUG FIX: Start at 0 not 1
    int late_count = 0;
    // BUG: j == SIZE instead of <
    while (j < SIZE) {
        // BUG: using <= instead of >
        if (days[j].wake_time + days[j].getting_ready_time > leave_time) {
            late_count++;
        }
        j++; // BUG FIX: Added missing increment
    }
    
    // BUG: using != instead of ==
    if (late_count == SIZE) {
        printf("Late every day!\n");
    } else if (late_count == 0) {
        printf("Great! You were not late!\n");
    } else {
        // BUG: printing j and not late_count
        printf("You were late for %d day(s).\n", late_count);
    }
   
    return 0;
}

////////////////////////////////////////////////////////////////////////////////
// QUESTION 4
////////////////////////////////////////////////////////////////////////////////


// The following code is meant to prompt the user to enter a string.
// It should then replace each capital letter with the total count of capital 
// letters in the string, up to and including that character.
// The updated string is then printed to the terminal, as well as the total 
// count of capital letters
//
// Unfortunately, this code contains a number of errors. 
// It's your job to fix them, good luck!

#include 
#include 

#define SIZE 50

int replace_uppercase_letters(char string[SIZE]);
void strip_newline(char *string);

int main(void) {
    char str[SIZE]; // BUG: was missing size

    printf("Enter a string: ");
    fgets(str, SIZE, stdin); // BUG: was missing stdin argument
    strip_newline(str);

    int count = replace_uppercase_letters(str);

    printf("Result: %s\n", str); // BUG: %c instead of %s
    printf("There are %d capital letters in the string.\n", count);

    return 0;
}

int replace_uppercase_letters(char string[SIZE]) {
    int i = 0;
    int count = 0;
    while (string[i] != '\0') {
        if ('A' <= string[i] && string[i] <= 'Z') { // BUG: || instead of &&
            count += 1; // BUG: was swapped with line below
            string[i] = count + '0'; // BUG: was missing + '0' (I'm so mean)
        }
        i++;
    }
    return count; // BUG: returned i instead
}

////////////////////////////////////////////////////////////////////////////////
//                  DO NOT CHANGE ANY OF THE CODE BELOW HERE                  //
//      The code below is correct as given. You do NOT need to modify it      //
////////////////////////////////////////////////////////////////////////////////

// Removes a newline character from end of the given string, if it exists.
void strip_newline(char *string) {
    int length = strlen(string);
    if (length != 0 && string[length - 1] == '\n') {
        string[length - 1] = '\0';
    }
}