COMP 1917 Computing 1
Session 2, 2016

Lab - Week 2

Compiling a C program


Objectives

In this Lab, you will practice:

Introduction

In this first Lab Exercise, the primary aim is for you to familiarise yourself with the process of creating, compiling and executing simple C programs. We'll assume that you've already read Chapter 1 of the textbook. This exercise will simply ensure you are capable of writing a small program and getting it to run. You should keep a copy of the textbook handy, in case you get stuck and the instructions here don't help you work things out.

Before the Lab Class finishes, you should demonstrate your program to the tutor. If the tutor says it's ok, then you should submit it using the give command. Once you've done this, you are free to leave the Lab Class. This means that if you want to prepare your Lab early and show it to the tutor at the start of the class, you can have the rest of the time off. A successful completion will be noted by the tutor in the class roll. The exercise your tutor will be checking will be the one involving heron.c .

Instructions

Your task for this lab is to write a program which inputs the sidelengths 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.
  1. You must first complete the first few steps of the Lab01 exercises, which show you how to create a Unix account, log in, and open a Web browser.

  2. It's a good idea to create a new directory for each new Lab. Create a directory called lab2 by typing

        % mkdir lab2
    
    Change to this directory by typing
        % cd lab2
    
    Create heron.c by typing
        % gedit heron.c &
    
    Copy the code template for a C file, into heron.c
        #include 
    
        int main (int argc, char *argv[]) {
    
        }
    
    Save your file and quit gedit. You can check that the file is there by typing
        % ls
    
    (ls is short for "list").

  3. You are now ready to start editing the file heron.c, using your favourite editor. Note that the basic structure of the program can be retained; you just need to change the comments, variable names and formulas. You can comment out or delete any parts of the program you think are not needed. Keep in mind that all the programs you write in this course should conform to the Computing 1 Style Guide. Some editors, like vim, make the job easier for you by automatically indenting each line to the required number of spaces.

  4. You can compile your program by typing
        % gcc -Wall -Werror -lm heron.c
    
    Note that the gcc command doesn't print anything if the compilation is successful. The -Wall option tells the compiler to warn the user about all potential problems. The -lm option reminds the linker to include the math library. After running gcc, you can check that it has produced an executable by typing ls -l and looking for a newly-created a.out file (check the file time to see if it really is new).

    Fix any errors identified by the compiler. Here's what the input and output of your program might look like when it is finished:

    % ./a.out
    Enter sidelengths of a triangle:
    3 4 5
    Area = 6.00
  5. What happens when you type the following numbers into your program?
    1 2 4
    
    Are there sets of numbers which cannot be the sidelengths of a triangle? (Hint: do a Google search on "triangle inequality")
    Modify your program so that it will print an appropriate error message in these circumstances.

  6. When you are satisfied with your program, ask your tutor to assess your work. Make sure you change the comments (including the header comments) as well as the executable statements in the program. When your tutor has said that your program is acceptable, you are to submit it electronically by typing
        % give cs1917 lab2 heron.c
    
  7. Bonus Challenge: (if time permits)
    Write a program easter.c which allows the user to enter a year, then computes the date of Easter Sunday for that year, using Butcher's Formula. (For this exercise, you are allowed to cut-and-paste the formula from the Web site and then fill in the C program around it; make sure every variable is declared, and every statement ends with a semicolon.) When it is run, the input and output of your program should look like this:
        % ./easter
        Enter Year: 2015
        In 2015, Easter is April 5
        % ./easter
        Enter Year: 2016
        In 2016, Easter is March 27
    
    If you manage to complete it, submit the file easter.c (along with heron.c) by typing
        % give cs1917 lab2 heron.c easter.c