CS 1044 Program 2 Spring 2002

Similar documents
CS 1044 Project 1 Fall 2011

The Program Specification:

CS 1044 Project 2 Spring 2003

Decision Logic: if, if else, switch, Boolean conditions and variables

CS 1044 Program 6 Summer I dimension ??????

CS 2604 Minor Project 1 Summer 2000

CS 2604 Minor Project 1 DRAFT Fall 2000

Fundamental Concepts: array of structures, string objects, searching and sorting. Static Inventory Maintenance Program

gcc o driver std=c99 -Wall driver.c bigmesa.c

Each line will contain a string ("even" or "odd"), followed by one or more spaces, followed by a nonnegative integer.

CS 1044 Project 5 Fall 2009

A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of two opposite corners:

Given that much information about two such rectangles, it is possible to determine whether they intersect.

You will provide an implementation for a test driver and for a C function that satisfies the conditions stated in the header comment:

For storage efficiency, longitude and latitude values are often represented in DMS format. For McBryde Hall:

CS 1044 Program 1 Fall 1998

CS 2604 Minor Project 3 Movie Recommender System Fall Braveheart Braveheart. The Patriot

CS 2604 Minor Project 3 DRAFT Summer 2000

gcc o driver std=c99 -Wall driver.c everynth.c

CS 1044 Programming in C++ Test 1 Spring 2011 Form B Page 1 of 12 READ THIS NOW!

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Do not start the test until instructed to do so!

CS 2704 Project 1 Spring 2001

READ THIS NOW! Do not start the test until instructed to do so!

Setting Justification

Pointer Casts and Data Accesses

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

I/O Streams and Standard I/O Devices (cont d.)

Creating a String Data Type in C

Expressions, Input, Output and Data Type Conversions

Spring 2002 Page 1 of 8 READ THIS NOW!

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

Week 3: File I/O and Formatting 3.7 Formatting Output

struct _Rational { int64_t Top; // numerator int64_t Bottom; // denominator }; typedef struct _Rational Rational;

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

CS 2704 Project 3 Spring 2000

Fundamentals of Programming Session 8

C++ Input/Output: Streams

We will exclusively use streams for input and output of data. Intro Programming in C++

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Definition Matching (10 Points)

Pointer Accesses to Memory and Bitwise Manipulation

Invoice Program with Arrays and Structures

CS 1044 Programming in C++ Test 1 READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties.

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

Chapter 3 - Notes Input/Output

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Do not start the test until instructed to do so!

For this assignment, you will implement a collection of C functions to support a classic data encoding scheme.

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

CS2304 Spring 2014 Project 3

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

Chapter 2 - Control Structures

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

بسم اهلل الرمحن الرحيم

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

CS 2704 Project 2: Elevator Simulation Fall 1999

File Navigation and Text Parsing in Java

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

Pointer Accesses to Memory and Bitwise Manipulation

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

Lab 15 Review of Arrays, Array of Objects and Vector Dr. John Abraham, Professor

Pointer Accesses to Memory and Bitwise Manipulation

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

PR quadtree. public class prquadtree< T extends Compare2D<? super T> > {

CS 142 Style Guide Grading and Details

Both parts center on the concept of a "mesa", and make use of the following data type:

The assignment requires solving a matrix access problem using only pointers to access the array elements, and introduces the use of struct data types.

Chapter 2 - Control Structures

Chapter 2 - Control Structures

CS 111X - Fall Test 1

Accessing Data in Memory

Department of Computer and Mathematical Sciences. Lab 10: Functions. CS 1410 Intro to Computer Science with C++

Lecture 4 Tao Wang 1

Simple C Dynamic Data Structure

CS 2505 Computer Organization I

Control Statements: Part Pearson Education, Inc. All rights reserved.

Getting started with C++ (Part 2)

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

Chapter 3. Numeric Types, Expressions, and Output

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Loops and Files. of do-while loop

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Chapter 3: Input/Output

What we will learn about this week:

Chapter 2 Basic Elements of C++

Full file at

3. When you process a largest recent earthquake query, you should print out:

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

CS3114 (Fall 2013) PROGRAMMING ASSIGNMENT #2 Due Tuesday, October 11:00 PM for 100 points Due Monday, October 11:00 PM for 10 point bonus

CS1100: Excel Lab 1. Problem 1 (25 Points) Filtering and Summarizing Data

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

Independent Representation

Transcription:

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