Programming Fundamentals

Objectives

  • creating functions
  • manipulating 2D arrays
  • scanning until ctrl-d
  • basic string manipulation

Feedback Week!

In this week's lab, your tutors will go around the class and give you one on one feedback on some code you have written in a previous week.

Take this as an opportunity to ask any questions you might have about the course content so far!

So, if you would like, have a think about if there is any particular exercise you would like to receive feedback for, or any particular content you would like to ask about.

Reminder: Help sessions

Help sessions are running this week!

These are one of the best ways for you to get one on one help with a tutor for any course content (including Lab Exercises and Assignments).

For the dates and times of the help sessions, see the Help Session Timetable.

To join a help session, or for more information, see the COMP(1511|1911) Help Session Microsoft Teams.

For face-to-face help sessions, the lab map can be found here. If help sessions are running in other buildings you can use Lost on Campus to help you find them.

Activities To Be Completed

The following is a list of all the activities available to complete this week...

Worth 1 mark(s) in total:

  • debug_stringcmp
  • word_square
  • alternating_case
  • simple_snake

Worth 1 mark(s) in total:

  • planter
  • palindrome

Worth 0.5 mark(s) in total:

  • remove_duplicates_function
  • largest_z_sum

For your interest, but not for marks:

  • p1511

Problem sets are capped at 15 marks (there are 4 possible bonus marks from the three-dot exercises that can bring you up to a total of 15 if you missed out on any other marks in the one- or two-dot exercises).

Completing just the one and two-dot exercises every week can give you the full 15 marks needed in this component.

For more details, see the course outline.

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples.

When attempting the following exercises, make sure to read the whole exercise, including any hints and assumptions that may make the exercise easier.

Exercise
(●◌◌)
:

Debugging - stringcmp

Download debug_stringcmp.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity debug_stringcmp

Debugging Tips!

Some debugging tips for you:

  • dcc output - as you run into issues, dcc will point out where the errors are. Remember that dcc gives you the line number the issue is on, and will give some sort of explanation. Make sure you read everything dcc gives you. Sometimes we get “errors carried forward”, so find your first error, fix that, then recompile.
  • print statements - sometimes it can be handy to see if the flow of your code puts you in the spot you expect it to be (ie. inside the right if statement, or going through a loop the correct amount of times). A quick way you can check this is by putting print statements in your code for testing purposes, like "the value of x is %d and y is %d". This lets you check what you got against what you expected.
  • COMP1511 debugging guide

The Task

This exercise takes in two strings as input, calcuates if they are equal and prints out the result. Currently it has some issues - it is your job to figure them out and fix the code.

Examples

dcc debug_stringcmp.c -o debug_stringcmp
./debug_stringcmp
Enter the first string: Hello, World!
Enter the second string: Hello, World!
The strings are equal!
./debug_stringcmp
Enter the first string: It's illegal to own just one guinea pig in Switzerland
Enter the second string: Pigeons can tell the difference between Picasso and Monet
The strings are not equal!
You can run an automated code style checker using the following command:
1511 style debug_stringcmp.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest debug_stringcmp

When you are finished working on this exercise, you must submit your work by running give:

give cs1511 lab05_debug_stringcmp debug_stringcmp.c

You must run give before Monday 21 October 20:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Sample solution for debug_stringcmp.c
// debug_stringcmp.c
//
// This program takes in two strings as input and checks if they are the same
// Wrriten by Sofia De Bellis, z5418801
// on June, 2023

#include <stdio.h>
#include <string.h>

#define MAX_SIZE 100

int main() {
    char str1[MAX_SIZE];
    char str2[MAX_SIZE];

    // Read input from user
    printf("Enter the first string: ");
    fgets(str1, MAX_SIZE, stdin);

    printf("Enter the second string: ");
    fgets(str2, MAX_SIZE, stdin);

    // Check if the strings are equal
    if (strcmp(str1, str2) == 0) {
        printf("The strings are equal!\n");
    } else {
        printf("The strings are not equal!\n");
    }

    return 0;
}

Exercise
(●◌◌)
:

Word Square

Write a program called word_square.c that prompts the user to enter a word, and afterwards, prints that word out n amount of times, where n is the length of the word.

You should use the function fgets to scan in the input word.

Examples

dcc word_square.c -o word_square
./word_square
Input word: a

Word square is:
a
./word_square
Input word: word

Word square is:
word
word
word
word
./word_square
Input word: abrakadabra

Word square is:
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra
abrakadabra

Assumptions/Restrictions/Clarifications

  • You can assume that you will be given a word no longer than 1024 character (Including the null-terminator).
You can run an automated code style checker using the following command:
1511 style word_square.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest word_square

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab05_word_square word_square.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 21 October 20:00 to obtain the marks for this lab exercise.

Sample solution for word_square.c
// Program to take in a word and print it out by its length amount of times.
// Written by Rory Golledge (z5308772) on October 2021

#include <stdio.h>
#include <string.h>

#define MAX_WORD 1024

int main(void) {
    char word[MAX_WORD];

    printf("Input word: ");
    fgets(word, MAX_WORD, stdin);

    printf("\nWord square is:\n");

    int i = 0;
    while (i < strlen(word) - 1) {
        printf("%s", word);
        i++;
    }

    return 0;
}

Exercise
(●◌◌)
:

Alternating case

Download alternating_case.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity alternating_case

Your task is to add code to these functions in alternating_case.c:

// Modifies `string` so that the first letter is converted to lowercase, 
// and the case of each following letter alternates.
// i.e. in the resulting string: 
//      the first letter is lowercase, 
//      second letter is uppercase, 
//      third letter is lower case, 
//      etc.
//
// e.g.: "Hello" -> "hElLo"
void make_alternating(char string[MAX_STRING_LENGTH]) {
    // TODO: complete this function
}

Complete the C program, alternating_case.c.

The main function has already been written for you. You must not modify it in any way.

This function takes in a string, and should modify it so that the first letter is lowercase, and the case of each following letter alternates.

In other words, the string should be modified so that:

  • The first letter is lowercase,
  • The second letter is uppercase,
  • The third letter is lowercase,
  • The forth letter is uppercase,
  • etc.

All non letter characters should be left unmodified.

Examples

./alternating_case
hello
hElLo
./alternating_case
Hello World
hElLo WoRlD
./alternating_case
The quick brown fox jumps over the lazy dog
tHe QuIcK bRoWn FoX jUmPs OvEr ThE lAzY dOg
./alternating_case
a1b2c3e4d5 f6g7
a1B2c3E4d5 F6g7

Assumptions/Restrictions/Clarifications

  • You can assume the string will contain at most 1024 characters
You can run an automated code style checker using the following command:
1511 style alternating_case.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest alternating_case

When you are finished working on this exercise, you must submit your work by running give:

give cs1511 lab05_alternating_case alternating_case.c

You must run give before Monday 21 October 20:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Sample solution for alternating_case.c
// Written by ...
// June, 2022

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX_STRING_LENGTH 1024

//////////////// DO NOT CHANGE ANY OF THE CODE BELOW HERE //////////////////
void make_alternating(char string[MAX_STRING_LENGTH]);

int main (void) {
    //You don't need to understand this code to complete the exercise.

    //Scans in a string from the user.
    char buffer[MAX_STRING_LENGTH];
    fgets(buffer, MAX_STRING_LENGTH, stdin);

    // Runs your function
    make_alternating(buffer);

    // Prints resulting string.
    printf("%s", buffer);

    return 0;
}
//////////////// DO NOT CHANGE ANY OF THE CODE ABOVE HERE //////////////////

////////////////////////////////////////////////////////////////////////////
///////////////////// ONLY WRITE CODE BELOW HERE ///////////////////////////
////////////////////////////////////////////////////////////////////////////

// Modifies `string` so that the first letter is converted to lowercase, 
// and the case of each following letter alternates.
// i.e. in the resulting string: 
//      the first letter is lowercase, 
//      second letter is uppercase, 
//      third letter is lower case, 
//      etc.
//
// e.g.: "Hello" -> "hElLo"
void make_alternating(char string[MAX_STRING_LENGTH]) {

    int i = 0;
    int letter_counter = 0;

    while (string[i] != '\0') {
        if (isalpha(string[i])) {
            if (letter_counter % 2 == 0) {
                string[i] = tolower(string[i]);
            } else {
                string[i] = toupper(string[i]);
            }
            letter_counter++;
        }
        i++;
    }
}

////////////////////////////////////////////////////////////////////////////
///////////////////// ONLY WRITE CODE ABOVE HERE ///////////////////////////
////////////////////////////////////////////////////////////////////////////

Exercise
(●◌◌)
:

Creating a simple Snake game

Download simple_snake.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity simple_snake

Snake is one of the oldest and most famous digital games.

The Snake game involves controlling a snake that moves along a 2D plane. The goal is to lead this snake to an apple on the map, that upon eating, increases the length of the snake.

As more apples are eaten, the player must be careful as they move the snake, as the game will end if the snake moves back into their tail.

For this exercise, you will be implementing a simpler version of Snake. This version will not implement the feature which ends the game when moving back onto the tail. It will involve:

  1. Spawning the snake and the apple
  2. Moving the snake up/down/left/right in a loop (using u/d/l/r commands)
  3. Ending the game when the snake reaches the apple

Since this game is fundamentally about moving a snake on a 2D plane, it should make sense to represent this as a 2D array in code. In the starter code provided, you should notice that the first line of code in the main() function defines this array as:

enum land map[SIZE][SIZE];

This means that the map is made up only of values defined in the enum land definition above the main() function.

Let's have a look at what each value in enum land represents:

  • NOT_VISITED - Indicates a land tile that the snake has not visited yet
  • VISITED - Indicates a land tile that the snake has visited
  • SNAKE - The land tile the snake is currently on
  • APPLE - The land tile the apple is currently on

You have been provided a print_map() function that will print the 2D array with the enum land tiles set.

Make sure you understand everything mentioned above before reading the example below!

Examples

dcc simple_snake.c -o simple_snake
./simple_snake
Welcome to Snake!
Please enter apple location: 2 3
Please enter snake location: 4 3
. . . . . . . . 
. . . . . . . . 
. . . A . . . . 
. . . . . . . . 
. . . S . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
r
. . . . . . . . 
. . . . . . . . 
. . . A . . . . 
. . . . . . . . 
. . . - S . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
u
. . . . . . . . 
. . . . . . . . 
. . . A . . . . 
. . . . S . . . 
. . . - - . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
u
. . . . . . . . 
. . . . . . . . 
. . . A S . . . 
. . . . - . . . 
. . . - - . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
u
. . . . . . . . 
. . . . S . . . 
. . . A - . . . 
. . . . - . . . 
. . . - - . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
l
. . . . . . . . 
. . . S - . . . 
. . . A - . . . 
. . . . - . . . 
. . . - - . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
d
. . . . . . . . 
. . . - - . . . 
. . . S - . . . 
. . . . - . . . 
. . . - - . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
Chomp!

Assumptions/Restrictions/Clarifications

  • You will always be given snake/apple locations inside the map
  • You will always be given correct direction commands (u/d/l/r)
  • You will never be given commands such that the snake walks off the map
You can run an automated code style checker using the following command:
1511 style simple_snake.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest simple_snake

When you are finished working on this exercise, you must submit your work by running give:

give cs1511 lab05_simple_snake simple_snake.c

You must run give before Monday 21 October 20:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Sample solution for simple_snake.c
// A simpler version of the famous Snake game!
//
// Written by Rory Golledge (z5308772) on 3/3/23


#include <stdio.h>

#define SIZE 8

enum land {
    NOT_VISITED,
    VISITED,
    SNAKE,
    APPLE
};

void initialise_map(enum land map[SIZE][SIZE]);
void print_map(enum land map[SIZE][SIZE]);

int main(void) {
    enum land map[SIZE][SIZE];
    initialise_map(map);
 
    printf("Welcome to Snake!\n");
    printf("Please enter apple location: ");
    int apple_row;
    int apple_col;
    scanf("%d %d", &apple_row, &apple_col);

    map[apple_row][apple_col] = APPLE;

    printf("Please enter snake location: ");
    int snake_row;
    int snake_col;
    scanf("%d %d", &snake_row, &snake_col);

    map[snake_row][snake_col] = SNAKE;

    print_map(map);

    while (!(snake_row == apple_row && snake_col == apple_col)) {
        char dir;
        scanf(" %c", &dir);

        map[snake_row][snake_col] = VISITED;    
        if (dir == 'u') {
            --snake_row;
        } else if (dir == 'd') {
            ++snake_row;
        } else if (dir == 'l') {
            --snake_col;
        } else {
            ++snake_col;
        }
        map[snake_row][snake_col] = SNAKE;

        print_map(map);
    }

    printf("Chomp!\n");

    return 0;
}

/**
 * Initialises the given `map` such that all tiles are `NOT_VISITED`
 * 
 * Parameters:
 *     map - The map to initialise
 * 
 * Returns:
 *     Nothing
 */
void initialise_map(enum land map[SIZE][SIZE]) {
    for (int row = 0; row < SIZE; ++row) {
        for (int col = 0; col < SIZE; ++col) {
            map[row][col] = NOT_VISITED;
        }
    }
}

/**
 * Prints the given `map` such that all enum values are printed as nice
 * characters.
 * 
 * Parameters:
 *     map - The map to print out
 * 
 * Returns:
 *     Nothing
 */
void print_map(enum land map[SIZE][SIZE]) {
    for (int row = 0; row < SIZE; ++row) {
        for (int col = 0; col < SIZE; ++col) {
            if (map[row][col] == NOT_VISITED) {
                printf(". ");
            } else if (map[row][col] == VISITED) {
                printf("- ");
            } else if (map[row][col] == SNAKE) {
                printf("S ");
            } else if (map[row][col] == APPLE) {
                printf("A ");
            }
        }
        printf("\n");
    }
}

Exercise
(●●◌)
:

Planter

Download planter.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity planter

Complete the program planter.c! The program is a garden simulator that uses a 2D array, a 5x5 grid, to represent a garden.

The gardener will be represented by the # character and each round will appear on the first column of the row that is about to be planted.

The program should prompt the user to choose which type of flower that will be planted in each row of the garden. The user may also choose to plant nothing, represented by the . character.

Flowers take 3 rounds to bloom after they are planted. During the round, if the flower is still growing, it will be represented by a number that represents how many rounds are left for the flower to bloom. If the flower has already bloomed, it will be represented by the first letter of its name.

The garden should be printed after each round, updating how long is left for the flowers to bloom.

Once all flowers are planted, the program should wait for the growing flowers to bloom, printing Waiting for flowers to bloom... and the garden after each round.

You must use the provided struct garden to represent the flowers in the garden, the print_garden function to display the current state of the garden and the provided enum flowers to represent the different flowers.

Examples

dcc planter.c -o planter
./planter
Welcome to the planter!

# . . . . 
. . . . . 
. . . . . 
. . . . . 
. . . . . 

0. Nothing
1. Daisy
2. Rose
3. Tulip
Which Flower would you like to plant in this row? 3

3 3 3 3 3 
# . . . . 
. . . . . 
. . . . . 
. . . . . 

0. Nothing
1. Daisy
2. Rose
3. Tulip
Which Flower would you like to plant in this row? 0

2 2 2 2 2 
. . . . . 
# . . . . 
. . . . . 
. . . . . 

0. Nothing
1. Daisy
2. Rose
3. Tulip
Which Flower would you like to plant in this row? 1

1 1 1 1 1 
. . . . . 
3 3 3 3 3 
# . . . . 
. . . . . 

0. Nothing
1. Daisy
2. Rose
3. Tulip
Which Flower would you like to plant in this row? 2

T T T T T 
. . . . . 
2 2 2 2 2 
3 3 3 3 3 
# . . . . 

0. Nothing
1. Daisy
2. Rose
3. Tulip
Which Flower would you like to plant in this row? 0

T T T T T 
. . . . . 
1 1 1 1 1 
2 2 2 2 2 
. . . . . 

Waiting for flowers to bloom...

T T T T T 
. . . . . 
D D D D D 
1 1 1 1 1 
. . . . . 

Waiting for flowers to bloom...

T T T T T 
. . . . . 
D D D D D 
R R R R R 
. . . . . 

Assumptions/Restrictions/Clarifications

  • Assume you will only be given valid inputs.
  • The provided struct garden, enum flowers and print_garden should not be modified.
  • You may not assume a flower will always be chosen to be planted in a row, the user may choose to plant nothing.
You can run an automated code style checker using the following command:
1511 style planter.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest planter

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab05_planter planter.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 21 October 20:00 to obtain the marks for this lab exercise.

Sample solution for planter.c
// planter.c 
// A program which lets you live out your dream of being a gardener.
// Written by Ibrahim Ghoneim 

/*

*/
#include <stdio.h>

#define GARDEN_ROWS 5
#define GARDEN_COLS 5
#define TRUE 1
#define FALSE 0

/*
Provided enum
*/
enum flowers {
    EMPTY,
    DAISY,
    ROSE,
    TULIP
};

/*
Provided struct
*/
struct garden {
    enum flowers flower;
    int bloom_countdown;
    int is_gardener;
};

/*
provided function prototypes
*/
void print_garden(struct garden garden[GARDEN_ROWS][GARDEN_COLS]);
int grow_garden(struct garden garden[GARDEN_ROWS][GARDEN_COLS]);

int main(void) {
    printf("Welcome to the planter!\n");
    struct garden garden[GARDEN_ROWS][GARDEN_COLS];

    // Double for loop to initialize the garden
    for (int i = 0; i < GARDEN_ROWS; i++) {
        for (int j = 0; j < GARDEN_COLS; j++) {
            garden[i][j].flower = EMPTY;
            garden[i][j].bloom_countdown = 0;
            garden[i][j].is_gardener = FALSE;
        }
    }
    int choice;

    // Loop to plant flowers in the garden going row by row and planting the
    // same flower in each column
    for (int i = 0; i < GARDEN_ROWS; i++) {
        garden[i][0].is_gardener = TRUE;
        print_garden(garden);
        grow_garden(garden);
        printf(
            "0. Nothing\n"
            "1. Daisy\n"
            "2. Rose\n"
            "3. Tulip\n"
            "Which Flower would you like to plant in this row? "
        );
        scanf("%d", &choice);
        if (choice != EMPTY) {
            for (int j = 0; j < GARDEN_COLS; j++) {
                garden[i][j].flower = choice;
                garden[i][j].bloom_countdown = 3;
            }
        }
        garden[i][0].is_gardener = FALSE;
    }

    print_garden(garden);
    // Loop to wait for the flowers to bloom, will keep looping until grow_garden
    // returns 0
    while (grow_garden(garden)) {
        printf("Waiting for flowers to bloom...\n");
        print_garden(garden);
    }
    return 0;
}

/*
    Decreases the bloom_countdown for all flowers in the garden
    Parameters:
        garden: a 2D array of struct garden
    Returns:
        int: returns 1 if a flower has grown, 0 otherwise
*/
int grow_garden(struct garden garden[GARDEN_ROWS][GARDEN_COLS]) {
    int flower_found = FALSE;
    for (int i = 0; i < GARDEN_ROWS; i++) {
        for (int j = 0; j < GARDEN_COLS; j++) {
            if (garden[i][j].bloom_countdown > 0) {
                garden[i][j].bloom_countdown--;
                flower_found = TRUE;
            }
        }
    }
    return flower_found;
}


////////////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT MODIFY ANYTHING BELOW THIS LINE ////////////////////
////////////////////////////////////////////////////////////////////////////////

/*
    Prints the garden to the terminal
    Parameters:
        garden: a 2D array of struct garden
    Returns:
        void
*/
void print_garden(struct garden garden[GARDEN_ROWS][GARDEN_COLS]) {
    printf("\n");
    for (int i = 0; i < GARDEN_ROWS; i++) {
        for (int j = 0; j < GARDEN_COLS; j++) {
            if (garden[i][j].is_gardener) {
                printf("# ");
            } else if (garden[i][j].flower == EMPTY) {
                printf(". ");
            } else if (garden[i][j].bloom_countdown > 0) {
                printf("%d ", garden[i][j].bloom_countdown);
            } else if (garden[i][j].flower == DAISY) {
                printf("D ");
            } else if (garden[i][j].flower == ROSE) {
                printf("R ");
            } else if (garden[i][j].flower == TULIP) {
                printf("T ");
            } else {
                printf(". ");
            }
        }
        printf("\n");
    }
    printf("\n");
}

Exercise
(●●◌)
:

Palindrome

Download palindrome.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity palindrome

Your task is to add code to this function in palindrome.c:

// Determines if the given `my_string` is a palindrome or not.
// Returns `1` if it is, otherwise `0`
int is_palindrome(char my_string[MAX_STRING_LENGTH]) {
    // TODO: Write is_palindrome function here
    return 42;
}

Write a program called palindrome.c that takes in a word, and afterwards, prints out whether that word is palindromic.

For example, if we have the string

racecar

it is a palindrome since the string reads the same in reverse order i.e. racecar (the original string) does equal racecar (the original string in reverse). In this case your program would print racecar is a palindrome!

However, if we have the string

hello

is not a palindrome since the string does not read the same in reverse order i.e. hello (the original string) does not equal olleh (the original string in reverse). In this case your program would print hello is not a palindrome!

The main function has already been written, which initalises a string, calls the is_palindrome function and prints the result.

Testing

palindrome.c contains a main function which allows you to test your is_palindrome function.

This main function:

  1. Scans in a string from the user
  2. calls the is_palindrome function and
  3. prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your is_palindrome function will be called directly in marking. The main function is only to let you test your is_palindrome function.

Examples

dcc palindrome.c -o palindrome
./palindrome
racecar
racecar is a palindrome!
./palindrome
pineapple
pineapple is not a palindrome!
./palindrome
tacocat
tacocat is a palindrome!
./palindrome
turtwig
turtwig is not a palindrome!

Assumptions/Restrictions/Clarifications

  • You may assume you will only be given single, alaphabetic words with no whitespace or special characters
You can run an automated code style checker using the following command:
1511 style palindrome.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest palindrome

When you are finished working on this exercise, you must submit your work by running give:

give cs1511 lab05_palindrome palindrome.c

You must run give before Monday 21 October 20:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Sample solution for palindrome.c
// palindrome.c
//
// Given a string, this program calculates whether it is palindromic or not and 
// prints out the result.
//
// Written by Sofia De Bellis, z5418801
// on June, 2023

#include <stdio.h>
#include <string.h>

#define MAX_STRING_LENGTH 1024

int is_palindrome(char my_string[MAX_STRING_LENGTH]);

//////////////// DO NOT CHANGE ANY OF THE CODE BELOW HERE //////////////////
int main (void) {
    // You don't need to understand this code to complete the exercise.

    // Scans in a string from the user.
    char my_string[MAX_STRING_LENGTH];
    fgets(my_string, MAX_STRING_LENGTH, stdin);

    // removing the newline character from the end of the string
    my_string[strlen(my_string) - 1] = '\0';
    
    // Runs your function and prints results
	if (is_palindrome(my_string)) {
        printf("%s is a palindrome!\n", my_string);
	} else {
		printf("%s is not a palindrome!\n", my_string);
	}
    
    return 0;
}
//////////////// DO NOT CHANGE ANY OF THE CODE ABOVE HERE //////////////////

////////////////////////////////////////////////////////////////////////////
///////////////////// ONLY WRITE CODE BELOW HERE ///////////////////////////
////////////////////////////////////////////////////////////////////////////
int is_palindrome(char my_string[MAX_STRING_LENGTH]) {
    int length = strlen(my_string); 
    int i = 0;
	int j = length - 1;

    for (int i = 0; i < (length - 1)/2; i++) {
        if (my_string[i] != my_string[j]) {
            return 0; 
        }
		j--;
    }

    return 1; 
}
////////////////////////////////////////////////////////////////////////////
///////////////////// ONLY WRITE CODE ABOVE HERE ///////////////////////////
////////////////////////////////////////////////////////////////////////////

Exercise
(●●●)
:

Remove any duplicate values from an array and write the result into another array

Write a C function that removes duplicate elements from an array, by copying the non-duplicate values to a second array, i.e. only the first occurrence of any value should be copied.

Place your answer in a file named remove_duplicates_function.c

Your function should take three parameters: the length of source array, the source array itself, and the destination array. It must have this prototype:

int remove_duplicates(int length, int source[length], int destination[length]);

Your function should return a single integer: the number of elements copied to the destination array.

For example, if the source array contains these 6 elements:

3, 1, 4, 1, 5, 9

Your function should copy these 5 values to the destination array:

3, 1, 4, 5, 9

Your function should return the integer 5, because there were 5 values copied -- the second occurrence of the digit 1 was not copied.

Assumptions/Restrictions/Clarifications

  • You can assume the source array only contains positive integers.
  • You can assume the source array contains at least one integer.
  • You can assume that the destination array will always be large enough to fit all of the copied values.
  • You cannot assume anything about the number of duplicates, i.e. there may not be any duplicates, or conversely, the entire array may be duplicates.
  • Your function should return a single integer.
  • Your function should not change the array it is given.
  • Your function should not call scanf (or getchar or fgets).
  • Your function should not print anything. It should not call printf.
  • Your submitted file may contain a main function. It will not be tested or marked.
You can run an automated code style checker using the following command:
1511 style remove_duplicates_function.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest remove_duplicates_function

When you are finished working on this exercise, you must submit your work by running give:

give cs1511 lab05_remove_duplicates_function remove_duplicates_function.c

You must run give before Monday 21 October 20:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Sample solution for remove_duplicates_function.c
int remove_duplicates(int length, int source[length], int destination[length]) {

    // Go through each element in the `source` array
    int destination_upto = 0;
    int source_upto = 0;
    while (source_upto < length) {
        // Check it against every other element we've seen so far.
        int duplicate = 0;
        int checking = 0;
        while (checking < source_upto && duplicate == 0) {
            if (source[source_upto] == source[checking]) {
                duplicate = 1;
            }
            checking = checking + 1;
        }

        if (!duplicate) {
            destination[destination_upto] = source[source_upto];
            destination_upto = destination_upto + 1;
        }

        source_upto = source_upto + 1;
    }
    return destination_upto;
}

Exercise
(●●●)
:

Find the Largest Sum of Numbers in a Z Shape

Download largest_z_sum.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity largest_z_sum

Your task is to add code to this function in largest_z_sum.c:

// Return the largest sum of numbers in a z shape.
int largest_z_sum(int size, int array[MAX_SIZE][MAX_SIZE]) {
    // Put your code here.
    return 42;
}

You are to implement the largest_z_sum function which should return the sum of values forming the shape of the letter 'Z' in a square 2D array.

A Z shape is made up of three lines of equal length. Two of these lines are horizontal and one is diagonal. The length of the three lines must be equal but can range from 3 up to the size of the array. Only correctly oriented Z shapes are valid - Z shapes with a northwest/southeast diagonal are not valid.

The 2D square array may contain any positive or negative integers.

You can assume that the side length of the 2D square array will always be greater than or equal to 3.

You can assume that the side length of the 2D array will never be greater than 100.

The file largest_z_sum.c contains a main function which reads values into a square 2D array and calls largest_z_sum.

Examples

dcc largest_z_sum.c -o largest_z_sum
./largest_z_sum
Enter 2D array side length: 3
Enter 2D array values:
1 1 1
1 1 1
1 1 1
The largest z sum is 7.
./largest_z_sum
Enter 2D array side length: 5
Enter 2D array values:
1  2  3  4  5
6  7  8  9  10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
The largest z sum is 169.
./largest_z_sum
Enter 2D array side length: 5
Enter 2D array values:
 28 -47 -40  29  49
 26 -42 -37  48  1
-36  50  41 -24 -33
 41  25 -39  39  48
 14 -26 -46 -3  -29
The largest z sum is 153.
./largest_z_sum
Enter 2D array side length: 5
Enter 2D array values:
1  1  1  1  1
1  1  1  1  1
99 99 99 1  1
1  99 1  1  1
99 99 99 1  1
The largest z sum is 693.

In the first example, there is only one possible Z sum of size 3.

The Z in the example input is underlined below for your reference:

1 1 1
1 1 1
1 1 1

In the second example, the Z of size 5 starting from (0, 0) is used to form the largest sum of:

1 + 2 + 3 + 4 + 5 + 9 + 13 + 17 + 21 + 22 + 23 + 24 + 25 = 169

The Z in the example input is underlined below for your reference:

1  2  3  4  5
6  7  8  9  10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

In the third example, the Z of size 4 starting from (0, 1) is used to form the largest sum of:

-47 - 40 + 29 + 49 + 48 + 41 + 25 - 39 + 39 + 48 = 153

The Z in the example input is underlined below for your reference:

 28 -47 -40  29  49
 26 -42 -37  48  1
-36  50  41 -24 -33
 41  25 -39  39  48
 14 -26 -46 -3  -29

In the fourth example, the Z of size 3 starting from (2, 0) is used to form the largest sum of:
99 + 99 + 99 + 99 + 99 + 99 + 99 = 693
The Z in the example input is underlined below for your reference:

1  1  1  1  1
1  1  1  1  1
99 99 99 1  1
1  99 1  1  1
99 99 99 1  1
You can run an automated code style checker using the following command:
1511 style largest_z_sum.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest largest_z_sum

When you are finished working on this exercise, you must submit your work by running give:

give cs1511 lab05_largest_z_sum largest_z_sum.c

You must run give before Monday 21 October 20:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Sample solution for largest_z_sum.c
// Find the largest sum of numbers in a z shape.

#include <stdio.h>
#include <assert.h>

#define MAX_SIZE 100

int largest_z_sum(int size, int array[MAX_SIZE][MAX_SIZE]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(void) {
    int array[MAX_SIZE][MAX_SIZE];

    // Get the array size.
    int size;
    printf("Enter 2D array side length: ");
    scanf("%d", &size);
    assert(size >= 3);

    // Scan in values for the array.
    printf("Enter 2D array values:\n");
    int i = 0;
    while (i < size) {
        int j = 0;
        while (j < size) {
            assert(scanf("%d", &array[i][j]) == 1);
            j++;
        }
        i++;
    }

    printf("The largest z sum is %d.\n", largest_z_sum(size, array));

    return 0;
}

// Return the largest sum of numbers in a z shape.
int largest_z_sum(int size, int array[MAX_SIZE][MAX_SIZE]) {
    int curr_size = 3;

    // Appropriate initial value for max_sum. Most basic z.
    int max_sum = array[0][0] + array[0][1] + array[0][2]
                              + array[1][1]
                + array[2][0] + array[2][1] + array[2][2];

    while (curr_size <= size) {
        int z_possible_y = 0;
        while (z_possible_y <= size - curr_size) {
            int z_possible_x = 0;
            while (z_possible_x <= size - curr_size) {

                // Consider (z_possible_y, z_possible_x) to be top left corner
                // of candidate z shape of curr_size sidelength.
                
                // Calculate sum of the top row of the z.
                int top_row_sum = 0;
                int i = 0;
                while (i < curr_size - 1) { // -1 to prevent intersection point being double counted.
                    top_row_sum += array[z_possible_y][z_possible_x + i];
                    i++;
                }

                // Calculate the sum of the diagonal.
                int diagonal_sum = 0;
                i = 0;
                while (i < curr_size) {
                    diagonal_sum += array[z_possible_y + i][z_possible_x + (curr_size - 1) - i];
                    i++;
                }


                // Calculate sum of the bottom row.
                int bottom_row_sum = 0;
                i = 1; // 1 initial value to prevent intersection point being double counted.
                while (i < curr_size) {
                    bottom_row_sum += array[z_possible_y + (curr_size - 1)][z_possible_x + i];
                    i++;
                }

                int total_sum = top_row_sum + diagonal_sum + bottom_row_sum;
                if (total_sum > max_sum) {
                    max_sum = total_sum;
                }

                z_possible_x++;
            }

            z_possible_y++;
        }

        curr_size++;
    }
    return max_sum;
}

Exercise
(☠)
:

Implement p1511, a new programming language.

So far in this course, you have been learning the C language.

While C is a very famous and popular language, thousands (perhaps tens of thousands) of programming languages exist. Some are very popular, though others are designed as a joke or a game. Joke languages like this are often called "eso-langs" or "esoteric languages".

Some more famous examples of eso-langs include:

In this challenge, we ask you to implement a dialect of P'' called P1511.

There are only three concepts you need to know:

  • Commands: A P1511 program is an array of characters. Each character represents a command. After running a command, you run the next one (unless the command says otherwise)
  • Your Memory: This is an array of integers, which is 0 initialized. The array can be as big as you like (we recommend 20 for debugging purposes). Each value in the array is called a "cell"
  • The Cell-Pointer One cell is always "selected". Many commands in P1511 act on the selected cell. To keep track of which cell is selected, we have the "cell pointer"

You should assume the list of commands is no longer than 10000.

An instruction can be one of the following commands:

  • >
    • Move the cell-pointer right. If you are at the last cell, loop back around to the first one
  • <
    • Move the cell-pointer left. If you are at the first cell, loop back around to the last one
  • +
    • Add one to the current value of the cell-pointer
  • -
    • Subtract one from the current value of the cell-pointer
  • .
    • Print out the value of the cell-pointer as an ascii character
  • ,
    • Replace the cell-pointer's value with a scanned in number (i.e. scanf a number, and set the cell-pointer to be equal to it)
  • any lowercase letter
    • If the value of the cell-pointer is 0, jump through your commands to the uppercase version of this letter
  • any uppercase letter
    • If the value of the cell-pointer is not 0, jump through your commands to the lowercase version of this letter
  • !
    • End the program :)

With this simple language, you can write almost any program!

To complete this challenge, you should write a program which simulates P1511.

Examples

Here is a simple P1511 program:

char commands[10000] = {
'>', '+', '+', '+', '+', '+', '+', '+', '+',
  'a', '<', '+', '+', '+', '+', '+', '+', '+', '+', '+', '>', '-', 'A',
'<', '.', '+', '.', '!'
};

This program prints out "HI". To see the logic of this program, run 1511 p1511, which will walk you through, step by step.

Write your own P1511 programs

If you manage to write a program in P1511, you can share it by making a post on the course forum!.

Try writing a program to print out a word, or (challenge) to add two numbers together!

You can run an automated code style checker using the following command:
1511 style p1511.c
Sample solution for p1511.c
#include <stdio.h>
#include <string.h>

#define MEM_SIZE 20

void print_memory(int commands[MEM_SIZE], int cmd_index) {
    int i = 0;
    int printed_chars = 0;
    int last_printed_chars = 0;
    while (i < MEM_SIZE) {
        char to_print[100];
        snprintf(to_print, 100, "%02d ", commands[i]);
        printf("%s", to_print);
        if (i <= cmd_index) {
            printed_chars += last_printed_chars;
            last_printed_chars = strlen(to_print);
        }
        i++;
    }
    printf("\n");

    i = 0;
    while (i < printed_chars) {
        printf(" ");
        i++;
    }

    i = 0;
    while (i < last_printed_chars - 1) {
        printf("^");
        i++;
    }
    printf("\n");
}

int main(void) {
    char commands[10000] = {
        '>', '+', '+', '+', '+', '+', '+', '+', '+',
        'a', '<', '+', '+', '+', '+', '+', '+', '+', '+', '+', '>', '-', 'A',
        '<', '.', '+', '.', '!'
    };


    int cmd_index = 0;
    int mem_index = 0;
    int memory[MEM_SIZE] = {0};

    while (cmd_index < 10000) {
        printf("Executing: %c\n", commands[cmd_index]);
        if (commands[cmd_index] == '+') {
            memory[mem_index] += 1;
            print_memory(memory, mem_index);
        }
        if (commands[cmd_index] == '-') {
            memory[mem_index] -= 1;
            print_memory(memory, mem_index);
        }
        if (commands[cmd_index] == '>') {
            mem_index++;
            if (mem_index >= MEM_SIZE) mem_index = 0;
            print_memory(memory, mem_index);
        }
        if (commands[cmd_index] == '<') {
            mem_index--;
            if (mem_index < 0) mem_index = MEM_SIZE - 1;
            print_memory(memory, mem_index);
        }
        if (commands[cmd_index] == '.') {
            printf("Printing %c\n", memory[mem_index]);
        }
        if (commands[cmd_index] == ',') {
            scanf("%d", &memory[mem_index]);
        }
        if ('a' <= commands[cmd_index] && commands[cmd_index] <= 'z' && !memory[mem_index]) {
            int new_cmd_index = 0;
            while (commands[new_cmd_index] != commands[cmd_index] + ('Z' - 'z')) {
                new_cmd_index++;
            }
            cmd_index = new_cmd_index;
        }
        if ('A' <= commands[cmd_index] && commands[cmd_index] <= 'Z' && memory[mem_index]) {
            int new_cmd_index = 0;
            while (commands[new_cmd_index] != commands[cmd_index] + ('z' - 'Z')) {
                new_cmd_index++;
            }
            cmd_index = new_cmd_index;
        }
        if (commands[cmd_index] == '!') {
            cmd_index = 9999;
        }

        cmd_index++;
    }

}

Submission

When you are finished each exercises make sure you submit your work by running give.

You can run give multiple times. Only your last submission will be marked.

Don't submit any exercises you haven't attempted.

If you are working at home, you may find it more convenient to upload your work via give's web interface.

Remember you have until Week 7 Monday 20:00 to submit your work.

You cannot obtain marks by e-mailing your code to tutors or lecturers.

You check the files you have submitted here.

Automarking will be run by the lecturer several days after the submission deadline, using test cases different to those autotest runs for you. (Hint: do your own testing as well as running autotest.)

After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.