Programming Fundamentals
Information
- This page contains additional revision exercises for week 04.
- 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
(●◌◌)
:
Working with Enums (Revision Session Exercise)
Copy the file(s) for this exercise to your CSE account using the following command:
1511 fetch-revision 04
Write a C program enum_weekday.c
that prompts the user to enter a day of the
week for their tutorial as an integer and prints a message indicating the day.
Use an enum to represent the days of the week.
No autotests are provided for this question.
Examples
dcc enum_weekday.c -o enum_weekday ./enum_weekday Please enter the day of the week your tutorial is on: 0: Monday 1: Tuesday 2: Wednesday 3: Thursday 4: Friday 5: Saturday 6: Sunday 0 Your tutorial is on Monday! ./enum_weekday Please enter the day of the week your tutorial is on: 0: Monday 1: Tuesday 2: Wednesday 3: Thursday 4: Friday 5: Saturday 6: Sunday 3 Your tutorial is on Thursday!
Assumptions/Restrictions/Clarifications
- You can assume the user supplies a valid integer between 0 and 6.
- You do not have to check the return value from scanf.
Exercise
(●◌◌)
:
Working with Structs (Revision Session Exercise)
Copy the file(s) for this exercise to your CSE account using the following command:
1511 fetch-revision 04
Modify the given C program structs.c
so that it initialises a struct representing a tutorial and then allows the user to update the time of the tutorial if it is scheduled on Tuesday.
Use an enum to represent the days of the week, and a struct to store details of the tutorial, including the day, time, stream, and number of students.
No autotests are provided for this question.
Examples
dcc struct_tutorial.c -o struct_tutorial ./struct_tutorial Time: 15, Stream: A. That time is full! Please enter a new time! 17 Your new time is: 17
Assumptions/Restrictions/Clarifications
- You can assume the initial tutorial day is Tuesday, and the user will enter a valid integer for the new time if prompted. - You do not have to check the return value from scanf.
Exercise
(●◌◌)
:
Working with While Loops (Revision Session Exercise)
Copy the file(s) for this exercise to your CSE account using the following command:
1511 fetch-revision 04
Write a C program while_grid_printer.c
that performs the following tasks using while loops:
- Prints all numbers from 0 to
SIZE
(not inclusive) that are divisible by 2. - Prints a
SIZE * SIZE
grid of 'X's. - Prints a
SIZE * SIZE
grid where every odd column is 'O' and every other position is 'X'. - Prints a
SIZE * SIZE
grid where the diagonals are 'O' and every other position is 'X'.
No autotests are provided for this question.
Examples
dcc while_grid_printer.c -o while_grid_printer ./while_grid_printer Part 1: 0 2 4 6 8 Part 2: X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Part 3: X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O X O Part 4: O X X X X X X X X O X O X X X X X X O X X X O X X X X O X X X X X O X X O X X X X X X X O O X X X X X X X X O O X X X X X X X O X X O X X X X X O X X X X O X X X O X X X X X X O X O X X X X X X X X O
Exercise
(●◌◌)
:
Scanning in Numbers Until Ctrl D (Revision Session Exercise)
Copy the file(s) for this exercise to your CSE account using the following command:
1511 fetch-revision 04
Write a C program number_scanner.c
that performs the following tasks:
- Prompts the user to enter a number and prints the number.
- Creates a loop that repeatedly scans and prints a single number until the user hits CTRL + D.
- Creates a loop that repeatedly scans and prints two numbers until the user hits CTRL + D.
No autotests are provided for this question.
Examples
dcc number_scanner.c -o number_scanner ./number_scanner Enter a number: 5 The number was 5 10 The number was 10 3 The number was 3
Assumptions/Restrictions/Clarifications
You can assume the user provides valid integers.
Exercise
(●●◌)
:
Guessing Card Game (Revision Session Exercise)
Copy the file(s) for this exercise to your CSE account using the following command:
1511 fetch-revision 04
Write a C program guessing_cards.c
that implements a small guessing game where
two players try to guess each other's card (number and suit). The game consists
of five rounds, with each player having a turn to guess the other’s card in each
round. Players score points based on correct guesses (1 point for guessing the
number, 1 point for guessing the suit). The game ends if one or both players
correctly guess the other’s card before five rounds are up, declaring the winner
or a draw. If the game reaches five rounds without a correct guess, it prints
the final score and declares the winner or a draw.
You should:
- Use structs to represent a card and a player.
- Use enums to represent the suits.
- Write functions to initialise cards, update points, and print the final results.
No autotests are provided for this question.
Examples
dcc guessing_card.c -o guessing_card ./guessing_card Player 1, please enter your card number & suit: 7 2 Player 2, please enter your card number & suit: 10 3 Round 1! Player 1, please guess player 2's card: 9 9 Player 2, please guess player 1's card: 5 2 Player 1: 0, Player 2: 1 Round 2! Player 1, please guess player 2's card: 10 3 Player 2, please guess player 1's card: 7 3 Player 1: 2, Player 2: 2 Game over! It's a draw!
Assumptions/Restictions/Clarifications
You can assume the user inputs valid card numbers and suits. You do not have to check the return value from scanf.
Exercise
(●◌◌)
:
Print the Middle Integer of 3 Integers
Download middle3.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity middle3
Write a C program middle3.c
that reads 3 integers into a struct and prints
the middle integer.
You are not permitted to use loops or arrays (use if statements).
Examples
dcc middle3.c -o middle3 ./middle3 Enter integer: 23 Enter integer: 5 Enter integer: 27 Middle: 23 ./middle3 Enter integer: 3 Enter integer: 6 Enter integer: 27 Middle: 6 ./middle3 Enter integer: 9 Enter integer: 7 Enter integer: 8 Middle: 8
You can assume the user supplies 3 integers. You do not have to check the return value from scanf.
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest middle3
Exercise
(●◌◌)
:
Loop Sum
Write a program called loop_sum.c
that reads an integer
n from standard input, and then scans in n integers from
standard input, adds them together, then prints the sum.
You can assume that n is non-negative (>= 0).
You can assume that you are given exactly n + 1 integers.
You can assume that scanf succeeds (you do not need to check for errors).
Make your program match the examples below exactly.
Note: you are not permitted to use an array in this exercise.
Examples
dcc loop_sum.c -o loop_sum ./loop_sum How many numbers: 2 1 2 The sum is: 3
How many numbers: 3 -3 4 13 The sum is: 14
How many numbers: 5 -2 -1 0 1 2 The sum is: 0
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest loop_sum
Exercise
(●●◌)
:
Print an Hourglass
Write a program called hourglass.c
that prompts the user to enter
an integer n and prints an nxn pattern containing an hourglass. This
hourglass will be made up of *'s', all surrounded by hyphens "-".
Please enter a size: 9 ********* -*******- --*****-- ---***--- ----*---- ---***--- --*****-- -*******- *********
You can assume n is >= 3.
Make your program match the examples below exactly.
This exercise is designed to give you practice with while loops and if statements. Do not use arrays for this exercise!
Note: you are not permitted to use an array in this exercise.
Examples
dcc hourglass.c -o hourglass ./hourglass Please enter a size: 3 *** -*- *** ./hourglass Please enter a size: 6 ****** -****- --**-- --**-- -****- ****** ./hourglass Please enter a size: 7 ******* -*****- --***-- ---*--- --***-- -*****- ******* Please enter a size: 10 ********** -********- --******-- ---****--- ----**---- ----**---- ---****--- --******-- -********- ********** ./hourglass Please enter a size: 15 *************** -*************- --***********-- ---*********--- ----*******---- -----*****----- ------***------ -------*------- ------***------ -----*****----- ----*******---- ---*********--- --***********-- -*************- *************** ./hourglass Please enter a size: 25 ************************* -***********************- --*********************-- ---*******************--- ----*****************---- -----***************----- ------*************------ -------***********------- --------*********-------- ---------*******--------- ----------*****---------- -----------***----------- ------------*------------ -----------***----------- ----------*****---------- ---------*******--------- --------*********-------- -------***********------- ------*************------ -----***************----- ----*****************---- ---*******************--- --*********************-- -***********************- *************************
For this exercise, try and break the problem down into small parts and solve them individually.
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest hourglass
Exercise
(●●◌)
:
Count the big numbers in the array
Download count_bigger.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity count_bigger
Your task is to add code to this function in count_bigger.c:
// return the number of "bigger" values in an array (i.e. larger than 99
// or smaller than -99).
int count_bigger(int length, int array[]) {
// PUT YOUR CODE HERE (you must change the next line!)
return 42;
}
count_bigger
should return a single integer: the number of values in the
array which are larger than 99 or smaller than -99.
For example if the array contains these 8 elements:
141, 5, 92, 6, 535, -89, -752, -3
Your function should return 3
, because these 3
elements are
bigger than 99
or smaller than -99
:
141, 535, -752
Assumptions/Restrictions/Clarifications
count_bigger
should return a single integercount_bigger
should not change the array it is givencount_bigger
should not callscanf
(orgetchar
orfgets
)- You can assume the array contains at least one integer
count_bigger
should not print anything. It should not callprintf
- Your submitted file may contain a main function. It will not be tested or marked
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest count_bigger
Exercise
(●●●)
:
Array Functions
Download array_functions.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity array_functions
In this exercise, you'll be creating a small collection of functions that could help you with working on arrays.
Your task is to implement the missing functions in array_functions.c
and
implement the functions.
Some of these functions read through an array and return some information about the array but do not change the array in any way.
These functions are:
array_max
returns the maximum value in the array,array_min
returns the minimum value in the array,array_average
returns the average value in the array, andarray_sum
returns the sum of all values in the array.
Note: array_average
should return the greatest int
less than or equal to
the statistical mean of the array.
Some other functions change the array values or have an effect outside the program. We say that these functions have side effects as they do something other than just return a value.
These functions are:
array_add
; which adds a value to every element in the array,array_scale
; which multiplies every value in array by the same value, andscan_array
; which reads in values from input and stores them in the arrayshow_array
; which shows an array on the screen in the style[1, 2, 3, 4, 5, 6]
copy_array
; which copies one array into another
The file array_functions.c
contains a main function with some unit tests to
help you test your functions.
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest array_functions
Exercise
(●●●)
:
Return Common Elements Of Two Arrays
Download common_elements.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity common_elements
Your task is to add code to this function in common_elements.c:
int common_elements(int length, int source1[length], int source2[length], int destination[length]) {
// PUT YOUR CODE HERE (you must change the next line!)
return 42;
}
common_elements
should copy the values in the first source array which are
also found in the second source array, into the third array.
In other words, all of the elements that appear in both of the source1
and
source2
should be copied to the third destination
array, in the order that
they appear in the first array.
common_elements
should return a single integer: the number of elements
copied to the destination array.
For example, if the source arrays contained the following 6 elements:
source1: 1, 4, 1, 5, 9, 2
source2: 1, 1, 8, 2, 5, 3
common_elements
should copy these 4 values to the destination array:
1, 1, 5, 2
The value 4 and 9 do not get copied because they do not occur in the second array.
common_elements
should return the integer 4, because there were 4 values
copied.
common_elements
should copy the elements in the order that they appear in the
first array.
If a value in the first array occurs one or more times in the second array, each occurrence of those values in the first array should be copied.
It doesn't matter how many times the values occur in the second array, as long as the values occur at least once.
For example. if the two arrays contained the following 5 elements:
source1: 1, 2, 3, 2, 1
source2: 1, 2, 3, 4, 5
Your function should copy all five values (1, 2, 3, 2, 1
) from source1
to
the destination array as all of the values (1, 2, 3
) appeared at least
once in source2
and it should return 5 because because 5 values were copied.
Assumptions/Restrictions/Clarifications
- You can assume the two source arrays contain only positive integers.
- You can assume that all three arrays are the same size (
length
) andlength
is> 0
. - You cannot assume anything about the number of common elements, i.e. there may not be any common elements between both arrays, or conversely, the entire contents of the first array may also be present in the second array.
common_elements
should return a single integercommon_elements
should not change the array it is givencommon_elements
should not call scanf (or getchar or fgets)common_elements
should not print anything. It should not call printf- Your submitted file may contain a main function. It will not be tested or marked
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest common_elements