Programming Fundamentals

Check in week

Reminder: This week is a check-in week, so please make sure to stay around in the lab time for your one-on-one with one of your Tutors.

Tutorial Overview:

Part 1: 2D Arrays Practice (20 mins)

In this section we will write a short program that deals with 2D arrays.

The objective of this program is to simulate the life of a farmer by placing farm structures such as coops, barns, and trees within a grid.

Functionality

  1. Firstly, the program should prompt for farm structures with Enter barns and trees: and take input for barns and trees until q is pressed.

    • Barns are added with b [row] [col] [points]
    • Trees are added with t [row] [col]

You may assume you will always be given valid input.

  1. After populating the farm with various farm structures your program will need to scan in the farmers's starting position. This will be given as a pair of integers which denotes the row and column (in that order). If the starting position is already occupied by a farm structure, the program should print out Invalid starting position!. Then the program should prompt with Re-enter starting position: and re-scan the position of the farner, repeating until a valid position is scanned in.

  2. After spawning the farmer, the program should prompt for coops with Enter the position and points of the coop(s): and take input in the form [row] [col] [points] until ctrl + D is pressed. Again, you may assume that the provided input is always valid.

  3. Finally, after spawning all farm structures, the program should print the farm with the print_farm function provided.

This program is the starting point we'll be using today:

// Farmer's Farm
// part1_farm.c
//
// This program was written by YOUR-NAME-HERE (zXXXXXXX)
// on INSERT-DATE-HERE
//
// This program is a simple game that allows the user to build a farm.
// The user can place barns, trees, and coops in the farm.

#include <stdio.h>

#define SIZE 5
#define TREE_POINTS -10

enum entity_type {
    COOP,
    BARN,
    TREE,
    FARMER,
    EMPTY,
};

struct farm_entity {
    enum entity_type entity;
    int points;
};

void print_farm(struct farm_entity farm[SIZE][SIZE]);

int main(void) {
    struct farm_entity farm[SIZE][SIZE];

    // TODO: Initialise the farm

    // TODO: Place the barns and trees in the farm
    printf("Enter barns and trees:\n");

    // TODO: Place the farmer in the farm
    printf("Enter the starting position of the farmer: ");

    // TODO: Place the coops in the farm
    printf("Enter the position and points of the coop(s):\n");

    // TODO: Print the farm

    return 0;
}

// Function prints the map of the farm
//
// Parameters:
// - farm: the 2D array representing the farm
//
// returns: nothing
void print_farm(struct farm_entity farm[SIZE][SIZE]) {
    printf("\n---------------------\n");
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            printf("|");
            if (farm[i][j].entity == FARMER) {
                printf(" X ");
            } else if (farm[i][j].entity == EMPTY) {
                printf("   ");
            } else if (farm[i][j].entity == COOP) {
                printf(" o ");
            } else if (farm[i][j].entity == BARN) {
                printf(" # ");
            } else if (farm[i][j].entity == TREE) {
                printf(" T ");
            }
        }
        printf("|\n");
        printf("---------------------\n");
    }
}

Part 2: Arrays and Functions (15 min max)

In this section, we'll practice writing and using functions, focusing in particular on how they interact with arrays.

To do this we will be working with the program we created in part1 and in groups refactoring the program to use functions.

Part 3: Strings (20 min max)

Warmup:

Your tutor may briefly run through the following code and diagram with you:

Diagram of strings in c
// part3_simple_string.c
//
// This program was written by Sofia De Bellis (z5418801)
// on March 2024
//
// This program demonstrates how to work with strings in C.

#include <stdio.h>

#define MAX_SIZE 1024

int main(void) {
    // Declare and initialise a string
    char my_string[] = "Hello world!";
    
    // Traverse the string and print each character
    int i = 0;
    while (my_string[i] != '\0') {
        printf("%c", my_string[i]);
        i++;
    }
    printf("\n");

    // Another way to traverse the string and print each character 
    for (int i = 0; my_string[i] != '\0'; i++) {
        printf("%c", my_string[i]);
    }

    // How to print a string in its entirety
    printf("My string: %s\n", my_string);

    // Declare a string
    char another_string[MAX_SIZE];
    
    // Read a string from the user, note we DO NOT use scanf for strings
    printf("Enter a string: ");
    fgets(another_string, MAX_SIZE, stdin);
    
    // Print the string using fputs
    fputs(another_string, stdout);

    return 0;
}

Practice

In this section you'll be completing some string activities in your groups.

Have a quick look at the following char functions that have been implemented for you:

// Provided char functions

// Returns : 1 if `c` is a lowercase letter
//         : 0 otherwise.
int is_lowercase(char c) {

    return 'a' <= c && c <= 'z';
}

// Returns : 1 if `c` is an uppercase letter
//         : 0 otherwise.
int is_uppercase(char c) {

    return 'A' <= c && c <= 'Z';
}

// Returns : 1 if `c` is a letter
//         : 0 otherwise.
int is_letter(char c) {

    return is_lowercase(c) || is_uppercase(c);
}

// Returns : `c` converted to lowercase, if it was an uppercase letter
//         : `c` unmodified, otherwise
char to_lowercase(char c) {
    if (is_uppercase(c)) {
        return c - 'A' + 'a';
    }

    return c;
}

// Returns : `c` converted to uppercase, if it was a lowercase letter
//         : `c` unmodified, otherwise
char to_uppercase(char c) {
    if (is_lowercase(c)) {
        return c - 'a' + 'A';
    }

    return c;
}

// Returns : 1 if `c` is an uppercase or lowercase vowel
//         : 0 otherwise.
int is_vowel(char c) {
    char lower_c = to_lowercase(c);

    return lower_c == 'a' 
        || lower_c == 'e'
        || lower_c == 'i' 
        || lower_c == 'o' 
        || lower_c == 'u';
}

You may want to use some of these in your solutions.

You will be completing one of these functions!

// Functions to implement:

// 1.
// returns the number of lowercase letters in `char *string`
int count_lowercase(char string[MAX_CHARS]) {
    return 42;
}

// 2.
// modifies `char *string` by converting all its vowels to uppercase
void make_vowels_uppercase(char string[MAX_CHARS]) {
    return;
}

// 3..
// shortens a string so that it ends after the first word
// e.g. "This is a sentence" should turn into:
//      "This"
// 
// (hint. what defines when a string ends?)
void delete_following_words(char string[MAX_CHARS]) {
    return;
}