This action might not be possible to undo. Are you sure you want to continue?
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.
3. Initialize total to zero Initialize counter to zero Input the first grade while the user has not as yet entered the sentinel
The keywords that are to be used include Do While. with careful indentation tend to foster desirable pseudocode. reset. add. When. etc. compute.. Call . Compute. Call. use the words Generate. display... edit. ...Enddo. calculate. print. . test ..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' 4. multiply.. Words such as set. etc. output. input... As verbs. Return... Case. with (parameters).Endif.. increment.EndCase...EndDo. 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 For looping and selection. Always use scope terminators for loops and iteration. Do Until... sum. Process. If. Return .
Number each instruction. Completeness.Do not include data declarations in your pseudocode. sales tax. Pseudo-code is best understood by looking at examples. Nothing is left out. get price of item get sales tax rate sales tax = price of time times sales tax rate final prince = price of item plus sales tax display final price halt Variables: price of item. We also extract and list all variables used in our pseudo-code. Note the three types of instructions: input (get). We also list all variables used at the end of the pseudo-code. 3. Example #1 . This will be useful when translating pseudo-code into a programming language . 6. final price Note that the operations are numbered and each operation is unambiguous and effectively computable. 3. operations.. This is to enforce the notion of an ordered sequence of . 3.. in this case the reader. Furthermore we introduce a dot notation (e. Pseudo-Code is simply a numbered list of instructions to perform some task. Each instruction should be unambiguous (that is the computing agent. conditional operations. and iterative operations. sales tax rate. process/calculate (=) and output (display) 1. Each example below demonstrates one of the control structures used in algorithms : sequential operations. 4 5. In this course we will enforce three standards for good pseudo code 1. is capable of carrying out the instruction) and effectively computable (do-able). 2.1 come after 3 but before 4) to number subordinate operations for conditional and iterative operations 2.g.Computing Sales Tax : Pseudo-code the task of computing the final price of an item after figuring in sales tax.
In both cases we resume the pseudo-code at line 5. you get paid time-and-a-half for all hours worked over 40. On the basis of the true/false question asked in line 3. 1. if you work more than 40 hours.1 4. Pseudo-code the task of computing gross pay given pay rate and hours worked.2 get quiz grade sum = sum + quiz grade . display gross pay halt variables: hours worked.1 gross pay = pay rate times 40 plus 1.1 4. 2. 4. 1.Computing Weekly Wages: Gross pay depends on the pay rate and the number of hours worked per week. 3. we execute line 3. 3. ray rate. gross pay This example introduces the conditional control structure. get number of quizzes sum = 0 count = 0 while count < number of quizzes 4. 6. Example #3 .1). else gross pay = pay rate times hours worked 4. line 4.Example #2 . 2. However. otherwise if the answer is False we execute the lines subordinate to line 4 (i.1 if the answer is True.Computing a Quiz Average: Pseudo-code a routine to calculate your quiz average. get hours worked get pay rate if hours worked ≤ 40 then 3.e.5 times pay rate times (hours worked minus 40) 5.
3 5. As long as the condition in line 4 is True. 7. . 6. we resume the pseudo-code at line 5. When the condition becomes False. 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. average This example introduces an iterative control statement.4.1 .3. we execute the subordinate operations 4. quiz grade.count. sum . count = count + 1 average = sum / number of quizzes display average halt variables: number of quizzes. This is an example of a top-test or while do iterative control structure.4.