hey guys,
i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this.
i have to write the following code. this will be the last time i ask for an entire code or u can give me the outine of what to do and i ll do it by myself.
the description is the following:
the program will read a text file that contains historical price of a stock. The program will allow users to query the stock price of a particular date and output its statistics (mean price, median price and standard deviation) for any
specified period. it is menu driven and works as follows.
$ StockAnalyser
Enter the stock file name: bhp.csv
Number of records: 762, from 26/08/2002 to 26/08/2005
Welcome to use the StockAnalyser (version 1.0)
[g] Get the price of a specific date
[s] Get the statistics of a period
[q] Quit
Please select [g,s,q]: g
Date (dd/mm/yyyy): 23/03/2003
Price is not available for the date: 23/03/2003
Welcome to use the StockAnalyser (version 1.0)
[g] Get the price of a specific date
[s] Get the statistics of a period
[q] Quit
Please select [g,s,q]: g
Date (dd/mm/yyyy): 27/03/2003
Stock Price for BHP on 27/03/2003 is $9.3600
Welcome to use the StockAnalyser (version 1.0)
[g] Get the price of a specific date
[s] Get the statistics of a period
[q] Quit
Please select [g,s,q]: s
Start date (dd/mm/yyyy): 01/01/2004
End date (dd/mm/yyyy): 10/01/2004
Statistics of stock BHP for the period of 01/01/2004 - 10/01/2004 is:
Mean price: $12.1417 Median Price: $12.1800 Std Deviation: 0.2705
Welcome to use the StockAnalyser (version 1.0)
[g] Get the price of a specific date
[s] Get the statistics of a period
[q] Quit
Please select [g,s,q]: s
Start date (dd/mm/yyyy): 15/01/2004
End date (dd/mm/yyyy): 15/01/2004
The period you specify must be more than one day
Welcome to use the StockAnalyser (version 1.0)
[g] Get the price of a specific date
[s] Get the statistics of a period
[q] Quit
Please select [g,s,q]: x
Wrong input!
Welcome to use the StockAnalyser (version 1.0)
[g] Get the price of a specific date
[s] Get the statistics of a period
[q] Quit
Please select [g,s,q]: q
Thank you for using StockAnalyser (version 1.0)!
$
Input File Format
The historical price for a particular stock can be exported from most Stock Brokers. The files are in text format. Usually, one file contains the price of a particular stock for a certain
period. For example, the file bhp.csv is the price of BHP stock for the period from 26 August 2002 to 26 August 2005, as partially shown
below.
BHP,"27 Aug 2002",9.53,9.56 ,9.45,9.45,8117 955
BHP,"26 Aug 2002",9.45,9.48 ,9.4,9.48,69906 17
BHP,"28 Aug 2002",9.54,9.55 ,9.4,9.44,12771 227
BHP,"29 Aug 2002",9.31,9.31 ,9.21,9.27,1198 6227
BHP,"30 Aug 2002",9.21,9.23 ,9.11,9.11,1934 0245
BHP,"02 Sep 2002",9.15,9.23 ,9.05,9.23,8681 758
BHP,"03 Sep 2002",9.14,9.25 ,9.14,9.24,7833 427
BHP,"04 Sep 2002",9,9.01,8. 88,8.9,18221850
........
........
BHP,"17 Aug 2005",20.6,20.8 4,20.53,20.55,1 7867065
BHP,"18 Aug 2005",20.3,20.4 ,20.13,20.28,18 604022
BHP,"19 Aug 2005",20.27,20. 75,20.27,20.68, 16486925
BHP,"22 Aug 2005",20.86,21. 25,20.82,21.22, 18694638
BHP,"23 Aug 2005",21.43,21. 47,20.98,21.07, 20581539
BHP,"24 Aug 2005",20.95,21. 11,20.56,20.56, 23072751
BHP,"25 Aug 2005",20.05,20. 23,19.9,19.9,40 871120
BHP,"26 Aug 2005",20.29,20. 6,20.21,20.6,37 759955
Each line or record contains the information on the stock for a particular day. It consists of seven
fields separated by “,”
first field name of the stock
second field date (notice that the date string is double quoted)
third field opening price
forth field highest bid
fifth field lowest bid
sixth field close price
seventh field volume
“StockAnal yser” only cares about the close price!
Notice that the file usually doesn’t have any records for weekends and public holidays and you
should not assume the records are in the order of dates!
bhp.csv can be downloaded from WeebCT.
Read a stock data file
“StockAnal yser” extracts the first (stock name), second (date) and sixth field (close price) from
the input file, and places these fields in a proper data structure.
Assume that “StockAnal yer” only supports data files containing stock prices after 01 January
2000, and each file will not have records of more than five years.
Get a price for a particular day
After extracting information from a file, “StockAnal yser” is ready to output the price of any date.
When users select menu [g], “StockAnal yser” asks for a date in a format of dd/mm/yyyy (e.g.
03/12/2003) outputs the price of the specified date or reports “Price is not available” for the date
if the file does not have the record.
Find the price of a particular date is a problem of search. You MUST use binary search
algorithm for the sake of speed.
Calculate the statistics of the price for a specified period
Users can also select menu [s] to request “StockAnal yer” to calculate the statistics of the price for
a specified period. In this case, “StockAnal yer” asks users for a starting and ending dates of the
period in a format of dd/mm/yyyy, and calculates the following statistics
- mean (average) price of the stock during this period
- median price of the stock during this period
- standard deviation of the stock price during this period
In a case that the stock file does not contain a record for either the staring date or the ending date.
“StockAnal yser” will set the staring date as the earliest date that is later than the specified starting
date and has a record in the file. For ending date, “StockAnal yzer” will set the ending date as the
latest date that are earlier than the specified ending date and has a record in the file.
For example, if a file contains records for 02/09/2004 and 05/09/2004 and no records for
03/09/2004 and 04/09/2004. If users specify 03/09/2004 as the starting date, “StockAnal yser”
will set the starting date as 05/09/2004. If users specify 04/09/2004 as the ending date,
“StockAnal yser” will set 02/09/2004 as the ending date.
Hints:
1) To use a binary search algorithm to find the price of a particular date, you need to sort the
records against the date field(s). Since date has three fields: day, month and year, sorting
against the date (multiple fields) is not an easy task. You may consider converting the date
into an integer in such a way that each date has a unique corresponding integer value. This
value can be used as the key to sort all records.
2) Find the median price of a period, you may need fully or partially sort the records of this
period against the price.
3) You may find this information useful. In a leap year, the number of days in February is 29.
and the following code is to check whether a year is a leap year or not
_Bool isleapyear(int year)
{
return ((year%400==0) || (year%4==0 && year%100 !=0));
}
i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this.
i have to write the following code. this will be the last time i ask for an entire code or u can give me the outine of what to do and i ll do it by myself.
the description is the following:
the program will read a text file that contains historical price of a stock. The program will allow users to query the stock price of a particular date and output its statistics (mean price, median price and standard deviation) for any
specified period. it is menu driven and works as follows.
$ StockAnalyser
Enter the stock file name: bhp.csv
Number of records: 762, from 26/08/2002 to 26/08/2005
Welcome to use the StockAnalyser (version 1.0)
[g] Get the price of a specific date
[s] Get the statistics of a period
[q] Quit
Please select [g,s,q]: g
Date (dd/mm/yyyy): 23/03/2003
Price is not available for the date: 23/03/2003
Welcome to use the StockAnalyser (version 1.0)
[g] Get the price of a specific date
[s] Get the statistics of a period
[q] Quit
Please select [g,s,q]: g
Date (dd/mm/yyyy): 27/03/2003
Stock Price for BHP on 27/03/2003 is $9.3600
Welcome to use the StockAnalyser (version 1.0)
[g] Get the price of a specific date
[s] Get the statistics of a period
[q] Quit
Please select [g,s,q]: s
Start date (dd/mm/yyyy): 01/01/2004
End date (dd/mm/yyyy): 10/01/2004
Statistics of stock BHP for the period of 01/01/2004 - 10/01/2004 is:
Mean price: $12.1417 Median Price: $12.1800 Std Deviation: 0.2705
Welcome to use the StockAnalyser (version 1.0)
[g] Get the price of a specific date
[s] Get the statistics of a period
[q] Quit
Please select [g,s,q]: s
Start date (dd/mm/yyyy): 15/01/2004
End date (dd/mm/yyyy): 15/01/2004
The period you specify must be more than one day
Welcome to use the StockAnalyser (version 1.0)
[g] Get the price of a specific date
[s] Get the statistics of a period
[q] Quit
Please select [g,s,q]: x
Wrong input!
Welcome to use the StockAnalyser (version 1.0)
[g] Get the price of a specific date
[s] Get the statistics of a period
[q] Quit
Please select [g,s,q]: q
Thank you for using StockAnalyser (version 1.0)!
$
Input File Format
The historical price for a particular stock can be exported from most Stock Brokers. The files are in text format. Usually, one file contains the price of a particular stock for a certain
period. For example, the file bhp.csv is the price of BHP stock for the period from 26 August 2002 to 26 August 2005, as partially shown
below.
BHP,"27 Aug 2002",9.53,9.56 ,9.45,9.45,8117 955
BHP,"26 Aug 2002",9.45,9.48 ,9.4,9.48,69906 17
BHP,"28 Aug 2002",9.54,9.55 ,9.4,9.44,12771 227
BHP,"29 Aug 2002",9.31,9.31 ,9.21,9.27,1198 6227
BHP,"30 Aug 2002",9.21,9.23 ,9.11,9.11,1934 0245
BHP,"02 Sep 2002",9.15,9.23 ,9.05,9.23,8681 758
BHP,"03 Sep 2002",9.14,9.25 ,9.14,9.24,7833 427
BHP,"04 Sep 2002",9,9.01,8. 88,8.9,18221850
........
........
BHP,"17 Aug 2005",20.6,20.8 4,20.53,20.55,1 7867065
BHP,"18 Aug 2005",20.3,20.4 ,20.13,20.28,18 604022
BHP,"19 Aug 2005",20.27,20. 75,20.27,20.68, 16486925
BHP,"22 Aug 2005",20.86,21. 25,20.82,21.22, 18694638
BHP,"23 Aug 2005",21.43,21. 47,20.98,21.07, 20581539
BHP,"24 Aug 2005",20.95,21. 11,20.56,20.56, 23072751
BHP,"25 Aug 2005",20.05,20. 23,19.9,19.9,40 871120
BHP,"26 Aug 2005",20.29,20. 6,20.21,20.6,37 759955
Each line or record contains the information on the stock for a particular day. It consists of seven
fields separated by “,”
first field name of the stock
second field date (notice that the date string is double quoted)
third field opening price
forth field highest bid
fifth field lowest bid
sixth field close price
seventh field volume
“StockAnal yser” only cares about the close price!
Notice that the file usually doesn’t have any records for weekends and public holidays and you
should not assume the records are in the order of dates!
bhp.csv can be downloaded from WeebCT.
Read a stock data file
“StockAnal yser” extracts the first (stock name), second (date) and sixth field (close price) from
the input file, and places these fields in a proper data structure.
Assume that “StockAnal yer” only supports data files containing stock prices after 01 January
2000, and each file will not have records of more than five years.
Get a price for a particular day
After extracting information from a file, “StockAnal yser” is ready to output the price of any date.
When users select menu [g], “StockAnal yser” asks for a date in a format of dd/mm/yyyy (e.g.
03/12/2003) outputs the price of the specified date or reports “Price is not available” for the date
if the file does not have the record.
Find the price of a particular date is a problem of search. You MUST use binary search
algorithm for the sake of speed.
Calculate the statistics of the price for a specified period
Users can also select menu [s] to request “StockAnal yer” to calculate the statistics of the price for
a specified period. In this case, “StockAnal yer” asks users for a starting and ending dates of the
period in a format of dd/mm/yyyy, and calculates the following statistics
- mean (average) price of the stock during this period
- median price of the stock during this period
- standard deviation of the stock price during this period
In a case that the stock file does not contain a record for either the staring date or the ending date.
“StockAnal yser” will set the staring date as the earliest date that is later than the specified starting
date and has a record in the file. For ending date, “StockAnal yzer” will set the ending date as the
latest date that are earlier than the specified ending date and has a record in the file.
For example, if a file contains records for 02/09/2004 and 05/09/2004 and no records for
03/09/2004 and 04/09/2004. If users specify 03/09/2004 as the starting date, “StockAnal yser”
will set the starting date as 05/09/2004. If users specify 04/09/2004 as the ending date,
“StockAnal yser” will set 02/09/2004 as the ending date.
Hints:
1) To use a binary search algorithm to find the price of a particular date, you need to sort the
records against the date field(s). Since date has three fields: day, month and year, sorting
against the date (multiple fields) is not an easy task. You may consider converting the date
into an integer in such a way that each date has a unique corresponding integer value. This
value can be used as the key to sort all records.
2) Find the median price of a period, you may need fully or partially sort the records of this
period against the price.
3) You may find this information useful. In a leap year, the number of days in February is 29.
and the following code is to check whether a year is a leap year or not
_Bool isleapyear(int year)
{
return ((year%400==0) || (year%4==0 && year%100 !=0));
}
Comment