





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






C Code Implementation
Revision 2. December 2001
Andrew J. Blauch Paul D. Johnson
Padnos School of Engineering
Simple programming exercises can often be solved by just sitting down and writing code to implement the desired problem. Complex problems, however, can be difficult to write and impossible to debug if not implemented using a structured design process. There are many structured design methodologies that can be used to implement programs and solve other types of engineering problems. Using a structured design process leads to the following benefits:
The application of a structure design methodology greatly increases the probability of completing a successful design with a minimum amount of time and expense. By using a structured design methodology, the likelihood of finding design flaws early improves considerably. Finding design flaws early in the design process greatly reduces the cost of fixing those flaws. A programming problem that might be fixable for a few dollars early in the design process might cost many thousands of dollars to repair when the flaw is not detected until the project is near completion. Structured design methods also improve the ability to modify programs at a later date since the techniques make the production of clear and complete documentation much easier. Structured designs can also be more easily broken up into modules to improve testing and to allow development by multiple design teams with a reasonable assurance that the resulting products will be compatible with each other.
A structured flowchart is one in which all of the processes and decisions must fit into one of a few basic structured elements. The basic elements of a structured flowchart are shown in Figure 2. It should be possible to take any structured flowchart and enclose all of the blocks within one of the following process structures. Note that each of the structures shown below has exactly one input and one output. Thus the structure itself can be represented by a single process block.
SEQUENCE structure IF-THEN-ELSE structure WHILE structure
The SEQUENCE process is just a series of processes carried out one after the other. Most programs are represented at the highest level by a SEQUENCE, possible with a loop from the end back to the beginning.
The IF-THEN-ELSE process logically completes the binary decision block by providing two separate processes. One of the processes will be carried out in the each path from the binary decision.
The WHILE process allows for the representation of a conditional loop structure within a program. The decision to execute the process in the loop is made prior to the first execution of the process.
Figure 2: Basic Flowchart Structures
Although all flowcharts can be represented by the above basic structures, it is sometimes useful to employ some additional structures, each of which can themselves be constructed from the above structures. These derived structures are shown in Figure 3.
DO-WHILE structure
CASE structure
The DO-WHILE structure differs from the WHILE structure in that the process contained within the loop is always executed at least one time. This is equivalent to performing the process once before going into a WHILE loop. In the WHILE structure the process may never be executed. Although the WHILE structure is preferred, the DO-WHILE structure is sometimes more intuitive.
Similarly, the CASE structure is useful in representing a series of IF-THEN-ELSE statements where there are more than two choices to be made. Hence the DECISION blocks are identical except for the choice being compared. For example, the DECISION could be ‘is the color of the sock …’ Each DECISION block would then have a different color as the choice. The true result always flows to the right, with the false result flowing into the next DECISION block. There will always be one less DECISION block than the number of choices.
Figure 3: Derived Flowchart Structures
Writing C code from a structured flowchart is very straightforward. Each type of process structure described previously has a corresponding C code program flow statement. Figures 5 and 6 illustrate the C code implementation of the three basic and two derived process structures. Note that the single process blocks can be implemented with a single line of code, multiple lines of code, a function call, or another group of structured process blocks.
SEQUENCE structure IF-THEN-ELSE structure WHILE structure
x = 10; y = 9*x/5+32; DispTemp(x,y);
if ( x > 5 ) { x = 5; } else { x = 0; }
while ( x < 10 ) { x = 2*x; }
Initialize Celsius temperature
Convert to Fahrenheit
Display temperatures
Limit voltage to 5 volts
Turn voltage off
Is voltage greater than 5?
Double result
Is result less than 10?
No Yes
Yes No
Figure 5: C Code Implementation for Basic Flowchart Structures
DO-WHILE structure
CASE structure
switch (mode) { case 1: /* Display process / break; case 2: / Control process */ break; case 3: red_light = 0; blue_light = 0; break; default: mode = mode + 1; break; }
Execute display process
Execute control process
Turn off lights
Increment mode
Double result
Is result less than 10?
mode equal 1?
mode equal 2?
mode equal 3?
Yes No
Yes No
Yes No Yes No
do { x = 2*x; } while ( x < 10 );
Figure 6: C Code Implementation for Derived Flowchart Structures