CS260 Assignment 3
Instructions
You have decided to write a C++ program using data abstraction to help you keep track of the stock market. You want a program that will quickly allow your users to find out very quickly about a stock or mutual fund.
The information about each stock or mutual fund that you want to keep track of is:
- ticker symbol (e.g., IBM for International Business Machines)
- stock or fund name (e.g. International Business Machines)
- share price (e.g. 25.73)
(this should be stored as an int, not as a float or a double) - Date of that share price (e.g. May, 23, 1967)
You will implement this as a hash table. You will use linear probing for handling collisions. This means that if you have a collision when inserting a new item, you will search the table in order of increasing indices until you find the first empty slot and insert the new item there. You will treat the table as a circular array, so if your search takes you “off the top” of the table, you will continue looking for an empty slot at location zero.
An attempt to insert a stock into the hash table can fail for one of two reasons:
- the stock is already in the hash table
- the hash table is full (i.e. no more empty slots)
Your code will need to handle this condition by indicating failure when requested to insert a stock which is already there, or for which there is not room.
It is required that the functions HashMap::get(), HashMap::put(), and HashMap::remove() use hashing and linear probing. Do not just do a linear search in the array. Doing so will result in a severe loss of points.
The function HashMap::hashStr() should be coded to be as fast as possible and to not do unnecessary work as it computes the hash value for a string. Getting this right is non-obvious, and will be an important factor in your grade for this assignment.
The share price is required to be stored as an int (representing the number of cents, not dollars). You should not use any floating point techniques for either storing or printing out the share price.
Your code needs to produce output that’s identical to the contents of asgmt03.correctOutput.txt (except for minor differences in whitespace). You will need to use I/O manipulators (defined in <iomanip>) in order to format the printed output properly.
In the printed output, “hc” is the label for what your function returned as symbolHash, “hi” is the label for hashIndex, “ui” is the label for usedIndex, and “sl” is the label for the linear probe sequence length. The driver code takes care of printing all of this. Your code just has to return the appropriate data.
Code To Start With
The code for you to start with is in ~michael.trigoboff/cs260/asgmts/asgmt03.tar.gz. This kind of file is called a tarball, and is the Linux equivalent of a zip file. You should copy this file into a directory of your own and then expand it using ~michael.trigoboff/bin/untarz.
Add code to the definition and implementation files as needed. You can add public and private members, but do not remove any of the public members that are already there. Also, make no changes to asgmt03.cpp.
There may be additional instructions in comments in the code files, which may contradict these instructions. In that case, do what the instructions in the code say you should do.
Required Submission File Structure
You are required to submit your work as a Linux tarball. No other file format is acceptable. Use ~michael.trigoboff/bin/tarz to create your tarball.
Do not change the structure of your project directory in any way. Do not rename files, add new files, or delete files. I grade your code using a Bash script that depends on files being in predictable places. Nothing in this assignment requires files to be moved, renamed, deleted, etc.
Strings
For storing and printing strings, you should use C strings. Your code should #include <cstring> and use the functions defined in that header, such as:
- int strlen(char *str)
// given a string pointer, returns the length of that string,
// which does not include the terminating ‘\0’ character - void strcpy(char *dst, char *src)
// copies the characters in src to dst,
// including the terminating ‘\0’ character
Printed Output
Your code’s printed output needs to be identical to the contents of the “correct output” file(s) provided as part of this assignment. “Identical” means the Linux utility diff will find no differences between your code’s output and the provided file, when called with flags: -w -B. This means that the sequence of non-whitespace characters in your output has to be identical, but that differences in whitespace will be ignored.
You are still required to produce output that lines up with itself properly. Columns may need to line up, numbers may need to be right-justified, etc. You should look at the “correct output” file(s) to see what you need to do.
Memory Leaks And Other Errors
Your code is required to run without memory leaks. You should use ~michael.trigoboff/bin/mchk to check this. In addition to memory leaks, mchk will also report other errors. You are required to fix those errors as well.
Initialization Lists
The constructors in your code should use an initialization list item for each data member defined by the class. Points will be deducted where this is not done.
Here is a class definition:
class SomeClass { ... private: int dataMember1; int dataMember2; }
Here is a constructor for this class that does not use an initialization list:
SomeClass::SomeClass(int dataMember1, int dataMember2) { this->dataMember1 = dataMember1; this->dataMember2 = dataMember2; }
And here is one that does use an initialization list:
SomeClass::SomeClass(int dataMember1, int dataMember2) : dataMember1{dataMember1}, dataMember2{dataMember2} { }
An initialization list consists of comma-separated items between the : and the { that begins the body of the function. Each of these items is of the form:
dataMemberName{value}
Each item sets the named data member to the value in the parentheses. In the SomeClass constructor above, each of the two initialization list items sets its data member to the constructor argument named inside the parentheses. This is weird-looking syntax, but it is standard usage in the culture of C++.
Grading Your Code
Your code needs to compile without errors or warnings and run correctly. Code that does not compile will receive zero points. Code that crashes will receive zero points.
I use Bash scripts to grade your code. Because of this, it is very important that you not make any changes to file names or the folder structure in your project. Your project should have all the same names in all the same places as the starter project you copied from ~michael.trigoboff/cs260/asgmts/asgmt03.tar.gz. If you change any of this, my scripts will crash. Your work will not be graded, you will have to resubmit a corrected version of your work, and you will lose points.
My scripts produce a log file containing your code, compiler warnings and errors (if any), your code’s output, and some statistics that are useful to me. I then personally go through that log file to produce your grade. In other words, the script doesn’t decide your grade, it just makes it more convenient for me to decide your grade.
To Submit This Assignment
Remove anything like calls to cin.get(), that would stop your code.
“Clean” your project by doing the following:
make x
After cleaning your project, compress it into a tarball named asgmt03.tar.gz using ~michael.trigoboff/bin/tarz. This tarball should expand into a directory named asgmt03. Submit the tarball containing your cleaned project to Desire2Learn.
Please be sure the source file prints your name, assignment description and number, as requested.
You may upload as many versions as you wish prior to the due date. I will only see and grade the last one. You won’t be able to upload assignments after the due date.