In this Lab, you will practise:
You need to know the syntax used to read numerical values with scanf
and
write numerical values withprintf
, as well as under C conditions.
You should also have read the lab assessment guide.
Change into your 1911 directory. If you are inside your home directory you would do this by using the following command:
$ cd 1911
Next, create a new directory for this lab called lab02
by typing:
$ mkdir lab02Change to this directory by typing:
$ cd lab02
cm2inches.c
which converts a height
in centimetres to inches.
In the tutorial you discussed a program cm2feet.c
to convert a height in centimetres to feet - use this as a starting point.
Copy cm2feet.c
from the class account into the current directory:
$ cp ~cs1911/public_html/tlb/02/cm2feet.c cm2feet.cConfirm
cm2feet.c
works by compiling and running it:
$ dcc -o cm2feet cm2feet.c ./cm2feet Enter your height in centimetres: 183 Your height in feet is 6Now use cm2feet.c as starting point for the program you have to write by copying it to
cm2inches.c
$ cp cm2feet.c cm2inches.cNow use
gedit
to make the changes needed to cm2inches.c
Use only int variables - don't use double for this exercise.
Then compile and test cm2inches.c
.
$ dcc -o cm2inches cm2inches.c ./cm2inches Enter your height in centimetres: 183 Your height in inches is 72
dcc
isn't available.
its best to run gcc with these options.
$ gcc -Wall -Werror -O -o cm2inches cm2inches.c
$ gedit cm2inches.c &Also use autotest to test your program:
1911 autotest lab02 cm2inches.c
heron.c
which reads 3 doubles, the side lengths
a, b and c of a triangle, and computes the area of the tria
ngle using Heron's formula:
area = sqrt(s * (s - a) * (s - b) * (s - c))where s is the "semi-perimeter"
s = (a + b + c) / 2.0
You can assume the user will enter 3 positive decimals.
Don't forget to add comments in your source code.
You can compile your program by typing:
$ dcc heron.c -o heron
Fix any warnings or errors identified by the compiler. Here's what the input and output of your program must look like when it is finished.
$ ./heron Enter side 1: 0.6 Enter side 2: 0.8 Enter side 3: 1.0 Area = 0.240000 $ ./heron Enter side 1: 5.31 Enter side 2: 4.2 Enter side 3: 7.77 Area = 10.542172 $ ./heron Enter side 1: 12.5 Enter side 2: 12.5 Enter side 3: 0.5 Area = 3.124375Not all 3 numbers can be the sides of a triangle (its called the triangle inequality), for example: these 3 numbers
2.0 2.0 5.0
can't be the side of a triangle
For this week's lab, you can assume that we will only test your program will valid triangles.
scanf
format ("%lf"
)
and the same for printf
.
sqrt
function from the maths library so you
need to add an extra line:
#include <math.h>after:
#include <stdio.h>
The COMP1911 class acount contains a script that automatically runs your program against some tests.
You can test heron.c
like this:
1911 autotest lab02 heron.cYou can also run just a single test by specifiying its number. For example:
1911 autotest lab02 2
Copy the program wam.c
from the subject account to this directory by typing:
$ cp ~cs1911/public_html/tlb/02/wam.c .
Try to compile the program by typing:
$ dcc -o wam wam.c
This program is a mess! It is full of errors, both syntactic (form) and semantic (meaning).
The program should require the user to enter 4 marks as integers and SHOULD behaves as follows:
$ dcc -o wam wam.c $ ./wam Enter marks for 4 courses enrolled in past semester: 97 97 100 100 Your WAM is: 98.50 Your grade is HD $ ./wam Enter marks for 4 courses enrolled in past semester: 0 0 5 10 Your WAM is: 3.75 Your grade is FL
First try to fix the errors the compiler reports, these are usually due to syntax. Then move on to fix the warnings, often due to semantic problems. You should be able to get your program to a point where it compiles with no errors or warnings.
Try and run the program. If it does not behave as expected, find out what the problem is and fix it.
Replace all "magic numbers" in the program using #define constants with meaningful names and fix any other style issues in the program
For those new to UNSW, the grading structure is as follows: HD for WAM>= 85, D for WAM>=75, CR for WAM>=65, PS for WAM>=50 and FL for WAM < 50
.
When you think you have wam.c
working use autotest to test it further:
$ 1911 autotest lab02 wam.c
sort3.c
that reads 3 integers and prints them in
from largest to smallest.
Your program should behave exactly like this example:
$ ./sort3 Please enter three integers: 23 5 27 Your integers after sorting: 27 23 5 $ ./sort3 Please enter three integers: 3 9 27 Your integers after sorting: 27 9 3 $ ./sort3 Please enter three integers: 9 8 7 Your integers after sorting: 9 8 7When you think you have
sort3.c
working use autotest to test it further:
$ 1911 autotest lab02 sort3.c
Note: This may be too challenging for many students in the class at this stage.
So don't be discouraged if you can't get it working.
You can still easily get an B (1.0/1.25) with the other 4 exercises.
Write a program easter.c
which allows the user to enter a year,
then calculates the date of Easter Sunday for that year.
Use the formula developed in 1876 by
Samuel Butcher, Bishop of Meath,.
Follow the output format in in the example below exactly:
$ dcc easter.c -o easter $ ./easter Enter Year: 2017 Easter is April 16 in 2017. $ ./easter Enter Year: 2018 Easter is April 1 in 2018. $ ./easter Enter Year: 2019 Easter is April 21 in 2019.
Make sure every variable is declared.
Make sure every statement ends with a semicolon.
As usual use your autotest to test your program:
$ 1911 autotest lab02 easter.c
lab02
directory):
give cs1911 lab02 heron.c cm2inches.c wam.c sort3.c easter.cSubmit advanced exercises only if you attempt the advanced exercise.
Remember the lab assessment guidelines - if you don't finish the exercises
you can finish them in your own time, submit them
by 19:59:59 Sunday using give
and ask ask tutor to assess them at the start of
the following lab.
You can also just run the autotests without submitting by typing
1911 autotest lab02