DCO20105  Data Structures and Algorithms

Semester A, 2002-3

 

Lab 4 -- C++ programming on class

Objectives:

 

Pre-Class activities

Laboratory Exercise

  1. (5 minutes) Q & A

  2. (10 minutes) Hand in the hard copy and demonstrate the program assignment from last lab: listSimple.cp.  
    1. Hand in a hard copy of your program. Write your name and group number in case this information is not provided in your hard copy.
    2. Execute the program once and show the result screen to your tutor.

  3. (10 minutes) Modify the method cons() in listSimple.cp (shown below) and make the second argument/parameter have a default value of null. 

    Node *cons( int const& x, Node const *const& y)
    {
       Node *new_Node = new Node;

       new_Node->item = x;
       new_Node->next = const_cast<Node*> (y);

       return new_Node;
    }


    Then change the first declaration statement in main():

       Node *a(cons(37, cons(52, cons(96, 0))));

    to

       Node *a(cons(37, cons(52, cons(96)))); 

    Compile and execute the program.

  4. (15 minutes) Modify the program set list.h, list.cc, and list-test.cp to allow the program to overload the operator << for output rather than using the method print().

  5. Start to work on the class Roster in the Practice Problems in CTE Exercise 3, finish it at home, and demonstrate it next lab.  For a sample of execution, download from here.  To run the sample, make sure names.dat (available from CTE) is there and run it as: Roster-test < names.dat.

Marking

2 for listSimple.cp -- no marks given when wrong/unexpected output for more than two executions.
1 for adding default arguments.
1 for overloading the operator <<.
Marks are awarded according to the completeness and quality of a program/answer.
0 -- absent (without extenuating circumstance) or no participation at all.

Supplementary Exercise

The following exercises do not affect your marks but are for the sake of more practice (make perfect),  using useful utilities, and/or challenge.

  1. Read Multiple files and separate compilation, have the program set list* ready and try different ways to compile the program set list*.

  2. Follow "Using gdb" in Practice Problems of Exercise 3.  It will lead you to get familiar with the debugger gdb and it will be useful to assist your future debugging.

  3. Write a program as specified in I/O Formatting in Practice Problems of Exercise 3.


Last modified: September 20, 2002