CSE 9A Fall 1996 Assignment 7: This one counts as one and one half assignments. (i.e., we will multiply your score for this assignment by 1.5.) This is the last assignment in the course. DO NOT TURN THIS IN WITH THE turnin PROGRAM BEFORE 8 AM Saturday, November 23. If you finish before then, just wait a while before turning it in. Also, check for changes before turning it in. Deadlines: For turnin program: midnight at end of day on Tuesday December 3 with the usual penalty: midnight at end of day on Wednesday Dec 4. On this assignment, there is another very late deadline of midnight at the end of the day Thursday, Dec 5. If you turn it in after Wednesday Dec 4 and before midnight Thursday Dec 5, you will receive an ADDITIONAL 10% penalty over what you would normally get for turning it in on Wednesday Dec 4. (Note Friday Dec 6 is the last day of classes) For interviews: End of day Sunday December 8 Do not wait till the last minute. There will not be a postponement of these deadlines. There will also be a rush for interviews. There are only a few interview days after the turnin date. Get your interview early or you may miss out on getting an interview! It's the end of the quarter! If you do not get an interview in time, you will get a zero on this assignment. PROGRAMMING: For this assignment you will write a program that performs some data processing for The National Wildlife Foundation. Your program will read data on a list of species and calculate an estimate of what the population for each species will be after two years. Input is from a file named data.in and output goes to a file called data.out. There is no input from the keyboard and no output to the screen (except error messages). The input file has the format: species name population birth-rate death-rate species name population birth-rate death-rate .... You can assume that the file contains complete records. So you can assume, for example, that your program will not encounter an end of file after a species name or a population or a birth-rate. However, your program must be able to handle extra white space after the species name and must be able to handle whitespace after the last record. NB: For simplicity we assume that a species name does not contain any blanks; hyphens are OK; e.g, if the species is fire ants, we spell it fire-ants. With this assumption, you can read and then write out a species name as follows: char name[40]; cin >> name; cout << name << endl; But, in this assignment you use file streams rather than cin and cout. See the section "File Names as Input" in Chapter 5 (starting on p 224) for a discussion of this kind of string input and output. The file data.in which is in the same directory as this file can be used as a sample input file, but your program should work even if we change the file data.in so that it contains more data or less data or different data. The output will be in the following form: 1. A heading that says: Species Population, Birth rate, Death rate: 2.A blank line 3.An entry for each species that has the form Species Name population birth-rate death-rate NOTE: The population figure in the output file is the estimated population after TWO years have past. That is the population figure is the estimated population after adjusting two times for births and deaths. After one year the new population is the old population plus birth_rate*(the old population) minus death_rate*(the old population). Note: When converting from values of type double to values of type int, simply discard the fraction after the decimal point. So, the update action for one year really is new population is the old population + floor(birth_rate*(the old population) - floor(death_rate*(the old population) The function floor is in math.h. (Look up floor in the index.) (The official compiler is the CC compiler, but you may want to also test things with the g++ compiler. To use math.h with the g++ compiler see the file ~/../public/g++.math for special instructions.) The file hw07.o has the object code for a solution to this. If you run hw07.o in a directory that also contains the file data.in, the program will create a file called data.out. Note that your program must must produce a file data.out that matches the the file produced by hw07.o. WARNING: hw07.o will overwrite any file named data.out that happens to be in the same directory as hw07.o when it is run. Below is the class definition for a class called SpeciesRecord. Your program will use an object of the class SpeciesRecord to record the data for one species as a time. Below are all the prototypes for all the functions you will use in your program. You just need to write main, add the include directives, write the program header comment, and add the definitions of all the functions, including the definitions for the member functions for the class SpeciesRecord. Hint: Proceed as follows: 1. Copy all of the code in hint 5 into a file: 2. Add include directives and the main part of the program. 3. Compile and fix any syntax errors. 4. Add the function definitions one at a time, compiling and fixing syntax errors for each function before adding another function. (You may want to also use some driver programs to test some of these functions.) hint 5: Class definition and some other code fragments. //Class for a population record for one species: //Note that this does not include the name. Only data numbers. class SpeciesRecord { public: SpeciesRecord(); //Sets all the data members to zero. SpeciesRecord(int new_population, double new_birth_rate, double new_death_rate); //Sets the data members according to the arguments. void update(); //Precondition: The member variables have values. //Postcondition: The population figure has been changed to what it //will be in one year given the birth_rate and death_rate. void input(istream& in_stream); //Note: We made the type istream so as to make the class definition //more versatile, even though ifstream would suffice for this assignment. //Precondition:If in_stream is an input file stream, then //in_stream has been connected to a file with open. //If it is not the end of a file, then the input that is coming next //in the stream starts with a complete record consisting of three numbers. //Action:Reads three numbers from the stream in_stream. //Sets the values of the population, birth rate, and //death rate (respectively) for the object record to these three integers. //This function call ends with a call to new_line(in_stream); //IMPORTANT: This function does not try to read more than one //character beyond the end of a file. This ensures that the //eof stream member function will be well behaved. If you try to //use this input function at the end of a file, then eof will be set to //true and the object that called input will not be changed. void output(ostream& outs); //Note: We made the type ostream so as to make the class definition //more versatile, even though ofstream would suffice for this assignment. //Precondition: If outs is a file output stream, then //outs has already been connected to a file with open. //Postcondition: The data in the calling object has been written to //the stream outs. The output is all on one line in the format: //population one-space birth_rate one-space death_rate //Note that the only output produced is 3 numbers separate by space //The values with a decimal point are output in ordinary notation //with 4 digits after the decimal point. private: int population; double birth_rate;//e.g., 0.5 means 50 births per 100 per year. double death_rate;//e.g., 0.5 means 50 deaths per 100 per year. }; void new_line(istream& in_stream); //Discards all the input remaining on the current input line of in_stream. //Also discards the '\n' at the end of the line. //Precondition: in_stream has been connected to a file with a call to open. //NOTE: You can find the definition of this function in Chapter 5. int main() { ..... return 0; } //Uses iostream.h. Uses new_line. void SpeciesRecord::input(istream& in_stream) { char next; in_stream.get(next);//If this is the end of a file, //then this will make eof FALSE. //If this is not the end of a file, //next is putback so this has no affect. if (! in_stream.eof()) { in_stream.putback(next); THE HEART OF THE FUNCTION DEFINITION GOES HERE. YOU HAVE TO FILL IT IN. } }