Lab #1: CDT, ADT and Polymorphism


Introduction

This lab is split into four parts. The first part concentrates on getting familiarized with the construction of concrete data types (CDT). The second part examines ADTs (abstract data types). And the third part deals with polymorphic code. Finally, the fourth part delves into objects. Both the latter two parts are optional.

Software Setup

  • Make sure you have intalled the CodeBlocks software. You need this version.
  • Set up the environment variable.
  • Download this file, and put it in the directory $(install_directory)/MinGW/bin.

1. Concrete Data Types (CDT)

CDTs are data types that the concrete data representations are transparent. In this problem, we'd like to experience with CDT via one example: the Complex_t data type. Download this program to start with (complex.h, complex.c).

First compile the code we offered you by a simple make at the prompt:

make
you may see a bunch of "make" messages and then an executable a.exe should be generated.

Run the executable a.exe, you should see some messages like this:

#############################################################
@@ TODO: fill in your code.
@@ in file: "complex.c", function: "Complex_subTraced"
@@ at line: 51
#############################################################
Open the file complex.c, it's clear what code you should modify or supply. Feel free to add any new functions to this file and the header file complex.h.

After finishing the task, try to modify the client code in file main.c to test your implementation and to see what are the problems of CDT (as we discussed in the class).

User-level tracing: It's very nice and convenient to be able to trace the code at runtime. The code we offered you has been instrumented with this facility.

To see how the debugger works, you may try to run this command:

a -trace @ -debug 3
to trace your code. Finally, try to run
a -h
to see other informations.

How this debugger works? You may firstly want to study trace.h, trace.c, and then study how these cooperate with user code.

2. Abstract Data Types (ADT)

ADTs are data types that the concrete data representations are opaque. In this problem, we'd like to experience with ADT by rethinking our previous example: the Complex_t data type. Download this program to start with (again complex.h, complex.c).

As last problem, your job is to substitute TODO in source file complex.c. Feel free to add any new functions to this file and the header file complex.h.

What's the difference between CDT and ADT? Can we access data representations directly in ADTs? Write some code to justity your conclusions.

3. Polymorphism

In this problem, your job is to implement the polymorphic data type Tuple_t as we discussed in class. Download this program to start with.

The newly added files are tuple.h and tuple.c. Your job is to filled the code that are absent in file tuple.c. Note that in this problem, if you want to make use of the Complex_t ADT as we did in problem 2. So you should first copy your code from problem 2 here.

We have suppied, in file main.c, some test code. After finishing the problem, write more test cases to test your solution.

Question #1: How can we generalize the Tuple_t ADT to a Ntuple_t ADT? Recall that Ntuple_t is of the form: (x_1, x_2, ..., x_n) and each element x_i (1<=i<=n) may be of different type.