Assignment 08

Due: Thursday March 24, 2011, at noon, 100 points

For this assignment you will submit (in the usual way) multiple files. These files will contain a C++ program that you have named appropriately, each file with an appropriate name and appropriate extension. Be sure that your program compiles using the g++ compiler before you submit it. To compile a program with multiple files, use the usual g++ command but instead of the single file you compiled before, compile *.cpp and the compiler will "grab" all the cpp files in that directory. So, you see, it's important that you put all the files for a particular program in their own directory! You don't have to list the header files; that will happen automatically.

Background: As is almost always the case, old Trog is finding a college edumacation a bit pricey and he's found himself a job for the summer working in a brokerage house selling burnination futures. Being that he's just a stupid dragon, he requires software to do almost all semi-intellegible activities for him. So, he needs you to write a program that will process information stored in a data file.

Specifications: The gist: Your program will read data from a file, the name of which is entered by the user of the program (that would be Trogdor). That data will be stored in an array, processed/used, then written out to another file. Call the output file "out.dat".

Details:

  • The data in the file will be stock information1. The information you have for each stock is the Item, the price paid, and the price-to-earnings ratio (P/E). You will create "burninations.dat" in the same directory in which you write this program. It will contain the information listed here in this format (without the headings, of course):

    • Item price P/E ---- ----- --- Huts 78.90 6.8 Peasants 15.75 5.2 Countryside 89.33 9.3 Villages 49.73 3.7 Trees 1.2 3.5

  • You will need to use a struct to contain the information for a stock.

  • Declare your array of size 6. Thus, the five stocks listed above will fit nicely with room to spare.

  • You would be wise to declare a "counter" to keep track of how many stocks are indeed in the array.

  • Once the data is read in, your program will sort the data in the array according to P/E, largest to smallest. You will include in your program a templated sorting algorithm. You can use the selection sort presented in class if you like. But if you really want to learn something, write the code for bubble sort by yourself. If you google it, you'll find it in one bizillionth of a second, but you won't learn anything. If you try, you can code it in 20 minutes and learn a lot. Now, if you template the sort, then the operator < will have to work for what you are sorting - stocks. That means you have to overload the < operator for stocks. Thus, make stock A less than stock B if A's P/E is less than B's P/E.

  • Once the "portfolio" (that's the array) is sorted, your program is to repeatedly ask the user (that would be Trogdor) if he wishes to add another stock to the list. Of course, you will not allow a full list to be expanded. Sorry, 6 is the maximum allowable stocks in a portfolio. Each time a stock is added, sort the list. When the user chooses not to make any modifications of the list, output the portfolio to the output file.

  • Optional: Make part of the last point above to give the user the option to drop a stock from the list (by name). That isn't quite as easy as the other part. If you do this part correctly, we'll give you 10 pts. However, do notattempt this part until you have everything else right!

  • Say goodbye.

Of course, you will do this progam in files (I count at least 4) and you will use functions nicely. Be sure to comment your code and functions adequately. As always, don't underestimate the time to complete this assignment.

When you submit:

  1. choose to add (Cars, 7.3, 8.3) to the list

  2. choose to add (Cattle, 1.2, 3.3) to the list

  3. quit the modifications and exit

As always, if you have questions, don't hesitate to ask your instructor or the LEAD GUY if you need help.

1You must understand that I know nothing about stock futures; I don't even know what they are! But I hear the term used on the radio and I'm pretty good at making up nonsense stories wrapped around real stuff... like Trogdor. He's real.