In this Lab, you will practice:
gcc
system to compile C programs
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
.
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.
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.
It's a good idea to create a new directory for each new Lab.
Create a directory called lab2
by typing
% mkdir lab2Change to this directory by typing
% cd lab2Create heron.c by typing
% gedit heron.c &Copy the code template for a C file, into heron.c
#includeSave your file and quit gedit. You can check that the file is there by typingint main (int argc, char *argv[]) { }
% ls(
ls
is short for "list").
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.
% gcc -Wall -Werror -lm heron.cNote 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
1 2 4Are there sets of numbers which cannot be the sidelengths of a triangle? (Hint: do a Google search on "triangle inequality")
% give cs1917 lab2 heron.c
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 27If you manage to complete it, submit the file
easter.c
(along with heron.c
) by typing
% give cs1917 lab2 heron.c easter.c