Simple Algebraic Calculations One of the first things you will learn about C++ is how to perform numerical computations. In this project, you are given an incomplete program (see the end of this specification), which already includes the statements necessary to read input and specify output. You must add statements so that your completed program will perform the specified calculations. This programming assignment uses many of the ideas presented in section 3 of the course notes and textbook, so you are advised to read those carefully. Read and follow the following program specification carefully. This program is considerably simpler than the map mileage program from assignment 1, but don't underestimate it, especially if you've not done much or any programming before. The Program Specification: Calculating the Future Value of an Investment You will write computation instructions in a program shell to calculate the future value of an investment, using the formula: where: S r nt = P 1 + n S P r n t is the value of the investment after t years is the initial value of the investment is the annual interest rate the investment earns is the number of periods per year is the number of years the investment is held Input file description and sample: Your program will read its input from a file named invest.txt use of another input file name will result in massive deductions by the automatic grader. The first line is a label line which must be skiiped. Each following line of the input file will contain four values, separated by whitespace: the amount initially invested (positive real number between 1.00 and 100,000.00) the annual interest rate earned (positive real number between 0.001 and 0.20) number of periods per year (integer between 1 and 365) number of years investment is held (integer between 1 and 30) You may assume that all input values will be logically correct (no negatives, for instance), and that all values will be in the specified ranges. For instance: # Principal Rate Period Years 1000.00 0.05 12 10 1000.00 0.05 12 20 1000.00 0.05 12 30 1000.00 0.05 365 10 1000.00 0.05 365 20 1000.00 0.05 365 30 Note that your program will not make any assumptions about the number of lines of data in the input file. Your program has been written so that it will detect when it's out of input and terminate correctly (a technique for this is also in the map mileage program). Due Date:Check section Web site Page 1 of 7
What to Calculate: Your program after reading each line of input data, as described above, must calculate the corresponding future balance value and summary data for each principal investment. Specifically, the program must also compute the total principal invested, total of all investment balances, and the average investment balance. In order to achieve the best possible accuracy, you are required to use variables of type double, not type float, to store all real (that is, decimal) numbers needed in this program. Although the given program is incomplete, it does declare all the variables that are needed to calculate the required results. You may declare additional variables if you like, but they will not be logically necessary. In order to raise the parenthesized quantity to a power, use the Standard Library function pow( ). This function may be used to calculate y = x k, where x is a double and k is an integer: y = pow(x, k); In order to use pow( ), your program includes the standard header file cmath. Output description and sample: Your program will write all output data to a file named balance.txt use of any other output file name will again result in a massive deduction of points by the auto-grader. The sample output file shown below corresponds to the sample input data given above: Dwight Barnette Future Investment Computations Principal Rate Periods Years Balance ------------------------------------------------------------ 1000.00 0.0500 12 10 1647.01 1000.00 0.0500 12 20 2712.64 1000.00 0.0500 12 30 4467.74 1000.00 0.0500 365 10 1648.66 1000.00 0.0500 365 20 2718.10 1000.00 0.0500 365 30 4481.23 ------------------------------------------------------------ 6000.00 17675.38 Average Investment Balance: 2945.90 You are not required to use this exact horizontal spacing, but your output will satisfy the following requirements: The first line of the output file must contain your name. The second line of the output file must contain the label Future Investment Computations. The third line of the output file must be blank. You must use the specified column labels, in the Fourth line of the output file. All dollar amounts must be formatted to show two places after the decimal. The annual interest rates must be formatted to show four places after the decimal. The number of periods and number of years must be formatted as integer values. Allow sufficient space for each value your program prints; the future values could be on the order of 10 million. You must arrange your output in neatly aligned columns. You must use the same ordering of the columns as shown here. Do not insert any additional lines of output; do have a newline at the end of each line. Due Date:Check section Web site Page 2 of 7
If you have read the description of how the Curator scores your program in the Student Guide, you know that is important that you use the same spelling and capitalization for all the labels shown above. The horizontal spacing does not effect scoring unless you combine things that should be separate or separate things that should be combined. In any case, the given code will exactly match the labels and spacing shown above. Testing: At minimum, you should be certain that your program produces the output given above when you use the given input file and all test date sets posted on the course web site. However, verifying that your program produces correct results on a single test case does not constitute a satisfactory testing regimen. You should make up and try additional input files as well; of course, you'll have to determine what the correct output would be. What to turn in and how: You will submit this assignment to the Curator System (read the Student Guide), and it will be graded automatically. Instructions for submitting, and a description of how the grading is done, are contained in the Student Guide. You will be allowed up to five submissions for this assignment. Use them wisely. Test your program thoroughly before submitting it. Make sure that your program produces correct results for every sample input file posted on the course website. If you do not get a perfect score, analyze the problem carefully and test your fix with the input file returned as part of the Curator e-mail message, before submitting again. Evaluation: Your submitted program will be assigned a score based upon the runtime testing performed by the Curator System. We will not be evaluating your submission of this program for documentation style. However, you should examine the given program as a guide to acceptable documentation, and include similar comments for the statements you add to it. For a number of the later projects, we will also evaluate your submission for documentation, and for other requirements. It is best to begin preparing for that now. Pledge: Each of your program submissions must be pledged to conform to the Honor Code requirements for this course. Specifically, you must include the following pledge statement in the header comment for your program: On my honor: - I have not discussed the C++ language code in my program with anyone other than my instructor or the teaching assistants assigned to this course. - I have not used C++ language code obtained from another student, or any other unauthorized source, either modified or unmodified. - If any C++ language code or documentation used in my program was obtained from another source, such as a text book or course notes, that has been clearly noted with a proper citation in the comments of my program. - I have not designed this program in such a way as to defeat or interfere with the normal operation of the Curator System. <Student Name> Failure to include this pledge in a submission is a violation of the Honor Code. Due Date:Check section Web site Page 3 of 7
On my honor: - I have not discussed the C++ language code in my program with anyone other than my instructor or the teaching assistants assigned to this course. - I have not used C++ language code obtained from another student, or any other unauthorized source, either modified or unmodified. - If any C++ language code or documentation used in my program was obtained from another source, such as a text book or course notes, that has been clearly noted with a proper citation in the comments of my program. - I have not designed this program in such a way as to defeat or interfere with the normal operation of the Curator Server. FutureValue.cpp: Calculating Future Value of an Investment You should substitute the correct information in the following lines: Programmer: Bill McQuain Modified: Dwight Barnette Date: Jan 23, 2002 Section : CS1044: #11348 Compiler: Visual C++ version 6.0 Platform: PentiumIV 1.5GHz / Windows 2000 Pro 5.00.2195 SP 2 Purpose of the program: If P dollars are invested at an annual rate of r, compounded n times per year for t years, the final (or future) value of the investment is given by the formula: S = P(1 + r/n)^(nt) This program applies this formula to calculate the future value of investments specified in the input file "invest.txt" and prints a report summarizing each investment to the output file "balance.txt". The report includes the information above and the final value of the investment. In addition, the total principal invested, the total of all investment balances, and the average investment balance is computed and output. #include <fstream> #include <iomanip> #include <string> #include <cmath> using namespace std; Due Date:Check section Web site Page 4 of 7
int main() { Constant declarations: const string DASHESLINE = " --------------------------------------------------"; const char NEWLINE = '\n'; Variable declarations: int NumPeriods, number of periods per year NumYears, number of years investment is held Exponent, number of periods in given number of years Investments; total number of investments double InitBalance, starting principal (balance) of investment AnnualRate, annual interest rate earned PeriodicRate, interest rate per compounding period BaseFactor, 1 + AnnualRate FinalBalance, final balance of investment TotalPrincipal, Total of all invested principals TotalBalance, Total of all investment balances AverageBalance; Average balance of all investments ifstream ifile; input file stream ofstream ofile; output file stream The following four statements prepare the input and output files for use: ifile.open("invest.txt"); open input file ofile.open("balance.txt"); open output file ofile.setf(ios::fixed, ios::floatfield); ofile.setf(ios::showpoint); Print the header information to the output file: ofile << "Dwight Barnette" ofile << "Future Investment Computations" ofile ofile << " Principal Rate Periods Years Balance" ofile << DASHESLINE Initialize record counter and principal total Investments = 0; TotalPrincipal = 0.0; TotalBalance = 0.0; ifile.ignore(255, NEWLINE); ignore the header label line Read initial balance of first record/line of data from input file: (priming read) ifile >> InitBalance; Due Date:Check section Web site Page 5 of 7
Process records/lines of investment data until none remain: while (ifile) { Read remaining data of record/line from input file: ifile >> AnnualRate >> NumPeriods >> NumYears; ifile.ignore(255, NEWLINE); skip end of line character a computation goes here a computation goes here Count input records/lines Sum principal investment -------------------------------- Begin investment computation section Some calculations go here... Set up terms for the future value formula. Compute the future value: a computation goes here --------------------------------- End of investment computation section Print the summary information for this investment: ofile << setw(10) << setprecision(2) << InitBalance << setw(10) << setprecision(4) << AnnualRate << setw(13) << NumPeriods << setw(10) << NumYears << setw(18) << setprecision(2) << FinalBalance --------------------------------- End of Output Section a computation goes here Sum investment balances Read the initial balance of the next record/line of input: ifile >> InitBalance; } End of while loop. ofile << DASHESLINE Due Date:Check section Web site Page 6 of 7
Output Total of invested principals and total of balances ofile << setw(10) << setprecision(2) << TotalPrincipal << setw(51) << setprecision(2) << TotalBalance ofile a computation goes here compute average investment balance Output average investment balances ofile << setw(51) << "Average Investment Balance:" << setw(10) << setprecision(2) << AverageBalance Close the files: ifile.close(); ofile.close(); return Investments; return number of processed investment records } end of main() Due Date:Check section Web site Page 7 of 7