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
cm2inches.c
// Author: andrewt@cse.unsw.edu.au // Sample COMP1911 lab solution // written March 2015 // Convert height from centimetres to inches. #include <stdio.h> #define CM_IN_INCH 2.54 int main (void) { int heightCentimetres; int heightInches; printf("Enter your height in centimetres: "); scanf("%d", &heightCentimetres); heightInches = (heightCentimetres / CM_IN_INCH); printf("Your height in inches is %d\n", heightInches); return 0; }
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
heron.c
// Compute area of a triangle // Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au) // Modified 1/03/2018 by Angela Finlayson (angf@cse.unsw.edu.au) as a lab example for COMP1911 // Note this program doesn't check whether // the three scanf calls successfully read a number. // This is good practice but was not requested in the lab exercise // because this isn't covered until later in the course. // This solutions does not check whether the 3 sides form a valid triangle // We will learn how to do this later in the course. // remember to compile with -lm if you use gcc because // you need to include the maths library to use sqrt #include <stdio.h> #include <math.h> int main(void) { double a, b, c; // side-lengths of the triangle double s; // semi-perimeter double area; printf("Enter side 1: "); scanf("%lf", &a); printf("Enter side 2: "); scanf("%lf", &b); printf("Enter side 3: "); scanf("%lf", &c); // compute semi-perimeter s = (a + b + c) / 2; // compute area using Heron's formula area = sqrt(s * (s - a) * (s - b) * (s - c)); printf("Area = %lf\n", area); return 0; }
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
wam.c
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au) // as a lab example for COMP1511 // Compute student WAM and grade #include <stdio.h> #define HD_CUT_OFF 85 #define DN_CUT_OFF 75 #define CR_CUT_OFF 65 #define PS_CUT_OFF 50 int main (void) { int course1, course2, course3, course4; double average; printf("Enter marks for 4 courses enrolled in past semester:\n"); scanf("%d %d %d %d", &course1, &course2, &course3, &course4); average = (course1 + course2 + course3 + course4) / 4.0; printf("Your WAM is: %.2lf\n", average); if (average >= HD_CUT_OFF) { printf("Your grade is HD\n"); } else if (average >= DN_CUT_OFF) { printf("Your grade is D\n"); } else if (average >= CR_CUT_OFF) { printf("Your grade is CR\n"); } else if (average >= PS_CUT_OFF) { printf("Your grade is PS\n"); } else { printf("Your grade is FL\n"); } return 0; }
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
sort3.c
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au) // as a lab example for COMP1511 #include <stdio.h> int main(void) { int a, b, c; int tmp; printf("Please enter three integers:\n"); scanf("%d %d %d", &a, &b, &c); if (a < b) { tmp = b; b = a; a = tmp; } if (a < c) { tmp = c; c = a; a = tmp; } if (b < c) { tmp = c; c = b; b = tmp; } printf("Your integers after sorting:\n"); printf("%d %d %d\n", a, b, c); return 0; }
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