Programming Fundamentals
Objectives
- using C input/output (I/O) facilities
- creating simple arithmetic expressions
- programming with
if
statements - creating relational expressions
Activities To Be Completed
The following is a list of all the activities available to complete this week...
Worth 1 mark(s) in total:
- addition
- division
- negative
Worth 1 mark(s) in total:
- get_letter
- is_leap_year
- shift_inversion
Worth 0.5 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.
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
(●◌◌)
:
Addition
Create a program called 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)
1511 style addition.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest addition
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab02_addition addition.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Tuesday 27 February 20:00
to obtain the marks for this lab exercise.
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.
1511 style division.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest division
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_division division.c
You must run give
before Tuesday 27 February 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
(●◌◌)
:
Don't be so negative
Complete the 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."
Examples
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.
Assumptions/Restrictions/Clarifications
- You can assume that the number will always be a whole number (i.e. an integer)
1511 style negative.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest negative
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_negative negative.c
You must run give
before Tuesday 27 February 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 Letters, Given Their Numbers
Download get_letter.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity get_letter
Write a C program 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 0 and 25 (inclusive).
- This means that
- index 0 corresponds to
'A'
, - index 1 corresponds to
'B'
and - so on till we reach index 25 which corresponds to
'Z'
.
- index 0 corresponds to
Examples
dcc get_letter.c -o get_letter ./get_letter Uppercase: y Index: 4 The letter is E ./get_letter Uppercase: n Index: 17 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 0 and 25 (inclusive).
1511 style get_letter.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest get_letter
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_get_letter get_letter.c
You must run give
before Tuesday 27 February 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
(●●◌)
:
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.
1511 style is_leap_year.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest is_leap_year
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_is_leap_year is_leap_year.c
You must run give
before Tuesday 27 February 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
(●●◌)
:
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
1511 style shift_inversion.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest shift_inversion
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_shift_inversion shift_inversion.c
You must run give
before Tuesday 27 February 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
(●●●)
:
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 |
---|---|---|
|
eyes | _ O |
|
eyes | ~ O |
|
eyes | \ O |
|
eyes | / O |
|
nose | ^ |
|
nose | ~ |
|
mouth | \_/ |
|
mouth | /-\ |
|
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 should only use content taught in the course up to (and including) week 2.
- 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.
1511 style print_faces.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest print_faces
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_print_faces print_faces.c
You must run give
before Tuesday 27 February 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) Debugging - Double
Download debug_double.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity debug_double
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 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
Walkthrough
Below is a video walkthrough of this exercise! Make sure to attempt it before watching this video
1511 style debug_double.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest debug_double
Exercise — individual:
(Not For Marks) Debugging - Smallest Prime Factor
Download debug_smallest_prime.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity debug_smallest_prime
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 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
Walkthrough
Below is a video walkthrough of this exercise! Make sure to attempt it before watching this video
1511 style debug_smallest_prime.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest debug_smallest_prime
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 3 Tuesday 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.