Structured Design & Programming

In this class, you will learn how to design and implement a problem solution in a computer using modular programming approach. We believe that it is easier to design a solution for a problem if, first, the problem is broken down into smaller, simpler subproblems with the relationship of the subproblems well defined; then solution to each subproblem is designed individually.

 Given a problem, you begin with an analysis of the problem at hand. You carefully look at the requirements of the problem and make sure all questions are answered before you begin the design of the modules and define their interactions. We will use structure chart to accomplish this. The next step is to develop the algorithms for each subproblem. We will use pseudo code or, a more fancy name, program design language (or PDL) to express the steps in an algorithm. We will discuss these two design tools next.

Structure Chart

Using the technique of (functional) decomposition, you break down the problem into subproblems and solve each subproblem with one or more modules (which will be implemented individually as procedures and/or functions in a particular high-level programming language). Typically, most problem can be broken down into input, process, and output.

To visualize the relationships between the modules and communications between mainly the main module (or the driver) and modules, we use a structure chart to show logical relationship of the modules. We use basically the following symbols :

 

Let us look at an example. Click Here 


Next step will be to write the algorithm for each module individually guided by the structure chart. This will be discussed in the next section. Note the directions of the data arrows in the structure chart are VERY IMPORTANT.

Program Design Language

Here is a set of PDL (Program Design Language) statements that each of you must use in writing your algorithm:

GET (a list of variables)

Semantic:Retrieve from an input device a set of values, one for each of the variables in the list.

 For example,

GET (INCODE, RATE, YEAR)

The statement means to retrieve from an input device three appropriate values (no indication if the values are all integers, or one real, one integer,... etc.) and store the first value in INCODE, the second in RATE, and third YEAR. There is no implication of how those values are grouped. The values may appear as one value per record (line), or two values in one record and the third in another.

We are making an assumption that whatever values needed are retrieved correctly by the GET statement. This assumption is important because without correct data, what follows next is no longer relevant. 


DISPLAY (a list of variables/literal strings)

Semantic: Display on an output device the values of the variables in the list and/or the literal strings.

 For example,

DISPLAY (RATE, YEAR)

The semantic for the above statement is for the computer to display to the output device the value stored in RATE followed by the value stored in YEAR.

 or

DISPLAY ('The rate is ', RATE)

Display to the output device the character/literal string The rate is followed by the value contained in the variable RATE


Assignment Statement

or Identifier <-- an arithmetic expression

Semantic:Store the value returned by the expression to the identifier (variable) on the left.

 For example,

SUM <-- 0

initialize SUM to integer zero.

SUM <- FIRST

copy the value of FIRST into SUM. A destructive copy.

 

SUM <- SUM + 1

increment the value of SUM by one and store the result in SUM.

PAY <- RATE * HOURS * 1.5

Take the value stored in RATE and multiplied it by the value...


Loop ... EndLoop

Semantic: Repeat the set of the statements between the Loop... and the EndLoop statement until a condition is met or not met. There are a variety of ways in expressing how the loop should be terminated.

    With WHILE condition
    LOOP WHILE not EndOfFile
      statement1
      statement2
      ...
    ENDLOOP

    With UNTIL condition
    LOOP UNTIL InputValue is greater than 10
      statement1
      statement2
      ...
    ENDLOOP

    With number of TIMES
    LOOP 4 TIMES
      statement1
      statement2
      ...
    ENDLOOP

    With VARYING
    LOOP VARYING Index FROM 3 TO(DOWNTO) 10(2)
      statement1
      statement2
      ...
    ENDLOOP
 
    With EXIT
    LOOP
       statement1
       ...
      EXIT when condition;
      statement2
      ...
    ENDLOOP


IF condition THEN statements ENDIF

Semantic: If the condition is evaluated to be true, perform the statements.

 For example,

    IF ( InputValue is less than Sum ) THEN
       statement1
       statement2
       ...
    ENDIF

Compare the value stored in InputValue with the value stored in Sum, perform statement1 and so on only if the less than condition is satisfied (true). 


IF condition THEN statements1 ELSE statements2 ENDIF

Semantic: If the condition is evaluated to be true, perform statements1. If the condition is evaluated to be false, perform statements2.

 For example,

    IF ( InputValue is less than Sum ) THEN
       statement1
       statement2
       ...
    ELSE
       statement1n
       statement2n
       ...
    ENDIF
Comments and suggestions send to schu@rucs. The above were developed based on what Bill Collins had used in CPSC 124 before.

PDL Exercises

  1. Add 1 to NUMBER OF FEMALES
  2. Add CURRENT RATING to TOTAL OF RATINGS
  3. Multiply RATE by TIME to get DISTANCE
  4. Initialize MOST RESPONSES to zero
  5. Swap th evalues of CURRENT HEIGHT and PREVIOUS HEIGHT.
  6. Get the next two numbers from the input and store them in the variables HEIGHT and WEIGHT, respectively.
  7. Get the next three numbers from the input and store them in the variables SCORE1, SCORE2 and SCORE3, respectively; and find the average of the three scores to put in AVERAGE.
  8. Read in and write out all of the ages in the input
  9. Add each number from 1 to 7 to SUM OF NUMBERS.
  10. Compare CURRENT SALE plus PREVIOUS SALE to
  11. HIGHEST TOTAL; replace HIGHEST TOTAL by that sum if the sum is larger than HIGHEST TOTAL.
  12. Print out the smaller of AMOUNT WITHHELD and TAX DUE. ( If they are equal, print out either.)
  13. Read in a number each time into NUMBER until the number -999 is encountered; print out the average of all numbers (except -999, the trip number or sentinel)

Answers

NOTE: Knowing the answers is not sufficient. YOU MUST understand why it is done that way. Some exercises have more than one answer but only one is shown here. 


    1. Add 1 to NUMBER OF FEMALES

 NumberOfFemales <-- NumberOfFemales + 1 


    1. Add CURRENT RATING to TOTAL OF RATINGS

 TotalOfRatings <-- TotalOfRatings + CurrentRating 


    1. Multiply RATE by TIME to get DISTANCE

 Distance <-- Rate * Tim 


    1. Initialize MOST RESPONSES to zero

 MostResponses <-- 0 


    1. Swap th evalues of CURRENT HEIGHT and PREVIOUS HEIGHT.

 TempHeight <-- CurrentHeight
CurrentHeight <-- PreviousHeight
PreviousHeight <-- TempHeight 


    1. Get the next two numbers from the input and store them in the variables HEIGHT and WEIGHT, respectively.

 Get ( Height, Weight ) 


    1. Get the next three numbers from the input and store them in the variables SCORE1, SCORE2 and SCORE3, respectively; and find the average of the three scores to put in AVERAGE.

 Get ( SCORE1, SCORE2, SCORE3 )
Sum <-- SCORE1 + SCORE2 + SCORE3
Average <-- Sum / 3


    1. Read in and write out all of the ages in the input

 

Get ( age )
Loop While not end of file 
&blank.  Display ( age )
  Get ( age )
EndLoop

    1. Add each number from 1 to 7 to SUM OF NUMBERS.

 

SumOfNumbers <-- 0
Number <-- 0
Loop 7 times
   Number <-- Number + 1
   SumOfNumbers <-- SumOfNumbers + Number
EndLoop

    1. Compare CURRENT SALE plus PREVIOUS SALE to HIGHEST TOTAL; replace HIGHEST TOTAL by that sum if the sum is larger than HIGHEST TOTAL.

 

TotalSale <-- CurrentSale + PreviousSale
If ( TotalSale > HighestTotal ) then
   HighestTotal <-- TotalSale
Endif

    1. Print out the smaller of AMOUNT WITHHELD and TAX DUE. ( If they are equal, print out either.)

 

If ( AmountWithheld > TaxDue ) then
   display ( TaxDue )
else
   display ( AmountWithheld )
endif

    1. Read in a number each time into NUMBER until the number -999 is encountered; print out the average of all numbers (except -999, the trip number or sentinel)

 

Sum   <-- 0
Count <-- 0
Get ( Number )
Loop While Number not equal to -999 
   Sum <-- Sum + Number
   Count <-- Count + 1
   Get ( Number )
EndLoop
Average <-- Sum / Count
Display ( 'The Average is ', Average )

Never, never write it like this (even though both achieve the same):

X <-- 0
Y <-- 0
Get ( W )
Loop While W not equal -999
   X <-- X + W
   Y <-- Y  + 1
   Get ( W )
EndLoop
U <-- X / Y
Display ( 'The Average is ', U )

If you have more, let me know. The above exercises were given to me by Bill Collins.

Examples

Example #1

Suppose that a professor gave a quiz to her class and compiled a list of scores ranging from 50 through 100. She intends to use only three grades: A if the score is 90 or above, B if it is below 90 but above or equal to 75, and C if it is below 75. She would like a program to assign the appropriate letter grades to the numeric scores.

 Design a structure chart and the algorithms for the above problem.

Main Module
-----------
     GetScores (Score)

By S.C. Chu

Documentation Guidelines

Each program must be documented. Here is a list of the minimum documentation guideliens that you need to put in every Ada program:

    1. A program header that includes the purpose of the program, the name of the author, and the date the program is written.
    2. Each procedure/function must have a header that includes the name of the procedure, the purpose of the procedure, and a list of the input and the output parameters with their purposes.

For example:

   -------------- P R O C E D U R E    procedureName  ----------------
 
     Purpose:  
         
          This procedure retrieves from input data file
             three integer numbers and stores them in
             CokeID, CokeInit, Sprite.
 
     INPUT Parameters:
          Number     IN      integer     Number of brand names
      
     OUTPUT Parameters:
          CokeID     OUT     integer     Stores the ID of Coke brand
          CokeInit   OUT     integer     Initial inventory of Coke brand
          CokeID     OUT     integer     Stores the ID of Sprite brand
   
     Author       : S. Chu
     Date Written : December 18, 1995.
 
     --------------------------------------------------------------------

Putting a header in a procedure does not exclude any documentation within the procedure body. The above is shown not to be followed exactly format-wise but to be followed content-wise.

    1. The purpose of each variable, constant, and type must be described. No one-letter identifiers or cryptic abbreviations are allowed. For example, if the variable is for Loan Balance, then it should not be named LB.
    2. Every set of statements that accomplish a major step must be documented.
    3. All loops must be documented with the purpose of the loop. Statements within the loop must be indented properly.
    4. All if's must be documented for the true and/or false conditions.
    5. Indentation must be used to make the program more readable. For example, this is not acceptable:

 

 IF LoanAmount <> LoanBalance THEN
 LoanAmount := LoanBalance + 1;
LoanBalance:= LoanAmount * 10.0;
end
 ELSE
Count := Count + 1;
end if;

The IF statement should be properly indented similar to this:

   IF LoanAmount <> LoanBalance THEN
      LoanAmount := LoanBalance + 1;
      LoanBalance:= LoanAmount * 10.0;
   ELSE
      Count := Count + 1;
   ENF IF;

Date Created : April 4, 1998
Last Update : Wednesday, September 30, 1998 at 02:00:49 PM
© S. Chu, Radford University