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.
Sample solution for prism.c
// Compute volume of a prism
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// 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.

#include <stdio.h>

int main(void) {
    int width, height, length;
    int area, volume, edgeLength;

    printf("Please enter prism length: ");
    scanf("%d", &length);
    printf("Please enter prism width: ");
    scanf("%d", &width);
    printf("Please enter prism height: ");
    scanf("%d", &height);

    volume = length * width * height;
    area = 2 * (length * width + length * height + width * height);
    edgeLength = 4 * (length + width + height);

    printf("A prism with sides %d %d %d has:\n", length, width, height);
    printf("Volume      = %d\n", volume);
    printf("Area        = %d\n", area);
    printf("Edge length = %d\n", edgeLength);

    return 0;
}

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
Sample solution for heron.c
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// Compute area of a triangle

// 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.

// 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;

    // check the triangle inequality
    if (a > b + c) {
        printf( "Error: triangle inequality violated.\n" );
    } else if (b > a + c) {
        printf( "Error: triangle inequality violated.\n" );
    } else if (c > a + b) {
        printf( "Error: triangle inequality violated.\n" );
    } else {
        // compute area using Heron's formula
        area = sqrt(s * (s - a) * (s - b) * (s - c));
        printf( "Area = %lf\n", area );
    }

    return 0;
}

Alternative solution for heron.c using the && operator to simplify the if statement.
// Compute area of a triangle
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// 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.

// 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;

    // check the triangle inequality
    if (a > b + c || b > a + c || c > a + b) {
        printf( "Error: triangle inequality violated.\n" );
    } else {
        // compute area using Heron's formula
        area = sqrt(s * (s - a) * (s - b) * (s - c));
        printf( "Area = %lf\n", area );
    }

    return 0;
}

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!
Sample solution for count5.c
// Convert a small integer to words
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// Note this program checks whether scanf 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.

#include <stdio.h>

int main(void) {
    int number, n_numbers_read;

    printf("Please enter an integer: ");
    n_numbers_read = scanf("%d", &number);

    printf("You entered ");

    if (n_numbers_read < 1) {
        printf("not an integer.\n");
    } else if (number < 1) {
        printf("a number less than one.\n");
    } else if (number == 1) {
        printf("one.\n");
    } else if (number == 2) {
        printf("two.\n");
    } else if (number == 3) {
        printf("three.\n");
    } else if (number == 4) {
        printf("four.\n");
    } else if (number == 5) {
        printf("five.\n");
    } else {
        printf("a number greater than five.\n");
    }

    return 0;
}

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!
Sample solution for dating_range.c
// Compute area of a triangle
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// 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.

#include <stdio.h>

int main(void) {
    int age;
    int lower_limit;
    int upper_limit;

    printf("Enter your age: ");
    scanf("%d", &age);

    lower_limit = age / 2 + 7;
    upper_limit = (age - 7) * 2;

    if (upper_limit < lower_limit) {
        printf("You are too young to be dating.\n");
    } else {
        printf("Your dating range is %d to %d years old.\n", lower_limit, upper_limit);
    }

    return 0;
}

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
Sample solution for easter.c
// Compute Easter Sunday date
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

//  code for Butcher's Formula copied from
//  http://smart.net/~mmontes/nature1876.html

// Note this program doesn't check whether
// the scanf call 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.

#include <stdio.h>

int main(void) {
    int a, b, c, d, e, f, g, h, i, k, l, m, p;
    int year, month, date;

    printf("Enter year: ");
    scanf("%d", &year);

    a = year % 19;
    b = year / 100;
    c = year % 100;
    d = b / 4;
    e = b % 4;
    f = (b + 8) / 25;
    g = (b - f + 1) / 3;
    h = (19 * a + b - d - g + 15) % 30;
    i = c / 4;
    k = c % 4;
    l = (32 + 2 * e + 2 * i - h - k) % 7;
    m = (a + 11 * h + 22 * l) / 451;
    month = (h + l - 7 * m + 114) / 31;  //  [3=March, 4=April]
    p = (h + l - 7 * m + 114) % 31;
    date = p + 1;  // (date in Easter Month)

    printf("Easter is ");

    if (month == 3) {
        printf("March");
    }
    else {
        printf("April");
    }

    printf(" %d in %d.\n", date, year);
    return 0;
}

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).