CS 112B1 -- Homework 2

Song Rating Program

Due Friday, February 17 @ 10pm


Written Assignment (Optional)

The following are "pencil and paper" problems that need not be compiled and tested. They should follow the data declarations and conventions from the textbook. You should assume that all linked lists have a head pointer, as discussed in the text.

  1. Binary search does not really correspond to the way humans search through sorted data (such as phone books); instead of looking in the middle of the list, we estimate the location the item we are looking for, and search there, and then repeat. One could imagine using this method to search a sorted array, by examining the beginning and ending of the list, and estimating how far through the list your item is (i.e., if the list begins with 1 and ends with 100, you might guess that the number 25 occurs about a quarter of the way through the list, not in the middle). Write a modified version of Program 2.2, called dictionarySearch, that implements this idea on sorted arrays of integers. Then answer the following question: How would you characterize lists for which this method will perform better than binary search?
  2. Write a method zip that accepts two linked lists of integers (with header nodes) and interleaves the two lists, e.g., if you give it the two lists 4, 6, 7, 2 and 2, 8, 5, 1 it returns the list 4,2,6,8,7,5,2,1. Your method should work on lists of any length (including empty lists).

Please gsubmit your answers in a text file called answers.02.txt.


Programming Assignment

You are hired by the web master of a local music store to implement the back end to a web page that will be used as apart of an ad campaign to advertise the store and its services. The web page will allow users to nominate their favorite songs, vote for songs that others have nominated, and delete songs that they absolutely can not stand. At the end of the ad campaign, a list will be published of the top three songs in the competition.

A preliminary version of this page was uploaded, and a log file of sample behavior (adding, deleting, and voting for songs) has been collected. It is your job to decide on the proper data structure to use for this back-end program, and you have decided, based on your understanding of the lectures in CS 112 so far, that two possibilities deserve further investigation: (i) a sorted array, using binary search (this array will be expandable, see details below); (ii) a sorted linked list (with header node), using linear search.

You decide that the best way to figure out which of these is the best data structure is to run through log file, performing the necessary actions on each of the two data structures, and to count the number of "basic operations" (to be explained below). You will select the data structure which performs the fewest such operations.

Your task in this assignment is to write the code that will implement adding, deleting, and searching for items in both kinds of lists, following the log file, and to print out some basic information at the end, including the number of string comparisons performs.

This assignment will exercise your knowledge of file I/O, formatted output basic data structures for lists, evaluation of data structures for a particular application, as well as the use of good object-oriented design in C++.

Details of the Program

Please take careful note of the following points. You may be permitted to change certain details if you discuss them with me first.

1. The log file will be a text file called "LogFile.txt" and contain lines of the following three types:

General Form                               Example
------------                               -------
add <artist:song title>                    add Beatles:NowhereMan
delete <artist:song title>                 delete DebbieBoone:YouLightUpMyLife
vote <artist:song title>                   vote GratefulDead:DireWolf

You must use the Scanner class to read this file (which you can assume will always have the same name, and will exist in the same directory as your program). Song titles will be Strings with no white space (which will simplify your task of reading them from the log file). You do not need to separate the band from the song title until you print out the top three songs at the end. You will not know how many lines the log file contains. Two log files (one short one to use as you develop your program, and one we will run your program on to grade it) will be available on the web site.

2. The operations to be performed on the lists, while reading the log file, are the following:

Note that everything starts with a search for the item and that actions that do not make sense are simply ignored.

3. The items in these lists will be nodes (classes) that contain a String (the band & song title), the number of votes cast for this song, and (in the case of linked lists) a reference to the next item in the list. The band and song title are simply a string, and you do not need to do anything other than compare these strings when searching the lists (at the end, when printing out your results, you will need to separate out the band and song title).

3. The array will be implemented as a class that contains an initial array of size 100 items; if the array fills up, you should reallocate a new array of twice the previous size, and copy the old list to the bottom half of the new list, and use the new list thereafter; thus the list could potentially grow to be of sizes 100, 200, 400, 800, and so on. You should use binary search to search for the item. In the case that you must insert or delete an item, you will need to move everything over to make sure (which may cause the array to be reallocated at twice the size). You may reuse your PA #1 code, or you can use the sample solution which we will post soon.

3. The linked list should be implemented as discussed in class. You can use either the lookahead technique or the double pointer technique for deletion and insertion, as we will discuss further in class.

4. You will estimate the number of times you "touch" a node by counting comparisons of Strings and copying of references in the array implementation. Specifically, you need to keep a counter for each of the two list implementations, and count:

The first kind of operation will occur in either list, and the last two only occur in the array implementation. You should do this in all cases when reading the log file, whether the search is successful or not. But, this number should not count operations (if any) performed during the search for the top song (you should not have to do string comparisons here in any case).

 

An Example Session

Your program should print out to the standard output for each implementation: (i) the length of the final list, (ii) the top three songs (ties may be arbitrarily ordered), and the number of basic operations taken. This should be nicely formatted, and in particular, the table of the top three songs should be output using tabs ('\t') to line up the columns, and you should separate the string from the node into the band (before the ':') and the song title (after the ':'). Do not separate the words in the title or band. The following is a sample of what you should produce:

Reading log file "LogFile.txt" for array implementation.....

Final voting resulted in 256 entries. The top three songs were:

             Band                 Song Title          Votes
             ----                 ----------          ----- 
             Beatles              NowhereMan          56
             GratefulDead         DireWolf            23
             TheBand              TheWeight           21
		 
The array implementation took 783 basic operations.

		 
Reading log file "LogFile.txt" for linked list implementation.....

Final voting resulted in 256 entries. The top three songs were:

             Band                 Song Title          Votes
             ----                 ----------          ----- 
             Beatles              NowhereMan          56
             GratefulDead         DireWolf            23
             TheBand              TheWeight           21
		 
The linked list implementation took 645 basic operations.

Note that the only thing that should be different in the results for the two implementations is the number of operations; printing out the results redundantly is simply a way of (partially) checking the correctness of your implementations.

Error Recover and Error Reporting

You do not need to account for any errors in this assignment.

Style Guidelines

You should follow the style guidelines as posted on the web page. You should use good OOD principles, breaking your program into (at least) the following three classes:

What to Submit

Your program should be written in three files: SongVoting.cpp, ArrayList.cpp, and LinkedList.cpp (you may add others if you wish, but your driver program should be called SongVoting.cpp). You should also submit a Makefile and a short README file describing any features of your implementation. Create a directory called PA2 in your home directory, copy your solution files into this directory and submit it using gsubmit.

Be sure to remove all debugging statements before submission.

Grading Criteria

Refer to the grading criteria file: p2.criteria.

You are responsible for thoroughly testing your program to make sure that it works. Your code must run correctly on csa. In grading your program, we will test your program on the (longer) of the two log files on the web site.


Academic Honesty and Collaboration

Cooperation is recommended in understanding programming concepts and system features. But the actual solution of the assignments including all programming must be your individual work. For example, copying without attribution any part of someone else's program is plagiarism, even if you have modified the code. The University takes acts of cheating and plagiarism very seriously; first time violators are routinely suspended for a semester.