COMP1911 23T2 Introduction to Programming

Objectives

In this Lab, you will practise:

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples

Getting Started

Login and run following commands inside a Unix terminal

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

mkdir labB2
Change to this directory by typing:
cd labB2

Autotests and Freeing Memory

As you know, every time we use malloc, we need to use a corresponding free when we are finished with the memory.
If we don't we will use more and more memory as our program runs.
It is given back to the system as soon as the program terminates, so for our short toy programs it has not been an issue.

However, users do not want memory leaks in their ADTs.
So in this lab and the next lab we want to make sure you correctly implement your destroy functions.
And use them correctly in your client program so we know that you can manage your memory accurately.

In the labs and the exam we will be testing your programs with the additional --leak-check option for dcc. For example:

dcc --leak-check prog.c -o prog
So your program will need to generate the correct output AND free all malloced memory to pass the autotests.
Keep this in mind when you are testing your ADTs in this lab.

Getting Provided Files

To get all of the provided files for this lab you can run the following command:

cp /web/cs1911/current/tlb/B2/code/*.[ch] .

How to Compile

For multi-file projects you must give all required C files to the compiler at the same time.
You don't need to include H files, but they must be in the current directory.

$ dcc ADT-file.c main-file.c -o executable
$ ./executable

Exercise 1: Recursion

Create a file hofstadter.c which implements the Hofstadter Male + Female sequences. The pair of sequences defined by

Female(0) = 1
Male(0)   = 0
and
Female(n) = n - Male(Female(n - 1))
Male(n)   = n - Female(Male(n - 1))
$ dcc hofstadter.c -o hofstadter
$ ./hofstadter 1
1
$ ./hofstadter 7
5
$ ./hofstadter 33
21
$ ./hofstadter 52
32
$ ./hofstadter 101
63
$ ./hofstadter 177
110

Exercise 2: Array Recursion

Create a file array_count_positive.c which uses a recursive function to count the number of positive integers in an array of integers.

$ dcc array_count_positive.c -o array_count_positive
$ ./array_count_positive 1 2 3 4 5
5
$ ./array_count_positive 1 2 3 4 5 0 -1 -2 -3
5
$ ./array_count_positive 1 2 3 4 5 0 -1 -2 -3 6 7 8 9
9

Exercise 3: Doubly Linked Lists & List Recursion

The file list.c and the interface list.h is the starting point for this lab exercise.

It contains two structs, List and Node,
which can be used to represent a sequence of strings.

It should, when you have finished your implementation, uses a doubly-linked-list as its data representation.
It is one of the same structs you have seen in lectures.

You will remember that each element in the list is represented by a Node struct.

You have to implement these functions:

Each function in list.c has a comment describing what it should do.
And any limitations on how it should be implemented.

many of these functions can be copied from the lectures with only minor changes.

Testing Your Functions

The main function in runList.c contains code which allows you to test your functions.

Note: you do not need to understand how this code works.

The examples below demonstrate how to use the testing code.

These examples also indicate how your functions should behave.

$ dcc --leak-check list.c runList.c -o list
$ ./list
list (0) = NULL
> insert_last Hello World
list (1) = "Hello World" -> NULL
> insert_last How are you
list (2) = "Hello World" -> "How are you" -> NULL
> insert_first COMP1911
list (3) = "COMP1911" -> "Hello World" -> "How are you" -> NULL
> insert 1 23T2
list (4) = "COMP1911" -> "23T2" -> "Hello World" -> "How are you" -> NULL
> delete 2
list (3) = "COMP1911" -> "23T2" -> "How are you" -> NULL
> reverse
list (3) = "How are you" -> "23T2" -> "COMP1911" -> NULL
> get 1
< 23T2
list (3) = "How are you" -> "23T2" -> "COMP1911" -> NULL
> quit

Submission/Assessment

There are no autotests for this lab. When you are satisfied with your work, submit it electronically by typing (run this command in your labB2 directory):
give cs1911 labB2 hofstadter.c array_count_positive.c list.c