7.7 Case Study: Calculating Depreciation



Similar documents
C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Comp151. Definitions & Declarations

Sequential Program Execution

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Member Functions of the istream Class

C++ Input/Output: Streams

Basics of I/O Streams and File I/O

Ch 7-1. Object-Oriented Programming and Classes

Appendix K Introduction to Microsoft Visual C++ 6.0

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Chapter 9 Text Files User Defined Data Types User Defined Header Files

Formatting Numbers with C++ Output Streams

Answers to Review Questions Chapter 7

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Passing 1D arrays to functions.

An Incomplete C++ Primer. University of Wyoming MA 5310

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

While Loop. 6. Iteration

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Moving from C++ to VBA

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

The While Loop. Objectives. Textbook. WHILE Loops

EP241 Computer Programming

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Conditions & Boolean Expressions

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Common Beginner C++ Programming Mistakes

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Computer Programming C++ Classes and Objects 15 th Lecture

Chapter One Introduction to Programming

Chapter 5 Functions. Introducing Functions

COMPUTER SCIENCE 1999 (Delhi Board)

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Chapter 7. Functions 7-1

Answers to Mini-Quizzes and Labs

Syllabus OBJECT ORIENTED PROGRAMMING C++

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

The little endl that couldn t

C++ INTERVIEW QUESTIONS

Lecture 2 Notes: Flow of Control

! " # $ %& %' ( ) ) *%%+, -..*/ *%%+ - 0 ) 1 2 1

CSI33 Data Structures

Visual Studio 2008 Express Editions

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Appendix M: Introduction to Microsoft Visual C Express Edition

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

J a v a Quiz (Unit 3, Test 0 Practice)

Compiler Construction

Depreciation and Depletion

Basics of C++ and object orientation in OpenFOAM

13 Classes & Objects with Constructors/Destructors

Data Structures using OOP C++ Lecture 1

C Strings and Pointers

6. Control Structures

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

The Payroll Program. Payroll

F ahrenheit = 9 Celsius + 32

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

The if Statement and Practice Problems

Schedule. Structures and Classes in C++ Outline. Goals for This Topic. Another Example of a Structure. What is a Structure? Classes May 12-17, 2005

Answers to Selected Exercises

Chapter 5. Selection 5-1

C++FA 5.1 PRACTICE MID-TERM EXAM

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

1. The First Visual C++ Program

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Fondamenti di C++ - Cay Horstmann 1

Understanding Depreciation, Fixed, and Variable Costs

CISC 181 Project 3 Designing Classes for Bank Accounts

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Function Overloading I

COSC 181 Foundations of Computer Programming. Class 6

Pointers and Linked Lists

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Using C++ File Streams

Sources: On the Web: Slides will be available on:

CS 241 Data Organization Coding Standards

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

C++ program structure Variable Declarations

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

C++ Language Tutorial

Setting up Auto Import/Export for Version 7

Course notes Standard C++ programming

Transcription:

7.7 Case Study: Calculating Depreciation 1 7.7 Case Study: Calculating Depreciation PROBLEM Depreciation is a decrease in the value over time of some asset due to wear and tear, decay, declining price, and so on. For example, suppose that a company purchases a new computer system for $200,000 that will serve its needs for 5 years. After that time, called the useful life of the computer, it can be sold at an estimated price of $50,000, which is the computer s salvage value. Thus, the value of the computing equipment will have depreciated $150,000 over the 5-year period. The calculation of the value lost in each of several years is an important accounting problem, and there are several ways of calculating this quantity. We want to write one or more functions to calculate depreciation tables that display the depreciation in each year of an item s useful life. OBJECT-CENTERED DESIGN BEHAVIOR. Each function should receive from its caller the amount to be depreciated and the number of years. The function should then display on the screen a depreciation table. OBJECTS. The objects in this problem are straightforward: Software Objects Problem Objects Type Kind Movement Name The amount to be depreciated varying real received amount The item's useful life (in years) varying integer received numyears The annual depreciation varying real depreciation Each function will have the same specification: Receive: Output: amount and numyears a depreciation table OPERATIONS AND ALGORITHMS. The operations in this problem depend on the method used to calculate the depreciation. There are several different methods, and we will consider two of them here. One standard method is the straight-line method, in which the amount to be depreciated is divided evenly over the specified number of years. For example, straight-line depreciation of $150,000 over a 5-year period gives an annual depreciation of $150,000 / 5 = $30,000:

2 7.7 Case Study: Calculating Depreciation Year With this method, the value of an asset decreases a fixed amount each year. The operations needed to calculate straight-line depreciation are all provided by C++ operations and statements: i. Divide a real (amount) by an integer (numyears) ii. Output an integer (the year) and a real (the depreciation) iii. Repeat ii a specified number (numyears) of times. Organizing them in an algorithm is straightforward. Depreciation 1 $30,000 2 $30,000 3 $30,000 4 $30,000 5 $30,000 Algorithm for Straight-Line Depreciation 1. Calculate depreciation = amount / numyears. 2. For year ranging from 1 through numyears do the following: Display year and depreciation. Another common method of calculating depreciation is called the sum-of-the-years -digits method. To illustrate it, consider again depreciating $150,000 over a 5-year period. We first calculate the sum of the years digits, 1 + 2 + 3 + 4 + 5 = 15. In the first year, 5/15 of $150,000 ($50,000) is depreciated; in the second year, 4/15 of $150,000 ($40,000) is depreciated; and so on, giving the following depreciation table: Year Depreciation 1 $50,000 2 $40,000 3 $30,000 4 $20,000 5 $10,000 In addition to the operations for the straight-line method, the sum-of-the-years -digits method requires: iv. Sum the integers from 1 through some given integer (numyears) For this we can use the function sum() in Case Study 7.7.1.

7.7 Case Study: Calculating Depreciation 3 An algorithm for displaying annual depreciation values using this method is as follows: Algorithm for Sum-of-the-Years-Digits Depreciation 1. Calculate sum = 1 + 2 +... + numyears. 2. For year ranging from 1 through numyears do the following: a. Calculate depreciation = (numyears year + 1) * amount / sum b. Display year and depreciation. CODING. The function straightline() in Case Study 7.7-1 implements the algorithm for the straight-line method of depreciation and the function sumofyears() implements the algorithm for the sum-of-the-years -digits method. Because these functions are useful in a variety of problems, we would probably store them in a library with these function definitions along with that for function sum() in an implementation file and the prototypes void straightline(double amount, int numyears); void sumofyears(double amount, int numyears); in the corresponding header file (e.g., Depreciation.h). Case Study 7.7-1 Depreciation Functions. #include <iostream> #include <iomanip> #include <cassert> using namespace std; // cout // setprecision(), setw(), setiosflags() // assert() /* straightline() displays a depreciation table for a given * amount over numyears years using the straight-line method. * * Receive: amount, a real * numyears, an integer * Output: a depreciation table * Uses: format manipulators from iostream and iomanip ****************************************************************/ void straightline(double amount, int numyears) double depreciation = amount / numyears; cout << "\nyear - Depreciation" << "\n--------------------\n"; cout << fixed << showpoint << right // set up format for $$ << setprecision(2); for (int year = 1; year <= numyears; year++) cout << setw(3) << year << setw(13) << depreciation << endl;

4 7.7 Case Study: Calculating Depreciation /* sumofyears() displays a depreciation table for a given * amount over numyears years using the sum-of-the-years'-digits * method. * * Receive: amount, a real * numyears, an integer * Output: a depreciation table * Uses: function Sum() from Figure 6.1 * format manipulators from iostream and iomanip ****************************************************************/ void sumofyears(double amount, int numyears) cout << "\nyear - Depreciation" << "\n--------------------\n"; double yearsum = sum(numyears); double depreciation; cout << fixed << showpoint << right // set up format for $$ << setprecision(2); for (int year = 1; year <= numyears; year++) depreciation = (numyears - year + 1) * amount / yearsum; cout << setw(3) << year << setw(13) << depreciation << endl; TESTING. The program in Case Study 7.7-2 uses these depreciation functions. It begins by defining a menu a - to enter information about a new item c - to use the sum-of-the-years'-digits method --> which it passes to the function getmenuchoice() described in Section 7.2 of the text. This function repeatedly displays a menu and reads the user s choice until the user enters a valid choice, which is then returned to the caller. For option a, the user enters an item s purchase price, its salvage value, and its useful life. Options b and c call the functions straightline() and sumofyears(), respectively, to display the depreciation tables. The sentinel value QUIT = 'd' signals the end of input and a switch statement is used to process non-quit options.

7.7 Case Study: Calculating Depreciation 5 Case Study 7.7-2 Depreciation Functions. /* depreciationtables.cpp computes depreciation tables. * * Input: purchase price, salvage value, and useful * life of an item * Output: depreciation tables. ****************************************************************/ #include "Depreciation.h" #include <iostream> #include <string> using namespace std; // <<, >>, cout, cin // string // or if not using this library, // insert the prototypes of straightline() and sumofyears() here and // insert after main(), the function sum() from Figure 7.2 of the text // and the #include directives and function definitions from // Case Study 7.7-1. char getmenuchoice(string MENU, char firstchoice, char lastchoice); int main() const string MENU = "\n\n" " \n" " \n" " c - to use the sum-of-the-years'-digits method\n" " \n" "--> "; const char QUIT = 'd'; cout << "This program computes depreciation tables using\n" << "various methods of depreciation.\n"; char option; double purchaseprice, salvagevalue, amount; int usefullife; // menu option selected by user // item's purchase price, // salvage value, and // amount to depreciate, and // useful life in years for (;;) option = getmenuchoice(menu, 'a', QUIT); // get user's choice if (option == QUIT) break;

6 7.7 Case Study: Calculating Depreciation switch (option) // perform the option selected case 'a': // get new item information cout << "What is the item's:\n" " purchase price? "; cin >> purchaseprice; cout << " salvage value? "; cin >> salvagevalue; cout << " useful life? "; cin >> usefullife; amount = purchaseprice - salvagevalue; break; case 'b': // straight-line method straightline(amount, usefullife); break; case 'c': // sum-of-years-digits method sumofyears(amount, usefullife); break; default: // execution shouldn t get here cerr << "*** Invalid menu choice: " << option << endl; /* getmenuchoice() repeatedly displays a MENU of choices in the * range firstchoice to lastchoice and reads a user's choice until * a valid choice is entered, which is then returned to the caller. * * Receive: MENU, a string * firstchoice and lastchoice, chars * Return: the choice entered by the user ****************************************************************/ char getmenuchoice(string MENU, char firstchoice, char lastchoice) char choice; // what the user enters for (;;) cout << MENU; cin >> choice; if ((choice >= firstchoice) && (choice <= lastchoice)) return choice; cerr << "\ni'm sorry, but " << choice << " is not a valid menu choice.\n"; Sample run:

7.7 Case Study: Calculating Depreciation 7 This program computes depreciation tables using various methods of depreciation. --> a What is the item's purchase price? 2000.00 salvage value? 500.00 useful life? 5 --> b Year - Depreciation -------------------- 1 300.00 2 300.00 3 300.00 4 300.00 5 300.00 --> c Year - Depreciation -------------------- 1 500.00 2 400.00 3 300.00 4 200.00 5 100.00 --> x

8 7.7 Case Study: Calculating Depreciation I'm sorry, but x is not a valid menu choice --> a What is the item's purchase price? 1200.00 salvage value? 200.00 useful life? 3 --> b Year - Depreciation -------------------- 1 333.33 2 333.33 3 333.33 --> d Exercise 1. A third method of calculating depreciation is the double-declining balance method. In this method, if an amount is to be depreciated over n years, 2 / n times the undepreciated balance is depreciated annually. For example, in the depreciation of $150,000 over a 5-year period using the double-declining balance method, 2/5 of $150,000 ($60,000) would be depreciated the first year, leaving an undepreciated balance of $90,000. In the second year, 2/5 of $90,000 ($36,000) would be depreciated, leaving an undepreciated balance of $54,000. Since only a fraction of the remaining balance is depreciated each year, the entire amount will never be depreciated. Consequently, it is permissible to switch to the straight-line method at any time. Develop an algorithm for this third method of calculating deprecia-

7.7 Case Study: Calculating Depreciation 9 tion. Modify the program in Case Study 7.7-1 so that it includes this third method of calculating depreciation as one of the options. Also, modify the output produced by the depreciation functions so that the year numbers in all the depreciation tables begin with the current year rather than with year number 1.