Pseudocode

Pseudocode

Pseudocode is a high-level way of putting your program into logical steps. It
uses structural conventions of programming languages, but is meant to be human
readable. It is used to help describe algorithms instruction by instruction.No
standard for pseudocode syntax exists, as a program in pseudocode is not an
executable program. Pseudocode resembles, but should not be confused with,
skeleton programs which can be compiled without errors. Flowcharts, drakon-
charts and Unified Modeling Language (UML) charts can be thought of as a
graphical alternative to pseudocode, but are more spacious on paper. Languages
such as HAGGIS bridge the gap between pseudocode and code written in programming
languages. Because pseudocode is detailed yet readable, it can be inspected by
the team of designers and programmers as a way to ensure that actual programming
is likely to match design specifications. Catching errors at the pseudocode
stage is less costly than catching them later in the development process.

Examples of pseudocode:

Initialize total to zero
Initialize counter to zero
Input the first grade
while the user has not as yet entered the sentinel
  add this grade into the running total
  add one to the grade counter
  input the next grade (possibly the sentinel)
if the counter is not equal to zero
  set the average to the total divided by the counter
 print the average
else
   print 'no grades were entered'
INITIALIZE Sum to 0
FOR i = 0 to array length - 1
  ADD array[i] to Sum
ENDFOR
WRITE Sum
  
CREATE array c having size = array length a + array length b
SET aIdx to 0
SET bIdx to 0
SET cIdx to 0
WHILE aIdx < length of a - 1 and bIdx < length of b - 1
  IF a [aIdx] < b [bIdx] THEN
    SET c[cIdx] to a[aIdx]
    INCREMENT aIdx
    INCREMENT cIdx
  ELSE
    SET c[cIdx] to b[bIdx]
    INCREMENT bIdx
    INCREMENT cIdx
  ENDIF
ENDWHILE

WHILE aIdx < length of a - 1
  SET c[cIdx] to a[aIdx]
  INCREMENT aIdx
  INCREMENT cIdx
ENDWHILE

WHILE bIdx < length of b - 1
  SET c[cIdx] to b[bIdx]
  INCREMENT bIdx
  INCREMENT cIdx
ENDWHILE

RETURN c
  

Pseudocode comes in all different styles. It could look based off the writer’s
preference. I will be going over how I was taught to create pseudocode and my
thoughts on the process. In later tutorials, we will be writing pseudocode for
popular algorithms used.

My thoughts on pseudocode:

I believe if we are writing pseudocode at the program level, meaning I am
writing a program’s worth of functions, rather then just one single function ,
we should specify variables at the top. Then each function should have its own
block . For an example i will write a simple program to swap integers.

Swap integers:
variables:
  x : integer
  y : integer

functions:

swap:
  a : void*
  b : void*
  create a temporary storage variable temp (void*)
  set temp to x
  set x to y
  set y to temp

main :
  print "input variable 1:"
  read input to x
  print "input variable 2:"
  read input to y
  print "Values before Swap:"
  print "X: " x " Y: " y
  swap address of x, address of y
  print "X: " x " Y: " y

As you can see, i define the functions thoroughly while still maintaining a
level of readable text. I can now convert this into the programming language of
my choice. You should practice writing some pseudocode! You can post what you
come up with on our forums! A post will be made for discussion here : http://csprogrammingcenter.forumotion.com/t2-pseudocode-examples#2

Leave a Reply