Elementary Programming in C++: Software Development Process

The software development life cycle is a multi-stage process that includes require-ments specification, analysis, design, implementation, testing, deployment, and maintenance.

Developing a software product is an engineering process. Software products, no matter how large or how small, have the same life cycle: requirements specification, analysis, design, implementation, testing, deployment, and maintenance, as shown in Figure 2.2.

Requirements specification is a formal process that seeks to understand the problem that the software will address and to document in detail what the software system must do. This phase involves close interaction between users and developers. Most of the examples in this book are simple, and their requirements are clearly stated. In the real world, however, prob­lems are not always well defined. Developers need to work closely with their customers (indi­viduals or organizations that will use the software) and study the problem carefully to identify what the software must do.

System analysis seeks to analyze the data flow and to identify the system’s input and out­put. Analysis helps to identify what the output is first, and then figure out what input data you need in order to produce the output.

System design is the process for obtaining the output from the input. This phase involves the use of many levels of abstraction to break down the problem into manageable components and design strategies for implementing each component. You can view each component as a subsystem that performs a specific function of the system. The essence of system analysis and design is input, process, and output (IPO).

Implementation involves translating the system design into programs. Separate programs are written for each component and then integrated to work together. This phase requires the use of a programming language such as C++. The implementation involves coding, self­testing, and debugging (that is, finding errors, called bugs, in the code).

Testing ensures that the code meets the requirements specification and weeds out bugs. An independent team of software engineers not involved in the design and implementation of the product usually conducts such testing.

Deployment makes the software available for use. Depending on the type of the software, it may be installed on each user’s machine or installed on a server accessible on the Internet.

Maintenance is concerned with updating and improving the product. A software product must continue to perform and improve in an ever-evolving environment. This requires peri­odic upgrades of the product to fix newly discovered bugs and incorporate changes.

To see the software development process in action, we will now create a program that computes loan payments. The loan can be a car loan, a student loan, or a home mortgage loan. For an introductory programming course, we focus on requirements specification, analysis, design, implementation, and testing.

Stage 1: Requirements Specification

The program must satisfy the following requirements:

  • Allow the user to enter the annual interest rate, loan amount, and number of years for which payments will be made
  • Compute and display the monthly payment and total payment amounts

Stage 2: System Analysis

The output is the monthly payment and total payment, which can be obtained using the following formulas:

Therefore, the input needed for the program is the monthly interest rate, the length of the loan in years, and the loan amount.

Note

The requirements specification says that the user must enter the annual interest rate, loan amount, and number of years for which payments will be made. During analysis, however, it’s possible that you may discover that input is insufficient or that some values are unnecessary for the output. If this happens, you can modify the requirements specification.

Note

In the real world, you will work with customers from all occupations. You may develop software for chemists, physicists, engineers, economists, and psychologists. Of course, you will not have (or need) complete knowledge of all these fields. Therefore, you don’t have to know how formulas are derived, but given the annual interest rate, the loan amount, and the number of years for which payments will be made, you can compute the monthly payment in this program. You will, however, need to communicate with customers and understand how a mathematical model works for the system.

Stage 3: System Design

During system design, you identify the steps in the program.

Step 1. Prompt the user to enter the annual interest rate, the loan amount, and the number of years. (The interest rate is commonly expressed as a percentage of the principal for a period of one year. This is known as the annual interest rate.)

Step 2. The input for the annual interest rate is a number in percent format, such as 4.5%. The program needs to convert it into a decimal by dividing it by 100. To obtain the monthly interest rate from the annual interest rate, divide it by 12, since a year has 12 months. To obtain the monthly interest rate in decimal format, you must divide the annual interest rate in percentage by 1200. For example, if the annual interest rate is 4.5%, then the monthly interest rate is 4.5/1200 = 0.00375.

Step 3. Compute the monthly payment using the preceding formula.

Step 4.       Compute the total payment, which is the monthly payment multiplied by 12 and multiplied by the number of years.

Step 5. Display the monthly payment and total payment.

Stage 4: Implementation

Implementation is also known as coding (writing the code). In the formula, you have to compute (1 + montMylnterestRate)number0fYears *12, which can be obtained using pow(1 + monthlyInterestRate, numberOfYears * 12).

Listing 2.11 gives the complete program.

Listing 2.II   ComputeLoan.cpp

1 #include <iostream>

2 #include <cmath>

3 using namespace std;

4

5 int main()

6 {

7    // Enter yearly interest rate

8    cout << “Enter yearly interest rate, for example 8.25: “;

9    double annualInterestRate;

10   cin >> annualInterestRate;

11

12    // Obtain monthly interest rate

13   double monthlyInterestRate = annualInterestRate / 1200;

14

15    // Enter number of years

16   cout << “Enter number of years as an integer, for example 5: “;

17   int numberOfYears;

18    cin >> numberOfYears;

19

20    // Enter loan amount

21    cout << “Enter loan amount, for example 120000.95: “;

22    double loanAmount;

23    cin >> loanAmount;

24

25    // Calculate payment

26    double monthlyPayment = loanAmount * monthlyInterestRate /

27    (1 1 / pow(1 + monthlyInterestRate, numberOfYears * 12));

28    double totalPayment = monthlyPayment * numberOfYears * 12;

29

30    monthlyPayment = static_cast<int>(monthlyPayment * 100) / 100.0;

31    totalPayment = static_cast<int>(totalPayment * 100) / 100.0;

32

33    // Display results

34    cout << “The monthly payment is ” << monthlyPayment << endl <<

35    “The total payment is ” << totalPayment << endl;

36

37    return 0;

38 }

To use the pow(a, b) function, you have to include the cmath library in the program (line 2) in the same way you include the iostream library (line 1).

The program prompts the user to enter annualInterestRate, numberOfYears, and loanAmount in lines 7-23. If you entered an input other than a numeric value, a runtime error would occur.

Choose the most appropriate data type for the variable. For example, numberOfYears is better declared as int (line 17), although it could be declared as long, float, or double. Note that unsigned short might be the most appropriate for numberOfYears. For sim­plicity, however, the examples in this book will use int for integer and double for floating­point values.

The formula for computing the monthly payment is translated into C++ code in lines 26-27. Line 28 obtains the total payment.

Casting is used in lines 30-31 to obtain a new monthlyPayment and totalPayment with two digits after the decimal points.

Stage 5: Testing

After the program is implemented, test it with some sample input data and verify whether the output is correct. Some of the problems may involve many cases, as you will see in later chapters. For these types of problems, you need to design test data that cover all cases.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *