Week 04 Laboratory Exercises
Objectives
- using while loops for repetition
- creating functions
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:
- devowel
- middle3
Worth 0.7 mark(s) in total:
- hollow_triangle
- souffle_pancake
Worth 0.4 mark(s) in total:
- cs_calculator
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
(●◌◌)
:
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!
1091 style devowel.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest devowel
When you are finished working on this exercise,
you must
submit your work by running give
:
give dp1091 lab04_devowel devowel.c
You must run give
before Monday 17 February 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 the Middle Integer of 3 Integers
Download middle3.c here
Or, copy these file(s) to your CSE account using the following command:
1091 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.
1091 style middle3.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest middle3
When you are finished working on this exercise,
you must
submit your work by running give
:
give dp1091 lab04_middle3 middle3.c
You must run give
before Monday 17 February 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
(●●◌)
:
Draw a Triangle
Write a program called hollow_triangle.c
that reads an integer n
from
standard input. and prints a pattern of asterisks forming a hollow triangle.
You can assume n
is greater than 3.
Make your program match the examples below exactly.
Note: you are not permitted to use an array in this exercise.
Examples
dcc hollow_triangle.c -o hollow_triangle ./hollow_triangle Enter size: 4 * ** * * **** ./hollow_triangle Enter size: 5 * ** * * * * ***** ./hollow_triangle Enter size: 8 * ** * * * * * * * * * * ******** ./hollow_triangle Enter size: 11 * ** * * * * * * * * * * * * * * * * ***********
1091 style hollow_triangle.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest hollow_triangle
When you are finished working on this exercise,
you must
submit your work by running give
:
give dp1091 lab04_hollow_triangle hollow_triangle.c
You must run give
before Monday 17 February 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
(●●◌)
:
Souffle Pancakes
Download souffle_pancake.c here
Or, copy these file(s) to your CSE account using the following command:
1091 fetch-activity souffle_pancake
Complete the C program, souffle_pancake.c
.
The main function has already been written, which help the user calculate how many resources they will need to cook any given amount of souffle pancakes!
Unfortunately due to a strange bug in the DCC Compiler, the math.h library has been acting up, so Paula has asked you to help her out and write some useful functions similar to those in that library.
Your job is to write 3 functions that are currently being used in
the main function. They are: divide
, ceiling
and a helper procedure print_time
.
Functions to Implement
divide
is a function that divides two doubles.
- function input:
- a
double
for the top of the divide - a
double
for the bottom of the divide
- a
- function output:
double
for the result
ceiling
is a function that rounds up a given double to the closest integer.
If the value is already a whole number, it stays the same value.
- function input:
- a
double
for the number being ceiling-ed
- a
- function output:
int
for the result
print_time
is a procedure that given minutes, will print out the time in
hours and minutes
- function input:
- an
int
for the cooking time in minutes
- an
- function output:
void
- It will print the time in the format
"X hours and X minutes"
, even if either the minutes or hours is 0.
Examples
dcc souffle_pancake.c -o souffle_pancake ./souffle_pancake How many souffle pancakes do you want to make? 3 You will need: - 2 eggs - 30g sugar - 24g flour Frying will take you 0 hours and 15 minutes. ./souffle_pancake How many souffle pancakes do you want to make? 5 You will need: - 4 eggs - 50g sugar - 40g flour Frying will take you 0 hours and 30 minutes.
1091 style souffle_pancake.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest souffle_pancake
When you are finished working on this exercise,
you must
submit your work by running give
:
give dp1091 lab04_souffle_pancake souffle_pancake.c
You must run give
before Monday 17 February 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
(●●●)
:
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.
1091 style cs_calculator.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest cs_calculator
When you are finished working on this exercise,
you must
submit your work by running give
:
give dp1091 lab04_cs_calculator cs_calculator.c
You must run give
before Monday 17 February 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 - Letter Search
Download debug_letter_search.c here
Or, copy these file(s) to your CSE account using the following command:
1091 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. - DPST1091 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 :(
1091 style debug_letter_search.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 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:
1091 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. - DPST1091 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
1091 style debug_cloud.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest debug_cloud
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 5 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