COMP1911 23T2 Introduction to Programming

Objectives

In this Lab, you will practise:

Preparation

Before the lab you should re-read the lecture slides on C Basics and the C conditions (also covered in capters 1-2 of Moffat).

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.

Getting Started

Login and run following commands inside a Unix terminal

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 lab02
Change to this directory by typing:
$ cd lab02

Exercise 1: Converting a Height in Centimetres to Inches

You need to write a program 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.c
Confirm 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 6
Now use cm2feet.c as starting point for the program you have to write by copying it to cm2inches.c
$ cp cm2feet.c cm2inches.c
Now 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

Hints

If you are working away from CSE where 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

Exercise 2: Area of a Triangle

Write a program 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.124375
Not 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.

Hints

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.c
You can also run just a single test by specifiying its number. For example:

1911 autotest lab02 2

Exercise 3: Debugging wam

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

Exercise 4: Printing Integers Largest to Smallest

Write a C program 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 7
When you think you have sort3.c working use autotest to test it further:
$ 1911 autotest lab02 sort3.c

(Optional) Challenge Exercise: Dating Easter

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.

Hints

Cut-and-paste the formula from the above Web site and then fill in the C program around it.

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

Submission/Assessment

When you are satisfied with your work, ask your tutor to assess it. You are to submit it electronically by typing (run this command in your lab02 directory):
give cs1911 lab02 heron.c cm2inches.c wam.c sort3.c easter.c
Submit 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