Programming Fundamentals
Objectives
- using arrays
- using struct arrays
- creating functions
- using while loops for repetition
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 COMP1511 Help Session Microsoft Teams.
For face-to-face help sessions, the lab map can be found Here.
Activities To Be Completed
The following is a list of all the activities available to complete this week...
Worth 1 mark(s) in total:
- devowel
- print_pi_style
- scanning_into_array_style
- points
Worth 1 mark(s) in total:
- savings_analysis
- cs_calculator
Worth 0.5 mark(s) in total:
- detective
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
(●◌◌)
:
Devowelling Text
Write a C program devowel.c
which reads characters from its input and writes
the same characters to its output, except it does not write lower case vowels
('a', 'e', 'i', 'o', 'u').
Your program should stop only at the end of input.
Examples
dcc devowel.c -o devowel ./devowel Are you saying 'Boo' or 'Boo-Urns'? Ar y syng 'B' r 'B-Urns'? In this house, we obey the laws of thermodynamics! In ths hs, w by th lws f thrmdynmcs!
1511 style devowel.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest devowel
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab04_devowel devowel.c
You must run give
before Monday 11 March 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.
Exercise
(●◌◌)
:
Print out pi to a certain number of digits
Good code style is required for this exercise.
To get full marks in this exercise, you MUST pass the automatic style checker
(1511 style
).
If 1511 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:
1511 style print_pi_style.c
Download print_pi_style.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity print_pi_style
Your task is to add code to these functions in print_pi_style.c:
void print_pi_style(int num_digits) {
// DO NOT CHANGE THIS LINE
int pi[MAX_DIGITS] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
// TODO: Finish this function below
}
Write a C program, print_pi_style.c
, which prints out the first n digits of
pi, where n
is specified by the user.
The main function has already been written for you. There's an empty function
that has been provided for you called print_pi_style
that takes in one
integer as an argument.
Place your code (only the printing code) inside this function. You can NOT put all of your code in main for this exercise, main must remain unmodified.
Examples
dcc print_pi_style.c -o print_pi_style ./print_pi_style How many digits of pi would you like to print? 3 3.14 ./print_pi_style How many digits of pi would you like to print? 5 3.1415 ./print_pi_style How many digits of pi would you like to print? 10 3.141592653
Assumptions/Restrictions/Clarifications
- You can assume that n >= 2 AND n <= 10, however, do not use this as a reason to avoid while loops.
1511 style print_pi_style.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest print_pi_style
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab04_print_pi_style print_pi_style.c
You must run give
before Monday 11 March 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.
Exercise
(●◌◌)
:
Scanning numbers into an array
Good code style is required for this exercise.
To get full marks in this exercise, you MUST pass the automatic style checker
(1511 style
).
If 1511 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:
1511 style scanning_into_array_style.c
Download scanning_into_array_style.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity scanning_into_array_style
Write a C program scanning_into_array_style.c
which scans in a certain amount
of numbers from the user and fills an array with them.
- First, it scans in the quantity of numbers from the user.
- Then, it scans in that many numbers from the user.
Once the array is filled with the numbers the user input, call a pre-written function that prints the minimum and maximum values in that array
Examples
dcc scanning_into_array_style.c -o scanning_into_array_style ./scanning_into_array_style How many numbers: 5 Please enter numbers: 1 2 3 4 5 Minimum: 1 Maximum: 5 ./scanning_into_array_style How many numbers: 4 Please enter numbers: 3 -2 7 1 Minimum: -2 Maximum: 7
Assumptions/Restrictions/Clarifications
- The given inputs will always be integers.
- The given list of numbers will never be more than 100 numbers.
1511 style scanning_into_array_style.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest scanning_into_array_style
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab04_scanning_into_array_style scanning_into_array_style.c
You must run give
before Monday 11 March 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.
Exercise
(●◌◌)
:
Using struct arrays to store points of a shape
Download points.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity points
Complete the C program points.c
, so that it scans in all the points of a shape
into an array of structs. It should then print out the points that were
given.
First, it scans in the number of points that the shape has. Then, it scans in the coordinates of each point. Afterwards, it prints out the points in order.
Examples
dcc points.c -o points ./points How many points in the shape? 2 Enter points: 1.231 4.560 7.892 0.12 Shape Points: 1: x = 1.231, y = 4.560 2: x = 7.892, y = 0.120 ./points How many points in the shape? 4 Enter points: 6.340 2.781 5.675 2.453 4.893 0.123 0.232 3.349 Shape Points: 1: x = 6.340, y = 2.781 2: x = 5.675, y = 2.453 3: x = 4.893, y = 0.123 4: x = 0.232, y = 3.349
Assumptions/Restrictions/Clarifications
- When asked for the number of points, you will be given a non-zero positive integer.
- All inputs will be the correct type and non-zero positive numbers.
- The number of points will not exceed 10.
1511 style points.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest points
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab04_points points.c
You must run give
before Monday 11 March 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.
Exercise
(●●◌)
:
Savings Analysis
Download savings_analysis.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity savings_analysis
You have been provided with some starter code savings_analysis.c
with functions for you to complete: calc_yearly_deposits
, calc_yearly_spending
, calc_ending_balance
and calc_months_saving_goal_met
. It is your job to implement the functions and complete the program.
The program reads in the user's starting balance and monthly saving goal for the year. This is followed by 12 months of deposits and 12 months of spending. The program then runs a series of functions to analyse the account over the year. It is your job to complete these functions.
Examples
dcc savings_analysis.c -o savings_analysis ./savings_analysis Please enter starting balance: 1320 Please enter monthly saving goal: 60 Please enter monthly deposits: 208 234 182 182 208 156 156 182 208 208 234 260 Please enter monthly spending: 120 103 150 122 130 372 106 115 127 124 104 200 Yearly deposits = $2418 Yearly spending = $1773 Ending balance = $1965 You met the monthly saving goal 9 times! ./savings_analysis Please enter starting balance: 500 Please enter monthly saving goal: 220 Please enter monthly deposits: 800 850 900 870 820 880 890 830 910 850 860 840 Please enter monthly spending: 600 620 610 630 640 615 610 625 600 630 635 670 Yearly deposits = $10300 Yearly spending = $7485 Ending balance = $3315 You met the monthly saving goal 8 times!
Assumptions/Restrictions/Clarifications
- The function names in your solution should be the same as the provided starter code.
- Numbers entered will always be positive integers.
- The correct amount of input will always be provided. That is, the number of monthly deposits and monthly spending will always be 12.
- You must implement the functions specified to pass the autotests and recieve marks for this activity.
1511 style savings_analysis.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest savings_analysis
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab04_savings_analysis savings_analysis.c
You must run give
before Monday 11 March 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.
Exercise
(●●◌)
:
Create a simple calculator, reading different numbers of integers
For this exercise, make a program called cs_calculator.c
which will scan in
instructions until End-Of-Input (which we enter to our terminal with the
keyboard shortcut CTRL-D
) and prints the output as
specified below. An instruction is a character, followed by one or two positive
integers.
The first character identifies what type the instruction is.
- If the first character in the instruction is
's'
, then your program should print out the square of the next number in the instruction. - If the first character in the instruction is
'p'
, then your program should print out the value of the next number raised to the power of the number after next.
Examples
dcc cs_calculator.c -o cs_calculator ./cs_calculator Enter instruction: s 2 4 Enter instruction: p 5 3 125 Enter instruction: s 4 16 Enter instruction: p 3 4 81 Enter instruction: ./cs_calculator Enter instruction: p 3 3 27 Enter instruction: s 10 100 Enter instruction:
One major challenge of this exercise is figuring out how to use scanf
effectively. The lessons you learn in this exercise regarding scanf
will be
useful in assignment 1!
Assumptions/Restrictions/Clarifications
- You can assume that the first character in the instruction is only either
's'
or'p'
- You can assume that for each instruction, the correct number of successive positive integers will be given.
- The autotest for this exercise expects your program to end WITHOUT a new line
character when the user presses
Ctrl-D
. This means that the command prompt for the next command should be on the same line as the end of your program.
1511 style cs_calculator.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest cs_calculator
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab04_cs_calculator cs_calculator.c
You must run give
before Monday 11 March 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.
Exercise
(●●●)
:
Detective
Download detective.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity detective
Your mission (if you choose to accept it) is to complete the program detective.c
to decipher encoded messages.
Over the past few months UNSW Security Society has found seemingly normal messages paired with random numbers. To help you solve this challenge we have created the program detective.c
that reads in the message and numbers, storing them in a struct array.
We have been lucky enough to find one decoded message:
Encoded Message: muahahaha_we're_so_evil#_GNTY
Secret Numbers: 7-23-3-6-13-22-24-2-16-4-14-18-20-11-9-28-21-1-27-8-0-26-12-5-10-15-25-19-17
Deciphered Message: haha_we_are_TeasiNG_You
Our observations from this are:
- Messages seem to end with a
#
. - There are always the same number of letters as there are numbers.
- Letters maintain their case between their encoded and deciphered message.
- Each number is unique counting from
0
ton - 1
(wheren
is the number of letters).
We wish you luck detective!
Examples
dcc detective.c -o detective ./detective Please enter Encrypted Message: muahahaha_we're_so_evil#_GNTY Please enter Secret Numbers: 7-23-3-6-13-22-24-2-16-4-14-18-20-11-9-28-21-1-27-8-0-26-12-5-10-15-25-19-17 Secret Message is: haha_we_are_TeasiNG_You ./detective Please enter Encrypted Message: STUDENTS_L0VE_COMP1511_#new_students_aww_:) Please enter Secret Numbers: 28-12-24-38-6-33-36-27-16-7-19-17-39-2-11-14-32-31-41-23-18-15-0-21-35-9-40-20-1-30-5-42-4-25-13-26-37-34-22-8-29-10-3 Secret Message is: sTEw_MeET_at_Unsw_tuNneLS_11:05
Assumptions/Restrictions/Clarifications
- The maximum number of characters is 100.
- The number of characters in the encrypted message and the amount of secret numbers are the same.
- The case of each letter is preserved in translation.
- Messages will end with a
#
.
1511 style detective.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest detective
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab04_detective detective.c
You must run give
before Monday 11 March 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.
Exercise — individual:
(Not for Marks) Refactoring - Class Details
Download class_details.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity class_details
For this activity, you've been provided with a fully working program: class_details.c
.
While this code produces the correct output, it's code style is not very good.
In particular, class_detail.c
is full of magic numbers and repeating code,
and it's main function is more than 200 lines long!
Try running 1511 style class_details.c
to see even more of it's style issues.
For this activity your job is to go through and improve the readability of the code by
- Replacing magic numbers with enums and #defines,
- Breaking it up into small functions (less than 40 lines),
- Using those functions to remove unnecessary repetition,
- Writing some function comments to document the code.
You shouldn't change any of the actual functionality of the program i.e. none of the changes you make should change any of the program's output.
Program description
Below is a description of the program. Keep in mind that this has all already been implemented for you.
The provided program scans in and stores the details of students in a class. It first scans in the size of the class, and then scans in the details of each of the students including:
- zID
- Degree type (Undergraduate/Postgraduate)
- Major (if an Undergraduate)
- Assignments mark (Collated mark of assignments and labs)
- Exam mark
- Course grade
Once all student details have been scanned in, the program then loops and scans
in commands. It doesn't stop looping until the QUIT
command is entered. The
program performs one of the following tasks:
- Command 0 Help: Display program instructions
- Command 1 (Display Student): Print the details of a specific student
- Command 2 (Display Class): Print the details of all students in a class
- Command 3 (Quit): Exit the program
Examples
dcc class_details.c -o class_details ./class_details Enter Class Size: 1 Student 1: Enter zID: 5111111 Select Degree Type: 0: Undergraduate 1: Postgraduate 0 Select Major: 0: Computer Science 1: Database Systems 2: eCommerce Systems 3: Artificial Intelligence 4: Programming Languages 5: Computer Networks 6: Embedded Systems 7: Security Engineering 8: None 2 Enter Assignments mark (out of 60): 35.5 Enter exam mark (out of 40): 30 Enter Command Number (0 for Help): 0 Enter a number corresponding to one of the following commands: 0 (Help): Display program instructions 1 (Display Student): Print the details of a specific student 2 (Display Class): Print the details of all students in a class 3 (Quit): Exit the program Enter Command Number (0 for Help): 1 Enter Student zID: 5222222 No student with that zID exists Enter Command Number (0 for Help): 1 Enter Student zID: 5111111 z5111111: { Degree Type: Undergraduate Major: eCommerce Systems Assignments Mark: 35.50/60 Exam Mark: 30.00/40 Course Grade: 65.50/100 } Enter Command Number (0 for Help): 100 Invalid Command Enter Command Number (0 for Help): 3 Exiting Program ./class_details Enter Class Size: 2 Student 1: Enter zID: 5111111 Select Degree Type: 0: Undergraduate 1: Postgraduate 0 Select Major: 0: Computer Science 1: Database Systems 2: eCommerce Systems 3: Artificial Intelligence 4: Programming Languages 5: Computer Networks 6: Embedded Systems 7: Security Engineering 8: None 6 Enter Assignments mark (out of 60): 35.5 Enter exam mark (out of 40): 30 Student 2: Enter zID: 5222222 Select Degree Type: 0: Undergraduate 1: Postgraduate 1 Enter Assignments mark (out of 60): 40.5 Enter exam mark (out of 40): 25.9 Enter Command Number (0 for Help): 2 Students: z5111111: { Degree Type: Undergraduate Major: Embedded Systems Assignments Mark: 35.50/60 Exam Mark: 30.00/40 Course Grade: 65.50/100 } z5222222: { Degree Type: Postgraduate Assignments Mark: 40.50/60 Exam Mark: 25.90/40 Course Grade: 66.40/100 } Enter Command Number (0 for Help): 1 Enter Student zID: 5222222 z5222222: { Degree Type: Postgraduate Assignments Mark: 40.50/60 Exam Mark: 25.90/40 Course Grade: 66.40/100 } Enter Command Number (0 for Help): 3 Exiting Program
Assumptions/Restrictions/Hints
- You should not change the functionality of the program in any way
- If there are any edge cases not handled by the original code (e.g. students sharing zids), you do not need to modify the code to handle them
- All inputs will positive, and be the correct type
1511 style class_details.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest class_details
Exercise — individual:
(Not For Marks) Debugging - Letter Search
Download debug_letter_search.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity debug_letter_search
Note that this exercise is not marked or worth marks!
Debugging Tips!
Some debugging tips for you:
- dcc output - as you run into issues, dcc will point you to 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 that you got against what you expected. - COMP1511 debugging guide
The Task
This exercise reads in a letter from the user, and then searches more input given by the user for that letter, or until CTRL+D is entered. It then tells the user if the search was sucessful or not.
Examples
dcc debug_letter_search.c -o debug_letter_search ./debug_letter_search Which letter are we searching for?: g COMP1511 is awesome! hmmm, how about this? what about now? hm. How about frog? We found g!!! ./debug_letter_search Which letter are we searching for?: ? No questions here. Only sentences. I could have a question. Nah The mission was not successful :(
Walkthrough
Below is a video walkthrough of this exercise! Make sure to attempt it before watching this video
1511 style debug_letter_search.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest debug_letter_search
Exercise — individual:
(Not For Marks) Debugging - Clouds
Download debug_cloud.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity debug_cloud
Note that this exercise is not marked or worth marks!
Debugging Tips!
Some debugging tips for you:
- dcc output - as you run into issues, dcc will point you to 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 that you got against what you expected. - COMP1511 debugging guide
The Task
This exercise reads in some cloud heights from a user and works out what type of clouds they are. The program then prints out the total count for all the types.
Note that fog clouds are the lowest, then cumulo, then alto, then cirro clouds occur at the highest heights.
Examples
dcc debug_cloud.c -o debug_cloud ./debug_cloud Enter some clouds: 200 550 880 2500 100000 CLOUDS There are 1 fog clouds There are 2 cumulo clouds There are 1 alto clouds There are 1 cirro clouds ./debug_cloud Enter some clouds: 65000 80000 3000 4000 4690.23 12.54 24.57 8 CLOUDS There are 3 fog clouds There are 0 cumulo clouds There are 3 alto clouds There are 2 cirro clouds
Walkthrough
Walkthrough
Below is a video walkthrough of this exercise! Make sure to attempt it before watching this video
1511 style debug_cloud.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest debug_cloud
Submission
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 5 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.