Professional Documents
Culture Documents
Programming
Revision
Algorithms, Pseudocode Top-down
Refinement
with looping
Ref Deitel & Deitel
1
Pseudocode -Revision
Develop pseudocode algorithms for each of the following:
Input two numbers and determine and display which (if either) is
the larger of the two numbers.
Input the first number from the keyboard
Input the second number from the keyboard
if the first number is greater than the second number
Print firstNumber
else
if the second number is greater than the first number
Print secondNumber
Else
Print a message stating that the numbers are equal
endif
endif
2
Pseudocode -Revision
Obtain a series of positive numbers from the keyboard, and
determine and display the
sum of the numbers.
Assume that the user types the sentinel value -1 to indicate
“end of data entry.”
Use the while statement.
sum=0
Input a value from the keyboard
While the input value is not equal to -1
add the number to sum
input the next number
Print sum
3
Pseudocode for Powers of 2
Write pseudocode that keeps printing the multiples
(powers) of the integer 2, namely 2, 4, 8, 16, 32, 64,
and so on. The loop should not should not terminate
(i.e., you should create an infinite loop). Implement
this program and evaluate the outcome of execution.
4
Pseudocode for Powers of 2
Allow program to run infinitely
Output in form
n 2^n
1 2
2 4
3 8
5
Code for Powers of 2
6
Top-down Stepwise Refinement
• This typically consists of three phases of pseudoced
development.
• Top-
– a single statement that conveys the programs overall
function.
• First refinement-
– divides the top task into smaller tasks into the general order
that they must occur.
• Second refinement-
– This identifies storage and variables and their initialisation. It
defines the control structures for managing the processing
logic including the termination and timing of any outputs.
– More complex programs will require further refinements. 7
Top-down Stepwise Refinement
Example
Requirement spec: A class of students took a quiz.
The grades (int 0 to 100) are available for input on
an input stream terminated by a sentiner of -1.
Determine the average grade for the class.
Top Refinement:
Determine the class average for the quiz
Second Refinement
Initialize variables
Input, sum up and count the quiz grades
Calculate and print the class average
8
Top-down Stepwise Refinement Example
Third refinement:
Initialize total to zero
Initialize counter to zero
11
Salesperson earnings
Top-down refinement
2)Top:
For an arbitrary number of salespeople, determine
each salesperson’s earnings for the
last week
First refinement:
Input the salesperson’s sales for the week,
calculate and print the salesperson’s wages
for the week,
then process the next salesperson
12
Salesperson earnings
Top-down refinement
Second refinement:
13
Salesperson earnings code
#include <stdio.h>
#include <conio.h>
int main( void )
{
float sales; /* gross weekly sales */
float wage; /* commissioned earnings */
16
do while statement
Use do…while statement to print the numbers from 1
to 10.
The control variable counter is preincremented in the
loop-continuation test.
17
for ( expression1; expression2; expression3 ) statement
18
for ( expression1; expression2; expression3 ) statement
The expressions
counter = counter + 1
counter += 1
++counter
counter++
23
For Examples Factorial n, n!
Second refinement:
Declare variable n and counter x,
print k, tab, k!
for x=1 to n
calcualte k!
print k, k!
endFor
24
For Examples Factorial n, n!
Third refinement:
Declare variable n and counter k,
print n, tab, n!
for k=1 to n
/* calculate x!
for j=1 to k
factorialK=factorialK * k
/* factorialK *= factorialK
endfor
print k, factorialK!
endFor 25