Sunday, 1 February 2015

Pseudocode Examples

Pseudocode Examples

Modified 15 December 1999

An algorithm is a procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed. An algorithm is merely the sequence of steps taken to solve a problem. The steps are normally "sequence," "selection, " "iteration," and a case-type statement.
In C, "sequence statements" are imperatives. The "selection" is the "if then else" statement, and the iteration is satisfied by a number of statements, such as the "while," " do," and the "for," while the case-type statement is satisfied by the "switch" statement.


Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is a "text-based" detail (algorithmic) design tool.

The rules of Pseudocode are reasonably straightforward. All statements showing "dependency" are to be indented. These include while, do, for, if, switch. Examples below will illustrate this notion.

GUIDE TO PSEUDOCODE LEVEL OF DETAIL: Given record/file descriptions, pseudocode should be created in sufficient detail so as to directly support the programming effort. It is the purpose of pseudocode to elaborate on the algorithmic detail and not just cite an abstraction.


Examples:
1.
If student's grade is greater than or equal to 60
    Print "passed"
else
    Print "failed"  
endif

2.
  
Set total to zero
Set grade counter to one
While grade counter is less than or equal to ten
    Input the next grade
    Add the grade into the total
endwhile 
Set the class average to the total divided by ten
Print the class average.

3.
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)
endwhile

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' 
endif 

4.
initialize passes to zero
initialize failures to zero
initialize student to one
while student counter is less than or equal to ten
    input the next exam result  
    if the student passed
        add one to passes 
    else 
 add one to failures 
      add one to student counter 
    endif 
endwhile 
print the number of passes
print the number of failures
if eight or more students passed
    print "raise tuition"
endif

5.
Larger example:  

NOTE:  NEVER ANY DATA DECLARATIONS IN PSEUDOCODE

Print out appropriate heading and make it pretty
While not EOF do:
     Scan over blanks and white space until a char is found 
 (get first character on the line)
     set can't-be-ascending-flag to 0
     set consec cntr to 1
     set ascending cntr to 1
     putchar first char of string to screen
     set read character to hold character
     While next character read != blanks and white space
          putchar out on screen
          if new char = hold char + 1
               add 1 to consec cntr
               set hold char = new char
               continue
          endif
          if new char >= hold char 
               if consec cntr < 3 
                    set consec cntr to 1
               endif
               set hold char = new char
               continue
          endif
          if new char < hold char
               if consec cntr < 3
                    set consec cntr to 1
               endif
               set hold char = new char
               set can't be ascending flag to 1
               continue
           endif
     end while
     if consec cntr >= 3 
          printf (Appropriate message 1 and skip a line)
          add 1 to consec total
     endif
     if  can't be ascending flag = 0
          printf (Appropriate message 2 and skip a line)
          add 1 to ascending total
     else
          printf (Sorry message and skip a line)
          add 1 to sorry total
     endif
end While
Print out totals:  Number of consecs, ascendings, and sorries.
Stop


Some Keywords That Should be Used And Additional Points


For looping and selection, The keywords that are to be used include Do While...EndDo; Do Until...Enddo; While .... Endwhile is acceptable. Also, Loop .... endloop is also VERY good and is language independent. Case...EndCase; If...Endif; Call ... with (parameters); Call; Return ....; Return; When;

Always use scope terminators for loops and iteration.

As verbs, use the words Generate, Compute, Process, etc. Words such as set, reset, increment, compute, calculate, add, sum, multiply, ... print, display, input, output, edit, test , etc. with careful indentation tend to foster desirable pseudocode. Also, using words such as Set and Initialize, when assigning values to variables is also desirable.


More on Formatting and Conventions in Pseudocoding


  • INDENTATION in pseudocode should be identical to its implementation in a programming language. Try to indent at least four spaces.
  • As noted above, the pseudocode entries are to be cryptic, AND SHOULD NOT BE PROSE. NO SENTENCES.
  • No flower boxes (discussed ahead) in your pseudocode.
  • Do not include data declarations in your pseudocode.

  • But do cite variables that are initialized as part of their declarations. E.g. "initialize count to zero" is a good entry.


  • Function Calls, Function Documentation, and Pseudocode

  • Calls to Functions should appear as:
      Call FunctionName (arguments: field1, field2, etc.)
  • Returns in functions should appear as:
      Return (field1)
  • Function headers should appear as:
      FunctionName (parameters: field1, field2, etc. )
  • Note that in C, arguments and parameters such as "fieldn" could be written: "pointer to fieldn ...."
  • Functions called with addresses should be written as:
      Call FunctionName (arguments: pointer to fieldn, pointer to field1, etc.)
  • Function headers containing pointers should be indicated as:
      FunctionName (parameters: pointer to field1, pointer to field2, ...)
  • Returns in functions where a pointer is returned:
      Return (pointer to fieldn)
  • It would not hurt the appearance of your pseudocode to draw a line or make your function header line "bold" in your pseudocode. Try to set off your functions.
  • Try to use scope terminators in your pseudocode and source code too. It really hels the readability of the text.

  • Source Code

  • EVERY function should have a flowerbox PRECEDING IT. This flower box is to include the functions name, the main purpose of the function, parameters it is expecting (number and type), and the type of the data it returns. All of these listed items are to be on separate lines with spaces in between each explanatory item.
  • FORMAT of flowerbox should be
      ********************************************************
      Function:   ( cryptic text describing single function
           ....... (indented like this)  
           .......
      Calls:      Start listing functions "this" function calls
           Show these functions:  one per line, indented
    
      Called by:  List of functions that calls "this" function
           Show these functions:  one per line, indented.
    
      Input Parameters:  list, if appropriate; else None
      
      Returns:    List, if appropriate.
      ****************************************************************
    
  • INDENTATION is critically important in Source Code. Follow standard examples given in class. If in doubt, ASK. Always indent statements within IFs, FOR loops, WILLE loops, SWITCH statements, etc. a consistent number of spaces, such as four. Alternatively, use the tab key. One or two spaces is insufficient.
  • Use scope terminators at the end of if statements, for statements, while statements, and at the end of functions. It will make your program much more readable.SPELLING ERRORS ARE NOT ACCEPTABLE

  • No comments:

    Post a Comment