Week 05 Laboratory Exercises

Objectives

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

Activities To Be Completed

This lab is worth a total of 1.4 marks and consists of the following activities:

TypeActivitiesWeight
One-dot (●◌◌) debug_stringcmp
word_square
alternating_case
simple_snake
15%
Two-dot (●●◌) planter
palindrome
15%
Group presentation presentation5
70%

Lab exercises are capped at 15 marks. For more details, see the course outline.

Exercise
(●◌◌)
:

Debugging - stringcmp

Good code style is required for this exercise.

To get full marks in this exercise, you MUST pass the automatic style checker (1091 style).

If 1091 style finds any issues in your code, the maximum mark you can achieve for this exercise is capped at 75%.

To run the automatic style checker on your code, use the following command:

1091 style debug_stringcmp.c

Download debug_stringcmp.c here

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

1091 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.
  • DPST1091 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:
1091 style debug_stringcmp.c

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

1091 autotest debug_stringcmp

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

give dp1091 lab05_debug_stringcmp debug_stringcmp.c

You must run give before Monday 23 February 09: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.

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:
1091 style word_square.c

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

1091 autotest word_square

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

give dp1091 lab05_word_square word_square.c

You must run give before Monday 23 February 09: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.

Exercise
(●◌◌)
:

Alternating case

Download alternating_case.c here

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

1091 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:
1091 style alternating_case.c

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

1091 autotest alternating_case

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

give dp1091 lab05_alternating_case alternating_case.c

You must run give before Monday 23 February 09: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.

Exercise
(●◌◌)
:

Creating a simple Snake game

Download simple_snake.c here

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

1091 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 involves:

  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:
1091 style simple_snake.c

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

1091 autotest simple_snake

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

give dp1091 lab05_simple_snake simple_snake.c

You must run give before Monday 23 February 09: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.

Exercise
(●●◌)
:

Planter

Download planter.c here

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

1091 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:
1091 style planter.c

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

1091 autotest planter

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

give dp1091 lab05_planter planter.c

You must run give before Monday 23 February 09: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.

Exercise
(●●◌)
:

Palindrome

Download palindrome.c here

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

1091 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:
1091 style palindrome.c

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

1091 autotest palindrome

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

give dp1091 lab05_palindrome palindrome.c

You must run give before Monday 23 February 09: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.

In-Class Exercise — group:
Presentation

This week during lab class, you will be divided into small groups and assigned an activity from this week to give a short presentation on (2–5 minutes per group).

There is no give or autotest for the group presentation.

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 6 Monday 9:00am 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.

Lab Marks

When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:

1091 classrun -sturec

Voice of the Student

✨ Help Us Improve Your Learning Experience ✨

Your feedback helps us understand what’s working well and what might need improvement. This quick, anonymous check-in has just two questions and takes less than a minute to complete.

Please answer honestly — your input makes a real difference.