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.
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.
Here is a set of PDL (Program Design Language) statements that each of you must use in writing your algorithm:
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.
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.
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...
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
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).
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
...
ENDIFComments and suggestions send to schu@rucs. The above were developed based on what Bill Collins had used in CPSC 124 before.
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.
NumberOfFemales <-- NumberOfFemales + 1
TotalOfRatings <-- TotalOfRatings + CurrentRating
Distance <-- Rate * Tim
MostResponses <-- 0
TempHeight <-- CurrentHeight
CurrentHeight <-- PreviousHeight
PreviousHeight <-- TempHeight
Get ( Height, Weight )
Get ( SCORE1, SCORE2, SCORE3 )
Sum <-- SCORE1 + SCORE2 + SCORE3
Average <-- Sum / 3
Get ( age )
Loop While not end of file
&blank. Display ( age )
Get ( age )
EndLoop
SumOfNumbers <-- 0
Number <-- 0
Loop 7 times
Number <-- Number + 1
SumOfNumbers <-- SumOfNumbers + Number
EndLoop
TotalSale <-- CurrentSale + PreviousSale
If ( TotalSale > HighestTotal ) then
HighestTotal <-- TotalSale
Endif
If ( AmountWithheld > TaxDue ) then
display ( TaxDue )
else
display ( AmountWithheld )
endif
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 )
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 )
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)
Each program must be documented. Here is a list of the minimum documentation guideliens that you need to put in every Ada program:
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.
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