pseudocode examples

6
Pseudocode Examples 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 imperative. 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. Examples: 1.. If student's grade is greater than or equal to 60 Print "passed" else Print "failed" 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 Set the class average to the total divided by ten Print the class average.

Upload: mukesh-agarwal

Post on 24-Mar-2015

1.907 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Pseudocode Examples

Pseudocode Examples

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 imperative. 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.

Examples:

1.. If student's grade is greater than or equal to 60

Print "passed"else

Print "failed"

2. Set total to zero

Set grade counter to one

While grade counter is less than or equal to ten

Input the next gradeAdd the grade into the total

Set the class average to the total divided by ten

Print the class average.

3.

Initialize total to zero

Initialize counter to zero

Page 2: Pseudocode Examples

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 counterprint the average

else

print 'no grades were entered'

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

print the number of passes

print the number of failures

if eight or more students passed

print "raise tuition"

Some Keywords That Should be Used

Page 3: Pseudocode Examples

For looping and selection, The keywords that are to be used include Do While...EndDo; Do Until...Enddo; 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.

Do not include data declarations in your pseudocode.

Pseudo-Code is simply a numbered list of instructions to perform some task. In this course we will enforce three standards for good pseudo code

1. Number each instruction. This is to enforce the notion of an ordered sequence of ... operations. Furthermore we introduce a dot notation (e.g. 3.1 come after 3 but before 4) to number subordinate operations for conditional and iterative operations

2. Each instruction should be unambiguous (that is the computing agent, in this case the reader, is capable of carrying out the instruction) and effectively computable (do-able).

3. Completeness. Nothing is left out.

Pseudo-code is best understood by looking at examples. Each example below demonstrates one of the control structures used in algorithms : sequential operations,  conditional operations, and iterative operations. We also list all variables used at the end of the pseudo-code.

Example #1 - Computing Sales Tax : Pseudo-code the task of computing the final price of an item after figuring in sales tax. Note the three types of instructions: input (get), process/calculate (=) and output (display)

1.         get price of item

2.         get sales tax rate

3.         sales tax = price of time times sales tax rate

4          final prince = price of item plus sales tax

5.         display final price

6.         halt

 

Page 4: Pseudocode Examples

Variables: price of item, sales tax rate, sales tax, final price

Note that the operations are numbered and each operation is unambiguous and effectively computable. We also extract and list all variables used in our pseudo-code. This will be useful when translating pseudo-code into a programming language

Example #2 - Computing Weekly Wages: Gross pay depends on the pay rate and the number of hours worked per week. However, if you work more than 40 hours, you get paid time-and-a-half for all hours worked over 40. Pseudo-code the task of computing gross pay given pay rate and hours worked.

1.         get hours worked

2.         get pay rate

3.         if hours worked ≤ 40 then

3.1       gross pay = pay rate times hours worked

4.         else

            4.1       gross pay = pay rate times 40 plus 1.5 times pay rate times (hours worked minus 40)

5.         display gross pay

6.         halt

 

            variables:  hours worked, ray rate, gross pay

This example introduces the conditional control structure. On the basis of the true/false question asked in line 3, we execute line 3.1 if the answer is True; otherwise if the answer is False we execute the lines subordinate to line 4 (i.e. line 4.1). In both cases we resume the pseudo-code at line 5.

Example #3 - Computing a Quiz Average:  Pseudo-code a routine to calculate your quiz average.

1.         get number of quizzes

Page 5: Pseudocode Examples

2.         sum = 0

3.         count = 0

4.         while count < number of quizzes

            4.1       get quiz grade

            4.2       sum = sum + quiz grade

            4.3       count = count + 1

5.         average = sum / number of quizzes

6.         display average

7.         halt

 

            variables: number of quizzes, sum ,count, quiz grade, average

This example introduces an iterative control statement. As long as the condition in line 4 is True, we execute the subordinate  operations 4.1 - 4.3. When the condition becomes False, we resume the pseudo-code at line 5.

This is an example of a top-test or while do iterative control structure. There is also a bottom-test or repeat until iterative control structure which executes a block of statements until the condition tested at the end of the block is False.