CPS 100E, Fall 1996 Lab 6

Linked Lists, Debugging, and Stocks

(You may find it easier to read this lab using Netscape or another browser, the URL is http://www.cs.duke.edu/~ola/courses/cps100e/lab/lab6.html)

The goals of this lab include doing the specific tasks outlined below and understanding general concepts behind the tasks.


In the last lab, we introduced you to some techniques for maintaining a database of CDs. In this lab, we will use another data structure to speed up the processing time for some queries. Rather than use a database of CDs, our application is a database used by people in the commodities' trading industry so that they know the prices of stocks, indices (Dow Jones and S & P 500, etc.), currencies, commoidites (oil, gas, gold, etc.), futures, and options.

The program usestock.cc will allow you to list all stocks, add and delete stocks, process trades made (e.g., for one day of trading), and find the most active stock.

Lab 6 table of contents

[ Class Structure | The Program | Debugging: Using ddd | Summary of ddd Commands | Modifications To Program | Submit ]

For this lab you'll first write member functions to search a database of stock quotes. The program compiles, but it does not run successfully. Your job is to first get the program to run correctly. From here you will write member functions to write an iterator for the database, and some functions that use the iterator to implement methods like searching for a stock, finding the most active stock, etc.

The structure of the program is similar to the usedb.cc program from a previous lab. The file usestock.cc stores the client program that manipulates stock databases. The class StockList implements a stock database (by storing linked lists of Stocks in a vector).

Class Structure

The class StockList stores a collection of stocks in a vector of linked lists. Each cell in the vector points to a linked list of stocks whose symbols begin with the same name, e.g., CPQ, CSG, and CVC. Stock information is stored in an instance of the class Stock, and a node of the linked list is represented by the struct StockNode. This is diagrammed below.

*

Compiling/Running usestock

In this section of the lab you'll copy files and then compile and run the stock database program.

First change into your cps100e subdirectory (type pwd to verify where you are). Create a lab6 subdirectory by typing mkdir lab6 and change into this subdirectory (be sure to check that you're in the lab6 subdirectory.) Now copy the files for the lab (don't forget the . when copying).

cp ~ola/cps100e/lab6/* .

You should see the files listed below (these are links to the files in case you use Netscape, and for users outside of Duke). This will copy two data files the program reads whose names are hard-wired into the program.

Be sure you're in the lab6 subdirectory, and check to see that all files are there (type ls). Then, from an xterm window (at the prompt [1] ola@teer8% or similar) compile the first version of the program by typing: make usestock.

Note that you type make usestock since the function main is in the usestock.cc file.

This should compile several files and link them together with the library libtapestry.a. Now run the program by typing: usestock at the prompt. When you run the program, you should get an error saying "segmentation fault". Your first job is to find the place in the code that causes the segmentation fault by using ddd (a debugging tool that we want you to begin to use.)

back to lab contents


Debugging: Using ddd:

In this part of the lab you'll learn how to use ddd to find the reason that usestock crashes. You will fix the bug and check to make sure that the program runs correctly.

To start the debugger, type

ddd usestock & from an xterm window. Two windows will pop up on your machine. One will be the command window, and the other window will show the source code usestock.cc. The windows may take a long time to pop up, so be patient.

You should run the program: either type run inside the command window, or click on the run button (located near the bottom lefthand corner of the source code window.) ddd will run the program and stop when the segmentation fault occurs. There will be an arrow (kind of greenish in color) on the left side of the source code window pointing at the line number that is causing the segementation fault. This information, the name of the file, and the line number is printed in the DDD command window (the command window is running the gdb debugger, ddd is a graphical front end to gdb. You can run gdb without the GUI front end if you're on a computer without the ability to display X windows.)

Displaying items with ddd

To start finding the error, first click on current in the source code window so that it is highlighted. Then click the Display() button. This should pop up the Data Window with a graphical picture of current. Note that the the value of current is shown as 0x0 which is NULL. Thus trying to dereference to get current->next generates a segmentation fault. To trace execution and find the error follow the steps below. Even if you already know what the problem is you should follow these steps to get practice with the debugger.

  • Place a break point on line 184 (the while loop). Click near the line number so that the line number is diplayed at the bottom left of the source code window, click on Break(), and a stop sign should appear to the left of 184.

  • Click the Run button again. A popup window will appear saying "The program being debugged has been started already. Start it from the beginning?". Click on Yes. The program will run until it reaches the line of code where the break point was set.

  • Note that the value of current in the display window is different. Click on current so that it is highlighted, then click on Dereference() in the dsiplaywindow. This shows a diagram of what *current is (the node current points to.)

  • Click on stock = {...} in the *current diagram. Then click on Show() to show the stock. You can drag this larger box around with the mouse button. Note that mySymbol is shown as hidden. Click on mySymbol = {...} so that it is highlighted, then click on Show(). This should show you the symbol for the stock. Write the symbol down.

  • Now click on the Next button in the source code window. This executes the program one step at a time. Press Next but stop before executing the statement at line 181 which causes the segmentation fault.

  • You will now display the value of newNode. Highlight newNode, click on Display(), then use Dereference() and Show() to find the symbol that should be added to the list of stokcs. Write the symbol down.

Fixing the Code

The problem is that to add a new node in order, the code needs to look one node ahead. This is shown in the Tapestry book on page 569 in the function WordList::Update(). Rather than use the loop test below while (current && current->stock.GetSymbolsymbol() <= symbol) you'll need to change to look one node ahead (see the book).

You'll need to modify the code after the loop to link newNode into the list (this will require two statements!). You should start up emacs, make the changes that are necessary (think about this!!, don't just type at random), then recompile. You can reload the source code into ddd from the Source menu. If you run the program from within ddd the newly compiled code will be loaded and run. You may need several iterations of edit, compile, debug to get the program working. You can either step through with the debugger or you can remove the break point (use Clear() to remove a break point) and try the print option when the menu of choices appears in the ddd command window. There are LOTS of stocks, but the last stock should have symbol ZRN. You can also use the option of printing stocks starting with a give letter.

back to lab contents


Summary of DDD buttons/commands

Useful Buttons in Source Code Window

  • Break: Set a break point at current line
  • Clear: Clear a break point at current line.
  • Print: Print the value of current variable.
  • Find << Search backwards for current text.
  • Find >> Search forwards for current text.
  • Run: Start Program from beginning until it ends or hits the first break point.
  • Step: Step into function. Run the next line of code. If the code is a function call go to first statement in the function call
  • Next: Go to Next line. Similar to Step, but if the line is a function call, Next will run the entire function and stop at the next line of code in the current function to be excuted.
  • Continue: Continue running the program from current spot until it hits the next break point or ends.

For more information, you can click on the Help option in the menu and look it up in the ddd manual.

Frequently Used Buttons in Data Window

  • Dereference: See where a pointer points to
  • Show/Hide: Show the value of the variable/object or hide it. Toggles the display ability.
  • Delete: Delete the object that is being displayed.

To find out more, click on the Help button on the menu bar and consult the DDD manual.

back to lab contents


Modifications to the Program:

Now that we have the program basics working (i.e. we can read in a data file, and we can print a stock, and add trades.), you will use these functions to implement a new member function called GetMostActiveStock which prints out the stock in the database that has been traded the most. You will also modify the SetCurrent function to take advantage of the fact that the linked lists that contain the stocks are sorted in alphabetical order. Finally, You will implement the Delete member function.

GetMostActiveStock

The GetMostActiveStock method should print out the stock that was traded the most (by volume). The volume of shares for each stock is accessible using the GetVolume function of the class Stock. You will need to add this method to stlist.h file, implement it in the stlist.cc file and call it from usestock.cc You can use the iterator functions to get at all the stocks in the database. You might want to test the function both before and after processing trades.

SetCurrent

Modify the SetCurrent member function to take advantage of the fact that the stocks are in alphabetical order. Currently, it walks down the linked list until it reaches the end to find out that the stock is not in the database. You can stop sooner, i.e., as soon as a stock is larger than the one being searched for. This will save some time.

Delete

You will now implement the DoDelete function in usestock.cc and the Delete() member function in the StockList class.

To Delete a record from the Database, you will need to

  • Prompt the user for the symbol of the stock that is to be deleted (Remember to use getline.)

  • In the Stock::Delete() member function you'll need to find where the stock to be deleted is (the index) and then unlink it from the linked list. Make sure that you do not lose any of the other elements in the linked list. This is tricky, you'll probably want to use the print-stocks-with-a-given-letter option.

back to lab contents


Submitting The Lab

To submit assignments you'll run the command below, but substitute your section number (1, 2, or 3) for N.
    submit100e lab6.N README usestock.cc stlist.h stlist.cc

You can enter the files in any order.

Remember that every assignment must have a README file submitted with it (please use all capital letters). Include your name, the date, and an estimate of how long you worked on the assignment in the README file. You must also include a list of names of all those people with whom you collaborated on the assignment.

You also must turn in the inlab questions either by turning in the sheet during lab or by submitting the answers with your README.