documentyy

9
Jason Chiang Wann Chun 4633775 CSCI124 Applied Programming Feb 2014 Session LAB TASK 7 This Lab Task is worth 2% to your subject marks Background: The aim of this lab is to get some experience with sorting data. TASK For this lab assume you work at the NASDAQ (http://www.nasdaq.com) stock market. Each day your supervisor gives you a list of the top 100 or so traders in the IT sector. Such data is represented in a file such as stocks-data.txt. The stock data file consists of many lines, each representing trade information relating to a specific company. Consider the following line: AAPL 155 747733 The first field represents the stock tag i.e. AAPL (Apple Inc). The second field represents the current value of each Apple share, in this case $155.00 and the last field represents the number of Apple shares traded during the day. The data presented to you in such a file is not sorted. Your supervisor would like you to write a little program that sorts the data in stocks-data.txt in various ways. Your program should produce one of either two reports. The reports are generated as text files, which can then be further processed.

Upload: jason-chiang

Post on 06-Nov-2015

212 views

Category:

Documents


0 download

DESCRIPTION

er

TRANSCRIPT

Jason Chiang Wann Chun 4633775

CSCI124 Applied ProgrammingFeb 2014 SessionLAB TASK 7

This Lab Task is worth 2% to your subject marks

Background:

The aim of this lab is to get some experience with sorting data.

TASK

For this lab assume you work at the NASDAQ (http://www.nasdaq.com) stock market. Each day your supervisor gives you a list of the top 100 or so traders in the IT sector. Such data is represented in a file such as stocks-data.txt. The stock data file consists of many lines, each representing trade information relating to a specific company. Consider the following line:

AAPL 155 747733

The first field represents the stock tag i.e. AAPL (Apple Inc). The second field represents the current value of each Apple share, in this case $155.00 and the last field represents the number of Apple shares traded during the day.

The data presented to you in such a file is not sorted. Your supervisor would like you to write a little program that sorts the data in stocks-data.txt in various ways. Your program should produce one of either two reports. The reports are generated as text files, which can then be further processed.

The first report your program should produce is a descending list (largest to smallest) of company stock values. The file your program should produce looks like this:

Corporate Stock Volume Data - NASDAQStock TagCostGOOG 390ADBE 192INFY 167AAPL 155LOGI 130ERIC 110As you can see, each line of the file represents a stock and its current value in order of largest to smallest. That said what about the first three lines? Well come back to those later.

The other report your program should generate is as follows:

Corporate Stock Volume Data - NASDAQStock TagVolumeERIC 928238INFY 899921ORCL 893487BEAS 868694AAPL 747733CTSH 685684NVDA 654743

This report represents the volume of shares traded for each company from largest to smallest.

Your task

Step 1Define a suitable data structure (possibly a struct) to represent a single stock.

Step 2Write a program in the file main.cpp that opens the file stocks-data.txt and reads the data into an array. The filename MUST be hard coded into your program. You can assume the array is of type defined in step 1. You can also assume you will not have more than 100 stocks at any given time.

Step 3Once all data is read in and stored in an appropriate data structure, display a menu.

Main Menu:c: Sort Data by Costv: Sort Data by Trade Volumeq: quitenter choice:

If a user enters an invalid choice, the main menu should be reprinted. If the user enters the c option, your program should sort the data stored in the array. Once the data has been sorted, it should be written to the file in a format specified above. The program should then terminate.

If a user enters the v option, exactly the same process is repeated but this time the data is sorted by trade volume. In either case, options c and v produce a single output file the file should be known as data.txt. There should be no prompting of file names in your program. Add this code to main.cpp. For both sorting, you are required to implement the quicksort sorting technique.

#include #include #include using namespace std;

struct stock{ char name[10]; int cost; int tradeVolume;};

void in_file ( fstream &, stock[100], int&);void q_sort_cost ( stock[100], int, int);void q_sort_tradeVolume ( stock[100], int, int);int cost ( stock[100], int, int);int tradevolume ( stock[100], int, int);void printCost ( ofstream &, stock[100], int);void printVolume ( ofstream &, stock[100], int);

int main(){ stock detail[100];

int i = 0; int size = 0; int high; char menu; int index;

fstream infile; ofstream writefile;

infile.open("stocks-data.txt", ios::in);

while ( infile.good() ) { infile >> detail[i].name; infile >> detail[i].cost; infile >> detail[i].tradeVolume;

if ( !infile.eof()) { i++; size ++; } }

infile.close();

cout