Programming Fundamentals

Information

  • This page contains additional revision exercises for week 06.
  • These exercises are not compulsory, nor do they provide any marks in the course.
  • You cannot submit any of these exercises, however autotests may be available for some them (Command included at bottom of each exercise if applicable).

Exercise — individual:
Array Score

Download array_score.c here

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

1511 fetch-activity array_score

Together with your tutor, you will complete the program array_score.c. This program:

  • Reads five numbers from the user, storing them in an array.
  • Calculates the total sum of all elements in the array.
  • Prints the sum to the terminal.

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

1511 autotest array_score

Exercise — individual:
Working with Arrays

Your tutor will split you into groups to use the provided code snippets and code descriptions to solve the following questions.

1. Array Offset

Create a program array_offset.c that reads in an offset from the user and adds it to every element of an array. It should then print out the resulting array so its new values can be checked.

2. Array Multiplier

Create a program array_multiplier.c that reads in a multiplier from the user and multiplies every element of an array by it. It should then print out the resulting array so its new values can be checked.

3. Prefix Sum

Create a program prefix_sum.c that transforms a predefined array into its prefix sum: each index of the array should end up holding the sum of all the original values up to and including that index, rather than just its own original value. It should then print out the resulting array so its new values can be checked.

Exercise — individual:
Refactoring Array Score

In this activity, you will refactor your solution from the previous array_score.c exercise.

Your task is to break the code down into modular, distinct functions to improve readability, maintenance, and structure.

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

1511 autotest refactoring_array_score

Exercise — individual:
Cats Amongst the Pigeons

Download cat_amongst_pigeons.c here

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

1511 fetch-activity cat_amongst_pigeons

In a large city, it is common to find both pigeons and stray cats sharing the same spaces. However, allowing these animals to live in close quarters can lead to absolute chaos.

Your task is to complete the program cat_amongst_pigeons.c using the provided starter code. The program should track the locations of cats and pigeons in a 10x10 city square represented by a 2D array, continuously scanning in the positions of animals until the user inputs [Ctrl-D].

  • Pigeons are represented on the grid by the characters >0.
  • Cats are represented on the grid by the characters ^-^.
  • Empty spaces are represented by three blank spaces    .

Your program should repeatedly prompt the user with "Enter the locations of the cats and pigeons: " and read:

  • A character ('c' to place a cat, 'p' to place a pigeon).
  • An integer specifying the row index (0–9).
  • An integer specifying the column index (0–9).

After reading these characters, you will need to check for the following errors:

  1. If the user attempts to place a new animal on a cell that is not EMPTY, the program should print: "Cannot place animal on an occupied cell.\n";
  2. If the user attempts to place a new animal directly adjacent (horizontally, vertically, or diagonally) to another animal of a different species, the program should not add the new animal and should print: "Oh no! Those animals are too close.\n".

If there are no errors, add the new animal by updating the corresponding cell in the array to hold the new animal's type (CAT or PIGEON).

After each input and possible error message, your program should print the map, with the provided print_map function.

Example

Input:

c 2 2
p 2 3
p 7 7
c 6 7
p 6 7

Input and Output:

dcc cat_amongst_pigeons.c -o cat_amongst_pigeons
./cat_amongst_pigeons
Enter the locations of the cats and pigeons: c 2 2

------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |^-^|   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
Enter the locations of the cats and pigeons: p 2 3
Oh no! Those animals are too close.

------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |^-^|   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
Enter the locations of the cats and pigeons: p 7 7

------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |^-^|   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |>0 |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
Enter the locations of the cats and pigeons: c 6 7
Oh no! Those animals are too close.

------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |^-^|   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |>0 |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
Enter the locations of the cats and pigeons: p 6 7

------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |^-^|   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |>0 |   |   |
------------------------------------------
|   |   |   |   |   |   |   |>0 |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
|   |   |   |   |   |   |   |   |   |   |
------------------------------------------
Enter the locations of the cats and pigeons: [Ctrl-D]

Assumptions/Restrictions/Clarifications

  • You may assume that the number of animals on the map will never exceed the size of the map.
  • You may assume only valid inputs will be entered.
  • If there are multiple errors for an input, your program should only print the first error in the order they were provided.

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

1511 autotest cat_amongst_pigeons

Exercise — individual:
Student Information

Download student_information.c here

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

1511 fetch-activity student_information

Your task is to complete the program student_information.c using the provided starter code. The program should read the details of two students from the command line. For each student, the details collected are their first name, last name, and zID.

Your program should then print both students' information, ordered lexicographically by their last name.

Examples

dcc student_information.c -o student_information
./student_information Sophie Moeskops z5555555 Amber Lucas z3333333
Student details in alphabetical order by last name:
First name: Amber
Last name: Lucas
zid: z3333333

First name: Sophie
Last name: Moeskops
zid: z5555555

Assumptions/Restrictions/Clarifications

  • No error checking is needed.

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

1511 autotest student_information

Exercise — individual:
Crochet (Revision Session Exercise)

Download crochet.c here

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

1511 fetch-activity crochet

Your task is to complete the program crochet.c using the provided starter code. Your program should track how many rows of a crochet project were completed on each day of the week, and works out whether the project is finished.

Your program should:

  • Prompt the user with "Enter the number of rows completed per day this week: " and read 7 integers, one for each day of the week. The integers should be stored in an array.
  • Prompt the user with "Enter the total number of rows needed to complete the project: " and read a single integer specifying how many rows are needed to complete the project.
  • Calculate the total number of rows completed over the week by calculating the sum of all values in the array.
  • Compare this total to the number of rows needed:
    • If the total rows completed is greater than or equal to the rows needed, print "You finished the project!\n".
    • Otherwise, print "You have x rows left to complete the project.\n", where x is the number of rows that still need to be completed.

Examples

dcc crochet.c -o crochet
./crochet
Enter the number of rows completed per day this week: 3 4 2 5 3 4 6
Enter the total number of rows needed to complete the project: 30
You have 3 rows left to complete the project.
./crochet
Enter the number of rows completed per day this week: 10 8 12 9 11 7 10
Enter the total number of rows needed to complete the project: 50
You finished the project!

Assumptions/Restrictions/Clarifications

  • No error checking is required

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

1511 autotest crochet

Exercise — individual:
Even Sums (Revision Session Exercise)

Download even_sums.c here

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

1511 fetch-activity even_sums

Your task is to complete the program even_sums.c using the provided starter code.

Your program should implement two functions:

  • count_even_rows: takes a 2D array, grid, and returns the number of rows whose elements sum to an even number.
  • count_even_cols: takes a 2D array, grid, and returns the number of columns whose elements sum to an even number.

Examples

Example 1:

If the grid contains these elements:

{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 }

count_even_rows should return 5 and count_even_cols should return 5, since every row and column sums to 0, which is even.

Example 2:

If the grid contains these elements:

{ 1, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0 },
{ 0, 0, -1, 0, 0 },
{ 0, 0, 0, 1, 0 },
{ 0, 0, 0, 0, -1 }

count_even_rows should return 0 and count_even_cols should return 0, since every row and column sums to 1 or -1, which are odd.

Example 3:

If the grid contains these elements:

{ 3, 2, 4, 6, -8 },
{ 2, 4, 6, 8, 10 },
{ 7, 5, -2, 4, 6 },
{ 44, -3, 6, 8, 10 },
{ 2, 4, 6, 7, -8 }

count_even_rows should return 2 and count_even_cols should return 4:

  • Row sums are 7, 29, 20, 66, 11 — two of these (20, 66) are even.
  • Column sums are 58, 14, 21, 18, 10 — four of these (58, 14, 18, 10) are even.

Assumptions/Restrictions/Clarifications

  • No error checking is needed.
  • Elements may be positive, negative, or zero.

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

1511 autotest even_sums

Exercise — individual:
Matrix Sums Debug (Revision Session Exercise)

Download matrix_sum_debug.c here

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

1511 fetch-activity matrix_sum_debug

The provided starter code matrix_sum_debug.c has two functions:

  1. `full_sum, which calculates and returns the total sum of all elements in a 2D array, and
  2. diagonal_sum which calculates and return the sum of elements along the top-left to bottom-right diagonal.

However, this program has many bugs. Look through the program and try fix them so that the program works as expected!

Debugging Tips!

Some debugging tips for you:

  • dcc output: as you run into issues, dcc will point you to where the errors are.
    • It gives you the line number the issue is on along with some explanation, so make sure you read everything it tells you.
    • Sometimes we get "errors carried forward," so find your first error, fix that, then recompile before addressing the rest.
  • print statements: it can be handy to check whether the flow of your code puts you where you expect (for example, inside the right if statement, or looping the correct number of times).
    • A quick way to do this is by adding temporary print statements, like "the value of x is %d and y is %d", so you can compare what you got against what you expected.

For more advice, see the full COMP1511 debugging guide.

Example

dcc matrix_sum_debug.c -o matrix_sum_debug
./matrix_sum_debug
Full sum: 190
Sum of top-left to bottom-right diagonal: 9

Assumptions/Restrictions/Clarifications

  • No error checking is needed.

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

1511 autotest matrix_sum_debug

Exercise — individual:
Constellations (Revision Session Exercise)

Download constellations.c here

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

1511 fetch-activity constellations

Your task is to complete the program constellations.c using the provided starter code. The program should track star counts across a series of constellations.

Your program should read the number of stars in each constellation as command-line arguments, where each argument represents one constellation, and its value is the number of stars observed in it.

Your program should:

  • If no arguments are given, print "The sky was empty!\n".
  • Otherwise, print "Tonight, there were x constellations:\n", where x is the total number of constellations (arguments) given.
  • Then print a list of all constellations with each line in the form "Constellation n: y star(s)\n", where n is the constellation's position (starting at 1) and y is its star count.

Examples

./constellations 4 5 1
Tonight, there were 3 constellations:
Constellation 1: 4 star(s)
Constellation 2: 5 star(s)
Constellation 3: 1 star(s)
./constellations
The sky was empty!

Assumptions/Restrictions/Clarifications

  • No error checking is needed.
  • The numbered list of constellations should be 1-indexed, meaning the first constellation is Constellation 1:.

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

1511 autotest constellations

Exercise — individual:
Looking at strings (Revision Session Exercise)

Download strings_length.c here

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

1511 fetch-activity strings_length

Your task is to complete the program strings_length.c using the provided starter code. Your program should read two strings from standard input, calculate the length of each string, and then determine which of the two strings is larger.

Your program should:

  • Prompt the user with "Enter first string: " and read a string.
  • Prompt the user with "Enter second string: " and read a second string.
  • Print the length of each string, not counting the trailing newline.
  • Compare the two strings lexicographically and print exactly one of the following:
    • "The first string is larger.\n", if the first string comes later lexicographically than the second.
    • "The second string is larger.\n", if the second string comes later lexicographically than the first.
    • "The strings are the same.\n", if the two strings are identical.

Examples

dcc strings_length.c -o strings_length
./strings_length
Enter first string: hello
Enter second string: world
Length of first string: 5
Length of second string: 5
The second string is larger.
./strings_length
Enter first string: same
Enter second string: same
Length of first string: 4
Length of second string: 4
The strings are the same.
./strings_length
Enter first string: xylophone
Enter second string: aardvark
Length of first string: 9
Length of second string: 8
The first string is larger.

Assumptions/Restrictions/Clarifications

  • No error checking is needed.

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

1511 autotest strings_length