Week 4 Code Examples

#include <stdio.h>
//void print_2d_array(int array[][], int size_1, int size_2);

int main(void) {
    int size_1 = 2;
    int size_2 = 3;
    int array[size_1][size_2];
    for (int i = 0; i < size_1; i++) {
        for (int j = 0; j < size_2; j++) {
            printf("Enter a number to store in our grdi: ");
            scanf("%d", &array[i][j]);
        }
    }
    printf("{");
    for (int i = 0; i < size_1; i++) {
        for (int j = 0; j < size_2; j++) {
            printf("%d, ", array[i][j]);
        }
        printf("\n");
    }
    printf("}\n");
    int max_sum_so_far;
    int row_location = 0;
    for (int i = 0; i < size_1; i++) {
        int row_sum = 0;
        for (int j = 0; j < size_2; j++) {
            row_sum += array[i][j];
        }
        if (i == 0) {
            max_sum_so_far = row_sum;
        }
        if (row_sum > max_sum_so_far) {
            max_sum_so_far = row_sum;
            row_location = i;
        }
    }
    printf("The max sum of a row was %d located in row %d.\n", max_sum_so_far, row_location);
    return 0;
}

//void print_2d_array(int array[], int size_1, int size_2) {
//
//}
#include <stdio.h>

int main(void) {
    int user_input[10] = {};
    for (int i = 0; i < 10; i++) {
        printf("Enter an integer: ");
        scanf("%d", &user_input[i]);
    }
    printf("{");
    for (int i = 0; i < 10; i++) {
        printf("%d, ", user_input[i]);
    }
    printf("}\n");
    int counter = 0;
    for (int i = 0; i < 10; i++) {
        counter = 0;
        if (user_input[i] == 6 || user_input[i] == 7) {
            counter++;
        }
    }
    printf("We hit the 67 emote %d times!\n", counter);
    return 0;
}
#include <stdio.h>

void get_input(void);
int calculate_sum(int array[], int size);

int main(void) {
    get_input();
    return 0;
}

void get_input(void) {
    int user_input[10] = {};
    for (int i = 0; i < 10; i++) {
        printf("Enter an integer: ");
        scanf("%d", &user_input[i]);
    }
    int sum = calculate_sum(user_input, 10);
    printf("The total sum of odd numbers entered is %d.\n", sum);
}

int calculate_sum(int array[], int size) {
    int sum = 0;
    int i = 0;
    while (i < size) {
        if (array[i] % 2 == 1) {
            sum += array[i];
        }
        i++;
    }
    return sum;
}
#include <stdio.h>

int main(void) {
    int month[4][7] = {};
    int row = 0;
    while (row < 4) {
        int col = 0;
        while (col < 7) {
            printf("%d ", month[row][col]);
            col++;
        }
        printf("\n");
        row++;
    }
    return 0;
}
#include <stdio.h>

int main(void) {
    int grid[3][3];
    int row = 0;
    while (row < 3) {
        int col = 0;
        while (col < 3) {
            printf("Enter a number to store at row %d col %d: ", row, col);
            scanf("%d", &grid[row][col]);
            col++;
        }
        row++;
    }
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {
            if (grid[row][col] % 2 == 0) {
                printf("Even number %d found a row %d col %d!\n", grid[row][col], row, col);
            }
        }
    }
    return 0;
}
#include <stdio.h>

struct coordinate {
    int x;
    int y;
};

int main(void) {
    struct coordinate maps[3];
    return 0;
}
#include <stdio.h>

struct season {
    int rating;
    int episode_count;
};

int main(void) {
    struct season array[2];
    struct season borneo;
    struct season australia;
    borneo.rating = 10;
    borneo.episode_count = 14;
    array[0] = borneo;
    australia.rating = 6;
    australia.episode_count = 17;
    array[1] = australia;
    printf("%d\n", array[0].rating);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int num;
    num = atoi(argv[1]);
    printf("%d\n", num);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int sum = 0;
    for (int i = 1; i < argc; i++) {
        printf("The command line arguemnt at argv[%d] is %d.\n", i, atoi(argv[i]));
        sum += atoi(argv[i]);
    }
    printf("The sum is %d.\n", sum);
    return 0;
}
#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("The command line arguemnt at index 0 (argv[0] is %s\n", argv[0]);
    printf("The command line arguemnt at index 1 (argv[1] is %s\n", argv[1]);
    printf("The command line arguemnt at index 2 (argv[2] is %s\n", argv[2]);
    printf("The command line arguemnt at index 3 (argv[3] is %s\n", argv[3]);
    return 0;
}
#include <stdio.h>

int main(int argc, char *argv[]) {
    int i = 0;
    while (i < argc) {
        printf("The argument at index %d was %s (argv[%d])\n", i, argv[i], i);
        i++;
    }
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    if (argc > 2) {
        int num_1 = atoi(argv[1]);
        int num_2 = atoi(argv[2]);
        if (num_1 == num_2) {
            printf("The two command line numbers are the same!\n");
        }
        else {
            printf("The two command line numbers are not the same!\n");
        }
    }
    else {
        printf("You did not enter at least two command line arguments!\n");
    }
    return 0;
}
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    if (argc > 2) {
        if (strcmp(argv[1], argv[2]) == 0) {
            printf("The two strings are equal!\n");
        }
        else {
            printf("The two strings are not equal!\n");
        }
    }
    else {
        printf("You did not enter at least two command line arguments!\n");
    }
    return 0;
}
#include <stdio.h>
#define MAX_SIZE 64

int main(void) {
    char array[MAX_SIZE];
    printf("Enter a string: ");
    fgets(array, MAX_SIZE, stdin);
    int i = 0;
    int word_counter = 1;
    while (array[i] != '\0') {
        if (array[i] == ' ') {
            word_counter++;
        }
        i++;
    }
    printf("The number of words in the string was %d\n", word_counter);
    return 0;
}
#include <stdio.h>

int main(void) {
    int grid[3][3] = {{3, 4, 5}, {7, 8, 9}, {10, 11, 12}};
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {
            if (grid[row][col] % 2 == 0) {
                printf("Even number %d found a row %d col %d!\n", grid[row][col], row, col);
            }
        }
    }
    return 0;
}
#include <stdio.h>

int main(void) {
    int weeks = 3;
    int days = 2;
    int diet_coke[weeks][days];
    for (int week = 0; week < weeks; week++) {
        for (int day = 0; day < days; day++) {
            printf("Enter Diet Coke cans for Week %d, Day %d: ", week + 1, day + 1);
            scanf("%d", &diet_coke[week][day]);
        }
    }
    int month_total = 0;
    for (int row = 0; row < weeks; row++) {
        int week_total = 0;
        for (int col = 0; col < days; col++) {
            if (diet_coke[row][col] > 3) {
                printf("That's a problem mate on day %d week %d\n", col + 1, row + 1);
            }
            week_total += diet_coke[row][col];
        }
        month_total += week_total;
        if (week_total > 30) {
            printf("That's a lot of diet coke in week %d mate...maybe chill\n", row + 1);
        }
    }
    if (month_total > 70) {
        printf("Your students are worried about you...please stop...(this is about diet coke)\n");
    }
    return 0;
}
#include <stdio.h>

#define MAX_SIZE 4

int main(void) {
    char array[MAX_SIZE];
    fgets(array, MAX_SIZE, stdin);
    printf("The string inputted was %s", array);
    return 0;
}
#include <stdio.h>

#define MAX_SIZE 64

int main(void) {
    char array[MAX_SIZE];
    while (fgets(array, MAX_SIZE, stdin) != NULL) {
        printf("%s", array);
    }
    return 0;
}
#include <stdio.h>

#define MAX_SIZE 64

int main(void) {
    char array[MAX_SIZE];
    fgets(array, MAX_SIZE, stdin);
    fputs(array, stdout);
    return 0;
}
#include <stdio.h>

int main(void) {
    int square_matrix[5][5] =  {{1, 0, 0, 0, 0}, 
                                {2, 5, 0, 0, 0}, 
                                {7, 8, 9, 0, 0},
                                {1, 2, 3, 4, 0},
                                {1, 2, 3, 4, 5}};
    int is_lower_triangle = 1;
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (j <= i) {
                if (square_matrix[i][j] == 0) {
                    is_lower_triangle = 0;
                }
            }
            else {
                if (square_matrix[i][j] != 0) {
                    is_lower_triangle = 0;
                }
            }
        }
    }
    if (is_lower_triangle == 1) {
        printf("Our square matrix is a lower triangle matrix!\n");
    }
    else {
        printf("Our square matrix is NOT a lower triangle matrix!\n");
    }
    return 0;
}
#include <string.h>
#include <stdio.h>

#define MAX_SIZE 10

int main(void) {
    char array[MAX_SIZE];
    fgets(array, MAX_SIZE, stdin);
    printf("The string entered was %s", array);
    int string_length = strlen(array);
    printf("%d\n", string_length);
    return 0;
}
#include <stdio.h>

#define MAX_SIZE 64

int main(void) {
    char array[MAX_SIZE];
    printf("Enter your string: ");
    fgets(array, MAX_SIZE, stdin);
    int i = 0;
    int counter = 0;
    while (array[i] != '\0') {
        if (array[i] != '\n') {
            counter++;
        }
        i++;
    }
    printf("Your string was %d characters long!\n", counter);
}
#include <stdio.h>

int main(void) {
    char board[3][3] = {{'-', '-', '-'},
                       {'-', '-', '-'},
                       {'-', '-', '-'}};
    int row_placement;
    int col_placement;
    int counter = 0;
    char who_turn = 'x';
    while (counter < 3) {
        printf("Enter the row to place your mark: ");
        scanf("%d", &row_placement);
        printf("Enter the col to place your mark: ");
        scanf("%d", &col_placement);
        if (board[row_placement][col_placement] != '-') {
            printf("Don't cheat. Place somewhere else! Go again\n");
        }
        else if (who_turn == 'x') {
            board[row_placement][col_placement] = 'x';
            who_turn = 'o';
        }
        else {
            board[row_placement][col_placement] = 'o';
            who_turn = 'x';
        }
        counter++;
    }
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%c ", board[i][j]);
        }
        printf("\n");
    }
    return 0;
}
#include <stdio.h>
#include <string.h>

#define MAX_SIZE 64

int main(void) {
    char show[MAX_SIZE] = "Survivor";
    char season[] = "Gabon";
    strcat(show, season);
    printf("%s\n", show);
    return 0;
}
#include <stdio.h>
#include <string.h>

int main(void) {
    char array[10];
    //Copy Survivor into our array
    strcpy(array, "Survivor");
    printf("%s\n", array);

    //Checks length of our string
    int length = strlen(array);
    printf("%d\n", length);

    //Compares two strings
    int compare_string = strcmp("Henry", "Epic");
    printf("%d\n", compare_string);

    int new_compare_string = strcmp("Survivor", array);
    printf("%d\n", new_compare_string);

    return 0;
}
#include <stdio.h>
#include <string.h>

int main(void) {
    char show[] = "Tom";
    char show_3[] = "Tommy";
    //char show_2[] = {'S', 'u', 'r', 'v', 'i', 'v', 'o', 'r', '\0'};
    int comparison = strcmp(show, show_3);
    printf("The result of the comparison was %d\n", comparison);
    return 0;
}