Topics covered on Wednesday, 2010-09-01
Software Development Life-cycle
- 1. Problem Definition
- The user defines the problem. The developer and the user work together to ensure that all aspects of the task to be done are clearly understood by both and documented.
- 2. Requirements Analysis
- The developer interviews all affected users to discover sources and sinks of data and processes involved in writing a package to solve the problem.
- 3a. Data Design
- The developer understands all data formats and designs structures and controls to ensure that appropriate data is captured, stored and exposed to the appropriate users in the system.
- 3b. Program Design
- The designer defines the problem in terms of sub-problems, each sub-problem in terms of sub-sub-problems, and so on. This process iterates until each problem element is small enough to be represented as a function prototype in the target programming language. The designer documents the interface and the function of each element of the solution using function prototypes and pseudo-code.
- 4. Coding
- Each function is handed over to be coded by one or more people.
- 5. Testing and debugging
- The code is tested and debugged.
- Unit Testing: Each function is tested in isolation to see whether it produces the correct result.
- Integration Testing: Program flow involving many functions is tested as a whole to ensure the solution components interact appropriately with each other.
- 6. Deployment
- The solution is deployed in the target environment, perhaps with one more level of live testing.
Reasons to Use Functions
There is no operation that may be done in a function which may not be done in the main program. Similarly, there is nothing that can be done in the main program which cannot be handed off to a function. However, some reasons to use functions instead of doing everything in main are:
- The same task done at multiple places in the program is best handed over to a function. Then the function may be called at each place where the task needs to be done.
- Any set of operations that achieve a clearly identifiable result is a candidate for moving into a separate function. This simplifies both program coding as well as understanding.
- Using functions helps develop programs top-down. At any stage, you need only think about the logic of what you are doing, and hand the details off to functions. Once that is done, you can look at each function and repeat the process (look at the logic and leave the particulars to more detailed functions).
Assignment
Use functions to achieve the task specified in the
previous assignment (compute4.cpp). One part of the program is done, please write the remaining functions.
compute4.cpp partial
1 // compute4.cpp
2 //
3 // Get an operation and two numbers from the user, from the command
4 // line or interactively, and display the computed result.
5 //
6 // Copyright (C) 2010, Raj Mathur
7 #include <iostream>
8 #include <cstdlib>
9
10 using namespace std;
11 //
12 // Functions (prototypes)
13 char get_operation(int argc, char *argv[]);
14 int get_first_number(int argc, char *argv[]);
15 int get_second_number(int argc, char *argv[]);
16 int do_operation(char op, int num1, int num2);
17
18 int main(int argc, char *argv[])
19 {
20 char
21 op;
22 int
23 number_1 = 0;
24 int
25 number_2 = 0;
26 int
27 result;
28 // Get operation type (p == plus, m == minus)
29 op = get_operation(argc, argv);
30 // Get values to be used for computation
31 number_1 = get_first_number(argc, argv);
32 number_2 = get_second_number(argc, argv);
33 // Do the operation
34 result = do_operation(op, number_1, number_2);
35 // Display it
36 cout << "The result is: " << result << endl;
37 exit(0);
38 }
39 //
40 // get_first_number
41 //
42 // Try to get the first number from the second argument on the command
43 // line. If it is not available, get it from the user.
44 //
45 int get_first_number(int argc, char *argv[])
46 {
47 int
48 num;
49 // Are there enough arguments? If yes, then convert the second one.
50 if( argc >= 2 )
51 {
52 num = atoi(argv[2]);
53 }
54 else // Not enough arguments, get it from user
55 {
56 cout << "Enter the first number: ";
57 cin >> num;
58 }
59 return( num );
60 }