Examples of PseudoCode, 12/30/2005. R. Roggio

Reasonably Good examples of Pseudo Code. These were taken from COBOL courses more advanced than COP2120. But you can see the style and level of design.

Note that the pseudo code is reasonably language independent! This is VERY important.

We use words like LOOP … End Loop; Set, Increment, Initialize, and words that are not necessarily tied to an implementing language.

1. Another real good function; Indentation good. Level of detail good.

Note also the "generalization of the CASE statements."

This is how to do such a thing...

A few additional examples of pseudocode that is too vague.

Compute(parameters: pointer to array, pointer to stat struct, state counter)

Initialize counter to 0.

Loop while counter <= state counter -1

Switch to compute element based on its region number

Case New England Region

Increment total counter

Add state population to total population

Increment New England counter

Add state population to New England Total Population

End Case

Case Mid Atlantic Region

...... <Infer details from New England Case stmts.

End Case

Case South Region

......

End Case

Case Midwest Region

......

End Case

Case Southwest Region

......

End Case

Case West Region

......

End Case

End Switch

End Loop

Calculate average state population <== TOO VAGUE AGAIN. Show HOW!

If New England counter is > 0

Compute New England average population <== TOO VAGUE. How???

End If

If Mid Atlantic counter is > 0

Compute Mid Atlantic average population DITTO...

End If

If South counter is > 0

Compute South average population

End If

If Midwest counter is > 0

Compute Midwest average population

End If

Collapse these!

If Southwest counter is > 0 Add a comment, which

Compute Southwest average population would be sufficient.

End If

If West counter is > 0

Compute West average population

End If

end Compute

======

2. See comments....

Excellent indentation. Good levels of abstraction. Only a couple

of problems...a couple of vague statements...

Good scope terminators

Sort_Array(parameters: pointer to temp struct, pointer to array, state counter)

Loop while A counter is <= state counter -1

Loop while B counter is <= A counter

Compare state abbreviation strings <= Vague... HOW to compare?

If element 2 string has a greater value than element 1 string

Copy element 1 into temp structure;

Copy element 2 into element 1's position in array

Copy temp structure into element 2's position in array

End If

End Loop

End Loop

Loop while A counter is <= state counter -1

Loop while B counter is <= A counter

Compare region number <== to what?

If element 2 has a greater region value than element 1

Copy element 1 into temp structure;

Copy element 2 into element 1's position in array

Copy temp structure into element 2's position in array

End If

End Loop

End Loop

end Sort_Array

======

3. Another well done routine....

See one comment herein.

Output(parameters: pointer to array, pointer to state struct, state counter)

Set Number region to 1

Set region counter equal to first element in array's region number

Loop while variable <= state counter -1

If array element region number does not equal region counter

Increment Number region

Set region counter equal to current elements region number

End If

End Loop

Compute average regional population

Loop while header counter <= Number region

Switch to decide which header is to be printed

Case region number = 1

Print New England Header

End Case

Case region number = 2

Print Mid Atlantic Header

End Case

Case region number = 3

Print South Header

End Case

Case region number = 4

Print Midwest Header

End Case

Case region number = 5

Print Southwest Header

End Case

Case region number = 6

Print West Header

End Case

End Switch

While region number is = element region number

Print detail lines

End While

Switch to decide which summary to print

Case region number = 1

Print New England Summary WHAT IS INCLUDED IN THE

End Case SUMMARY??? (TELL ONE

TIME IN PSEUDOCODE.

Case region number = 2

Print Mid Atlantic Summary

End Case

Case region number = 3

Print South Summary

End Case

Case region number = 4

Print Midwest Summary

End Case

Case region number = 5

Print Southwest Summary

End Case

Case region number = 6

Print West Summary

End Case

End Switch

Advance screen with user input

End Loop

Print grand totals

end Output

======