COMP1511 17s1 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 accompanying example programs

You need to know the syntax used to read numerical values with scanf and write numerical values with printf. You should also be familiar with the syntax of if statements.

You should also have read the lab assessment guidelines.

Getting Started

One member of your programming pair should login and run the following commands inside a Linux terminal

Create a new directory for this lab called lab02 by typing:

mkdir lab02
Change to this directory by typing:
cd lab02

Exercise 1: Computing the Volume of a Prism

Write a C program prism.c that computes the volume of a rectangular prism.

It should prompt the user to enter the three integers, the side lengths of a rectangular prism.

The program should print out the three numbers entered as well as the volume, surface area and total edge length of the rectangular prism.

Here's what the input and output of your program must look like when it is finished.

Our testing programs will be checking that your output matches exactly (don't panic).

./prism
Please enter prism length: 3
Please enter prism width: 4
Please enter prism height: 5
A prism with sides 3 4 5 has:
Volume      = 60
Area        = 94
Edge length = 48

Hints

Run gedit to create a new file named prism.c, don't forget the &.
gedit prism.c &
You can start by cut-and-pasting the code from a C program from lecture like this sum2c.c.

Use only int variables.

Don't forget to add comments in your source code.

You can assume the user will always enter 3 positive integers.

Remember you can compile your program by typing:

dcc prism.c -o prism
Fix any warnings or errors identified by the compiler.

The COMP1511 class account contains a script that automatically runs your program against some tests.

You can test prism.c like this:

~cs1511/bin/autotest lab02 prism.c
Test prism_1 (1 1 1) - passed
Test prism_2 (9 10 7) - passed
2 tests passed  0 tests failed
Don't be surprised if the autotest script complains about a small difference in output.

You might think small differences don't matter but it is common when dealing with computers to need to produce data in a very precise format. Getting autotests right is good practice for this.

You can repeat just a single test by specifying its label. For example:

~cs1511/bin/autotest lab02 prism_1
What happens when you type the following numbers as side length into your prism program?

./prism
Please enter prism length: 2000
Please enter prism width: 2000
Please enter prism height: 2000
You must be able to explain what happens and why, when your tutor marks your work.

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 triangle using Heron's formula:
   area = sqrt(s(s-a)(s-b)(s-c))
where s is the "semi-perimeter"
   s = (a + b + c)/2.
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

You program should print exactly the error message below if the 3 numbers entered can not be a triangle.

./heron
Enter side 1: 2.0
Enter side 2: 2.0
Enter side 3: 5.0
Error: triangle inequality violated.

Hints

Like the last exercises use gedit to create heron.c.
gedit heron.c &
This program is similar to prism.c so cut-and-paste your code from prism.c to get started.

But note there are some key differences.

You can test heron.c like this:

~cs1511/bin/autotest lab02 heron.c
Test heron_1 (3.0 4.0 5.0) - passed
Test heron_2 (6.0 10.0 8.0) - passed
Test heron_3 (2.5 2.5 1.5) - passed
Test heron_4 (42.0 42.0 42.0) - passed
Test heron_5 (42.0 2.0 39.0) - passed
Test heron_6 (20.0 42.0 20.0) - passed
Test heron_7 (39.0 2.0 42.0) - passed

Exercise 3: Converting Numbers to Words

Write a C program count5.c that converts a small integer to words.

It should prompt the user to enter an integer, reads it from the input and prints out the number in words if it is between 1 and 5, or the messages below otherwise.

Here's what the input and output of your program must look like when it is finished.

dcc -o count5 count5.c
./count5
Please enter an integer :2
You entered two.
./count5
Please enter an integer:5
You entered five.
./count5
Please enter an integer: 0
You entered a number less than one.
./count5
Please enter an integer: 1000
You entered a number greater than five.

Hints

You can write this program using some if statements and simple comparisons.

Use autotest to help test your program:

~cs1511/bin/autotest lab02 count5.c
Test count5_1 (-1) - passed
Test count5_2 (0) - passed
Test count5_3 (1) - passed
Test count5_4 (2) - passed
Test count5_5 (5) - passed
Test count5_6 (6) - passed
Test count5_7 (2000) - passed
7 tests passed  0 tests failed
Also do your own testing!

Challenge Exercise 1: Dating Age Ranges

A popular rule for dating is that you should not date anyone younger than half your age + 7. COMP1511 does not endorse this rule and is not responsible if it leads to you having a bad date.

Write a C program dating_range.c that reads a person's age and calculates the upper and lower age limits of people they should date according to this rule.

Hint: you'll need to use symmetry to calculate the upper bound.

Hint: you only need to use the int type.

For example:

dcc -o dating_range dating_range.c
./dating_range
Enter your age: 18
Your dating range is 16 to 22 years old.
./dating_range
Enter your age: 22
Your dating range is 18 to 30 years old.
./dating_range
Enter your age: 40
Your dating range is 27 to 66 years old.
The above rule implies an empty range for young ages. When the rule produces an empty range program should behave like this:
    ./dating_range
    Enter your age: 12
    You are too young to be dating.
Use autotest to help test your program:
~cs1511/bin/autotest lab02 dating_range.c
Test dating_range_1 (11) - passed
Test dating_range_2 (22) - passed
Test dating_range_3 (44) - passed
3 tests passed  0 tests failed
Also do your own testing!

Challenge Exercise 2: Dating Easter

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 autotest to help test your program:

~cs1511/bin/autotest lab02 easter.c
Test easter_1 (1888) - passed
Test easter_2 (1999) - passed
Test easter_3 (2015) - passed
Test easter_4 (2030) - passed
Test easter_5 (2222) - passed
5 tests passed  0 tests failed

Submission/Assessment

When you are satisfied with your work, ask your tutor to assess it. You also need to submit your work electronically by typing (run this command in the lab02 directory):
give cs1511 lab02 prism.c count5.c heron.c dating_range.c easter.c
Submit the challenge exercises only if you attempt them.

If you are working at home, you may find it more convenient to upload your work via give's web interface.

Remember the lab assessment guidelines - if you don't finish the exercises you can finish them in your own time, submit them by Monday 11:00am using give and ask your tutor to assess them at the start of the following lab.

Either or both members of a programming pair can submit the work (make sure each program lists both of you as authors in the header comment).