New version page

MIT 1 124J - Problem Set 4

Documents in this Course
Load more

This preview shows page 1-2-3 out of 10 pages.

View Full Document
View Full Document

End of preview. Want to read all 10 pages?

Upload your study docs or become a GradeBuddy member to access this document.

View Full Document
Unformatted text preview:

Local DiskProblem Set # 4 of 1.124J Foundations of Software Engineering1.124J Foundations of Software Engineering Problem Set 4 Due Date: Tuesday 10/17/00 Reference Readings: From C++ Primer (in addition to the reference reading from PS1, PS2 & PS3): ● Chapter 11: I/O ● Chapter 12: Templates ● chapter 14: File processing From algorithms in C++ ● Chapter 9: Quicksort ● Chapter 14: Binary search Problem 1:[40%] In this problem you need to develop a program that can handle user-provided data. You need to write the main() function and two template functions. The program should be able to read using the one template function monthly data for a number of years. It should be used twice, once to read the number of visitors, let's say to a tourist resort, and the other the income in millions of dollars. The input data are provided in the following files, in tabular form with each row representing the year and for each row having 12 columns, one for each months ● visitorsNumber.data: contains the number of tourist visitors ● touristIncome.data: contains the revenue in millions of dollars e.g. the touristIncome.data file looks as follows: 474.33 129.72 232.33 239.43 234.56 244.60 286.72 362.46 283.43 245.26 253.40 98.07240.34 268.36 245.36 165.36 234.45 242.43 344.25 320.20 447.93 375.26 201.30 94.34 The first element represents the tourist income for the first month of the corresponding year, which in this case assume that it is 1997. The second row has the 12 values for the income for the 1 months of year 1998, and so on. You are asked to implement the two template functions in the file ps4_1.h, and the main() function in the file ps4_1.C. main(): The main() function should: ● Create an array of ints, of a 50 rows by 12 columns size, to hold the provided data. This array should be named visitors. ● Invoke the template function readData to read in the data. The readData function should read until the end of the provided file. Each row has 12 values, which correspond to the values of each month of a given year. The function readData should get from the user the initial year. It should also count the number of years for which data are provided in the input file. ● Invoke the function writeInvertedData to save in a file, whose name is given by the user, the numbers for visitors, in inverted format. The data should be saved in tabular form with each column representing a year and each row a month. A certain formatting should be used as shown in a sample execution of the program. ● Then, create an array of double, of size 50 rows by 12 columns, to hold the provided data. Name the array income. ● Invoke the template function readData to read in the data. The readData function should read all rows until the end of the provided file. Each row has 12 values, one for each month of the corresponding year. The function readData should get from the user the initial year. It should count the number of years for which data are provided in the input file while reading the data. ● Invoke the function writeInvertedData to save in a file the income values in tabular form. The name of the file should be given by the user, and the data should be saved in an "inverted format", with each column representing a year and each row a month. In contrast, the data are read in with each row corresponding to a year and each column to a month. The exact reverse of this should be used in this function, i.e. each row should correspond to a month and each column to the year. This formatting is illustrated below in a sample execution of an implemented program. readData() template function: This template function should: ● prompt the user to give the first year of statistical data, e.g. 1997 ● prompt the user to give the name of the input data, e.g. touristIncome.data ● open, then, the file with that name ● read until the end of file in the values into a 2-dimensional array that is passed by reference frommain() to hold the corresponding data. It should also count the number of years for which data are provided while reading in the data ● print out the number of years for which data are provided writeInvertedData() template function: This template function should: ● prompt the user to give the name of the file where the data should be stored ● then, save in that file the data, which are provided by the array that is passed as argument to the function, e.g. visitors or income. The data should be printed in an inverted way. A sample execution of the program, below, demonstrates the expected format that should be used while saving the data in a file. Each row should correspond to a month and each column to a year. A specified number of decimals (i.e. precision) should be used to print out the data. Values of double (such as those of the income array) should be printed with 3 decimal points, while int values (such as those of the visitors array) should be printed without any decimal point. Any int value should be printed as an integer and not in a scientific way, e.g. you should print 3453000 and not as 3.453+06. ● print out the number of years for which data has been saved Sample execution of the program: The execution of your program should have an output similar to the following. The bolded and italic identify whatever is entered by the user of your program. Visitors Statistics First year: 1997 File with data: visitorsNumber.data Data for 3 years have been read File to store data: visitors.out Data for 3 years have been stored to file: visitors.out Income Statistics First year: 1997 File with data: touristIncome.data Data for 2 years have been read File to store data: touristIncome.out Data for 2 years have been stored to file: touristIncome.outThe contents of the input and output files, after the above execution, are presented below: Input files: visitorsNumber.data: 475544 1985572 2076432 2165239 2283546 2344460 2635040 2958672 3047626 2843543 2734526 2523400 2412307 878734 2343240 2546836 2436456 1943656 2453425 2754240 3256404 3454645 3698200 4138479 3743756 3398320 983474 923498 2845672 3456566 2341653 2734562 2534400 2346307 943734 2234544 2398636 2345346 1546256 2453435 2345240 3456504 touristIncome.data 474.33 129.72 232.33 239.43 234.56 244.60 286.72 362.46 283.43 245.26 253.40 98.07 240.34 268.36 245.36 165.36 234.45 242.43 344.25 320.20 447.93 375.26 201.30 94.34 Output files: visitors.out: Month 1997 1998 1999 1 475544


View Full Document
Loading Unlocking...
Login

Join to view Problem Set 4 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Problem Set 4 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?