Week 02 Laboratory Exercises
Objectives
- using C input/output (I/O) facilities
- creating simple arithmetic expressions
- programming with
if
statements - creating relational expressions
- introducing characters
Activities To Be Completed
The following is a list of all the activities available to complete this week...
Worth 0.7 mark(s) in total:
- addition
- division
- negative
Worth 0.7 mark(s) in total:
- get_letter
- is_leap_year
- shift_inversion
Worth 0.4 mark(s) in total:
- print_faces
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.
Exercise
(●◌◌)
:
Addition
addition.c
, which will add together the number of students and tutors in a class.
This program should ask for two integers using the message
Please enter the number of students and tutors:
and then display the sum of the integers as
students + tutors = sum
### Examples
dcc addition.c -o addition ./addition Please enter the number of students and tutors: 2 5 2 + 5 = 7 ./addition Please enter the number of students and tutors: 3 5 3 + 5 = 8 ./addition Please enter the number of students and tutors: -1 5 -1 + 5 = 4 ### Assumptions/Restrictions/Clarifications - You can assume that the values entered will always be 2 whole numbers (i.e. integers)
1091 style addition.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest addition
When you are finished working on this exercise,
you must
submit your work by running give
:
give dp1091 lab02_addition addition.c
You must run give
before Monday 09 September 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
(●◌◌)
:
Division
Write a program division.c
, which divides two numbers scanned in and rounds the result to the nearest integer, 1 decimal place, 5 decimal places, and 20 decimal places.
In mathematics, a dividend is a number that is divided by another number (the divisor). For example, in the following equation, $$ 10 \div 5 = 2 $$
has 10 as the dividend and 5 as the divisor.
Examples
dcc division.c -o division ./division Enter the dividend: 6 Enter the divisor: 2 6 divided by 2 To the nearest integer: 3 To 1 decimal place: 3.0 To 5 decimal places: 3.00000 To 20 decimal places: 3.00000000000000000000 ./division Enter the dividend: 1 Enter the divisor: 3 1 divided by 3 To the nearest integer: 0 To 1 decimal place: 0.3 To 5 decimal places: 0.33333 To 20 decimal places: 0.33333333333333331483 ./division Enter the dividend: 10 Enter the divisor: 0 Don't be silly!
Assumptions/Restrictions/Clarifications
- You may assume you will be given 2 integers.
- If the divisor is 0, print "Don't be silly!" and return from main.
1091 style division.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest division
When you are finished working on this exercise,
you must
submit your work by running give
:
give dp1091 lab02_division division.c
You must run give
before Monday 09 September 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
(●◌◌)
:
Don't be so negative
Write a complete program negative.c
so that it uses scanf
to get a number from a user and prints
"Don't be so negative!" if they entered a negative number.
If the number is positive, the program should print "You have entered a positive number."
If the user enters the number 0, the program should print
"You have entered zero."
Note: you can assume that the number will always be a whole number (i.e. an integer)
Your program should behave as follows:
dcc -o negative negative.c ./negative 3 You have entered a positive number. ./negative -3 Don't be so negative! ./negative 0 You have entered zero.
1091 style negative.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest negative
When you are finished working on this exercise,
you must
submit your work by running give
:
give dp1091 lab02_negative negative.c
You must run give
before Monday 09 September 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
(●●◌)
:
Print Letters, Given Their Numbers
get_letter.c
that prints a letter from the
alphabet in either uppercase or lowercase, given its position.
**First, scan in a character.**
- This character will determine whether the letter should be printed in uppercase or lowercase.
- The character entered will be either 'y'
or 'n'
, i.e. 'yes' or 'no'.
- If the character entered is 'y'
, then the letter should be printed in *uppercase*.
- If the character entered is 'n'
, then the letter should be printed in *lowercase*.
**Second, scan in an integer.**
- This integer is the 'index' that we will use to determine which letter of the alphabet to print.
- The integer entered will be a number between 1 and 26 (inclusive).
- This means that
- index 1 corresponds to 'A'
,
- index 2 corresponds to 'B'
and
- so on till we reach index 25 which corresponds to 'Z'
.
dcc get_letter.c -o get_letter ./get_letter Uppercase: y Index: 5 The letter is E ./get_letter Uppercase: n Index: 18 The letter is r ### Assumptions/Restrictions/Clarifications - You may assume you will always be given a character first - You may assume you will always be given a whole number second - No error checking is required, i.e. The character entered will always be'y'
or'n'
- The integer entered will always be between 1 and 26 (inclusive).
1091 style get_letter.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest get_letter
When you are finished working on this exercise,
you must
submit your work by running give
:
give dp1091 lab02_get_letter get_letter.c
You must run give
before Monday 09 September 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
(●●◌)
:
Leap Year
Write a C program is_leap_year.c
that reads a year
and then
prints whether that year is a leap year.
Examples
dcc -o is_leap_year is_leap_year.c ./is_leap_year Enter year: 2017 2017 is not a leap year. ./is_leap_year Enter year: 2016 2016 is a leap year. ./is_leap_year Enter year: 2000 2000 is a leap year. ./is_leap_year Enter year: 3000 3000 is not a leap year.
Assumptions/Restrictions/Hints
- You may assume the tested year is not negative.
- You may assume the tested year is a whole number.
1091 style is_leap_year.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest is_leap_year
When you are finished working on this exercise,
you must
submit your work by running give
:
give dp1091 lab02_is_leap_year is_leap_year.c
You must run give
before Monday 09 September 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
(●●◌)
:
Shift Inversion
Write a program shift_inversion.c
which takes a character as input from the user,
along with preferences for case inversion and character shifting,
and prints the modified character based on these preferences.
Examples
dcc shift_inversion.c -o shift_inversion ./shift_inversion Please enter a character: z Would you like to invert the case? y or n: n By how much would you like to shift the character? 3 The character is c! ./shift_inversion Please enter a character: a Would you like to invert the case? y or n: y By how much would you like to shift the character? 5 The character is F! ./shift_inversion Please enter a character: K Would you like to invert the case? y or n: n By how much would you like to shift the character? 12 The character is W! ./shift_inversion Please enter a character: x Would you like to invert the case? y or n: y By how much would you like to shift the character? 10 The character is H!
Assumptions/Restrictions/Clarifications
- You may assume that either 'y' or 'n' will always be entered as input for deciding to invert the case.
- You may assume a valid alphabetical character (A-Z a-z) will be entered as input for the character.
- You may assume that the shift will always be positive
1091 style shift_inversion.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest shift_inversion
When you are finished working on this exercise,
you must
submit your work by running give
:
give dp1091 lab02_shift_inversion shift_inversion.c
You must run give
before Monday 09 September 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
(●●●)
:
Custom Printing Faces
Write a program print_faces.c
that creates a face based on user input.
You will prompt the user to input some characters. These characters correspond to different facial features. These features are:
Character | Group | Feature |
---|---|---|
'a' |
eyes | _ O |
'b' |
eyes | ~ O |
'c' |
eyes | \ O |
'd' |
eyes | / O |
'e' |
nose | ^ |
'f' |
nose | ~ |
'g' |
mouth | \_/ |
'h' |
mouth | /-\ |
'i' |
mouth | o |
The user will put in the eye preference, then nose, then mouth.
If the user wants the eyes to be different, they can enter two letters for the eyes, with the first letter corresponding to the first eye, and the second letter being the second eye. This means the user can enter either 3 or 4 letters.
Examples
dcc print_faces.c -o print_faces ./print_faces How do you want to build a face?: afg _ _ O O ~ \_/ ./print_faces How do you want to build a face?: ceh \ \ O O ^ /-\ ./print_faces How do you want to build a face?: cdei \ / O O ^ o ./print_faces How do you want to build a face?: abeg _ ~ O O ^ \_/ ./print_faces How do you want to build a face?: dbfi / ~ O O ~ o ./print_faces How do you want to build a face?: bbei ~ ~ O O ^ o
### Assumptions/Restrictions/Clarifications - You can assume you will only be given letters between `'a'` and `'i'` inclusive. - You can assume that the characters are given in the correct order (e.g. eyes then nose then mouth) - You can assume you will only be given 3 or 4 letters, no more, no less. - You can assume you will only get one letter corresponding to the nose, and one letter corresponding to the mouth.
1091 style print_faces.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest print_faces
When you are finished working on this exercise,
you must
submit your work by running give
:
give dp1091 lab02_print_faces print_faces.c
You must run give
before Monday 09 September 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
— individual:
(Not For Marks) Debugging - Double
Copy the program debug_double.c
from the course account to your
directory by typing (make sure you type the dot at the end):
cp ~dp1091/public_html/24T3/activities/debug_double/debug_double.c .
Note that this exercise is not marked or worth marks! Do not submit it.
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 - https://cgi.cse.unsw.edu.au/~dp1091/24T3/resources/debugging_guide.html
The Task
This exercise reads a number from the user, doubles it, and prints it out. Currently it has some issues - it is your job to figure them out and fix the code.
Examples
dcc debug_double.c -o debug_double ./debug_double Enter a number: 2 Doubled: 2 x 2 = 4 ./debug_double Enter a number: -5 Doubled: -5 x 2 = -10 ./debug_double Enter a number: 0 Doubled: 0 x 2 = 0
1091 style debug_double.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest debug_double
Exercise
— individual:
(Not For Marks) Debugging - Smallest Prime Factor
Copy the program debug_smallest_prime.c
from the course account to your
directory by typing (make sure you type the dot at the end):
cp ~dp1091/public_html/24T3/activities/debug_smallest_prime/debug_smallest_prime.c .
Note that this exercise is not marked or worth marks! Do not submit it.
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 takes a number greater than 1 and determines if 2, 3, or 5, is its smallest prime factor. If it has a different smallest prime factor, like 7, then the program lets you know that that factor is higher than 5.
You can assume the program is never given any number smaller than 2.
Examples
dcc debug_smallest_prime.c -o debug_smallest_prime ./debug_smallest_prime Enter a number: 6 The lowest prime factor is 2. ./debug_smallest_prime Enter a number: 15 The lowest prime factor is 3. ./debug_smallest_prime Enter a number: 55 The lowest prime factor is 5.
### Assumptions/Restrictions/Clarifications - You can assume that the values entered will always be a whole number greater than 1. - You can assume you will not be given negative numbers
1091 style debug_smallest_prime.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest debug_smallest_prime
Submission
give
.
You only need to do this if the exercise specifies a give command, otherwise - the exercise is not worth marks.
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 3 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
Generative AI Permission Level
In completing this assessment, you are permitted to use standard editing and referencing functions in the software you use to complete your assessment. These functions are described below. You must not use any functions that generate or paraphrase passages of text or other media, whether based on your own work or not.
If your Convenor has concerns that your submission contains passages of AI-generated text or media, you may be asked to account for your work. If you are unable to satisfactorily demonstrate your understanding of your submission, you may be referred to UNSW Conduct & Integrity Office for investigation for academic misconduct and possible penalties.
DPST1091/CPTG1391 Specific Information
You are permitted to use the tools dcc-help to help you understand the error messages you may get when compiling the code you have written.
You are permitted to use autotest-help to help you understand why your code may not be passing the automated tests.
You are not permitted to submit code generated by automatic AI tools such as Github Copilot, ChatGPT, Google Bard in DPST1091/CPTG1391/COMP1511 for assignments. Submitting code generated by Github Copilot, ChatGPT, Google Bard and similar tools will be treated as plagiarism.
Our reasoning behind our decisions:
Systems such as Github Copilot and ChatGPT based on large language models or other generative artificial intelligence techniques, look likely to become heavily used by programmers. However, you need a good understanding of the language you are coding in and the systems involved before you can effectively use these tools. Using these tools to generate code for DPST1091/CPTG1391/COMP1511 instead of writing the code yourself will hinder your learning.