We have discussed so far various features of C language and are ready to write and execute programs of modest complexity. However, before attempting to develop complex programs, it is worthwhile to consider some programming techniques that would help design efficient and error free programs.
The program development process includes three important stages, namely, program design, program coding and program testing. All the three stages contribute to the production of high quality programs. In this chapter we shall discuss some of the techniques used for program design in c, coding and testing.
Program design is the foundation for a good program and is therefore an important part of the program development cycle. Before coding a program, the program should be well conceived and all aspects of the program design should be considered in detail.
Program design is basically concerned with the development of a strategy to be used in writing the program, in order to achieve the solution of a problem. This includes mapping out a solution procedure and the form the program would take. The program design involves the following four stages:
- Problem analysis.
- Outlining the program structure.
- Algorithm development.
- Selection of control structures.
Before we think of a solution procedure to the problem, we must fully understand the nature of the problem and what we want the program to do. Without the comprehension and definition of the problem at hand, program design might turn into a hit or miss approach. We must carefully decide the following at this stage;
What kind of data will go in ?;
What kind of outputs are needed?; and
What are the constraints and conditions under which the program has to operate?
Outlining the Program Structure
Once we have decided what we want and what we have, then the next step is to decide how to do it. C as a structured language lends itself to a top down approach. Top down means decomposing of the solution procedure into tasks that form a hierarchical structure, as shown in Fig.
The essence of the top down design is to cut the whole problem into a number of independent constituent tasks, and then to cut the tasks into smaller subtasks, and so on, until they are small enough to be grasped mentally and to be coded easily. These tasks and subtasks can form the basis of functions in the program.
An important feature of this approach is that at each level, the details of the design of lower levels are hidden. The higher level functions are designed first, assuming certain broad tasks of the immediately lower level functions.
The actual details of the lower level functions are not considered until that level is reached. Thus the design of functions proceeds from top to bottom, introducing progressively more and more refinements.
This approach will produce a readable and modular code that can be easily understood and maintained. It also helps us classify the overall functioning of the program in terms of lower level functions.
After we have decided a solution procedure and an overall outline of the program, the next step is to work a out a detailed definite, step by step procedure, known as algorithm for each function. The most common method of describing an algorithm is through the use of flowcharts.
The other method is to write what is known as pseudocode. The flow chart presents the algorithm pictorially, while the pseudocode describe the solution steps in a logical order. Either method involves concepts of logic and creativity.
Since algorithm is the key factor for developing an efficient program, we should devote enough attention to this step. A problem might have many different approaches to its solution.
For example, there are many sorting techniques available to sort a list. Similarly, there are many methods of finding and select the one, which is simple the area under a curve. We must consider all possible approaches to follow, takes less execution time, and produces results with the required accuracy.
Control Structures – Program Design
A complex solution procedure may involve a large number of control statements to direct the flow of execution. In such situations, indiscriminate use of control statements such as goto may lead to unreadable and comprehensible programs.
It has been demonstrated that any algorithm can be structured, using the three basic control structure, namely, sequence structure, selection structure, and looping structure.
Sequence structure denotes the execution of statements sequentially one after another. Selection structure involves a decision, based on a condition and may have two or more branches, which usually join again at a later point.
if …else and switch statements in C can be used to implement a selection structure. Looping structure is used when a set of instructions is evaluated repeatedly. This structure can be implemented using do, while, or for statements.
A well designed program would provide the following benefits:
- Coding is easy and error free.
- Testing is simple.
- Maintenance is easy.
- Good documentation is possible.
- Cost estimates can be made more accurately.
- Progress of coding may be controlled more precisely.
|Read More Topics|
|Program testing and debugging|
|Common programing error|
|Malloc in C|