[Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

21
PSEUDOCODE, ALGORITHM AND FLOWCHART Submitted to: Prof. Bernice Abad Submitted by: GROUP 1 Alcantara, Ma. Rhima G. Calonia, Abigail Bernadette S. Lopez, Mareanne A. Maglaya, Mitzi S.

description

brief discussion about flowcharts

Transcript of [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

Page 1: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

PSEUDOCODE, ALGORITHM AND

FLOWCHART

Submitted to:Prof. Bernice Abad

Submitted by:GROUP 1

Alcantara, Ma. Rhima G.Calonia, Abigail Bernadette S.

Lopez, Mareanne A.Maglaya, Mitzi S.

Palma, Camille Aira P.Tee, Andrea May B.

Villafranca, Rachell M.

Page 2: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

Table of Contents

Pseudocode, Algorithm and Flowchart

I. Algorithm and Flowcharta. Algorithmb. Flowchart

II. Flowcharting SymbolsIII. Rules in FlowchartingIV. Examples of Algorithm and FlowchartV. “If-then-else” StatementVI. “For-to-step” StatementVII. “Next” StatementVIII. Nested-IfIX. Pseudocode

Page 3: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

I. Algorithm and Flowcharta. Algorithm

Algorithm is a list of instructions for carrying out some process step by step. Algorithms executed by a computer can combine millions of elementary steps such as additions and subtraction, into a mathematical calculation. When we get to mathematical algorithms, we will have to be much more precise.

Example: How to break the Casanova’s heart?

How to make Graham Balls?

1. In a mixing bowl, combine crushed graham, condensed milk, evaporated milk and cinnamon powder then mix well.

2. Scoop a spoonful of mixture then mold into a ball shapes and roll in sprinkles.

Place into paper cups then refrigerate for at least 3 hours before serving.

b. FlowchartA diagram representing the logical sequence in which combination of

steps or operation is to be performed. The most widely used graphic method for describing computer operations.

Page 4: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

Major Uses of Flowchart1. As a documentation tool – a flowchart aids in communicating the

nature of the operation.2. In program development – the programmer uses flowcharting in

and through every part of his task to:a. Marshall and organize his facts on paperb. Outline problems, logic and solutionsc. Deal systematically with the problem as a wholed. Build, step by step, his own reference documentation and

reminders

To a programmer, a flowcharta. Flowchart is a kind of all purpose toolb. “BLUEPRINT” of a programc. Means of experimenting with various approachesd. Laying out the logice. Flowchart is valuable because it shows a lot at a glancef. Flowchart helps us to see relationship which we had not

previously observed and our over-all logical thinking is greatly improved by flowcharts.

g. Uses the flowchart as his guide for codingh. Visual representation of an algorithm

Example:

Page 5: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

II. Flowcharting Symbolsa. Input/Output Symbol – represents an instruction to an input/output device

b. Processing Symbol – represents group of program instructions that perform processing function of the program such as arithmetic operations, compare & sort

c. Decision Symbol – denotes a point in program where more than one path can be taken; used to document points in the program where alternative paths is possible

d. Preparation Symbol – represents an instruction that will alter program’s course of execution

e. Terminal Symbol – designates the beginning and the end of a program; used in specifying error conditions, such as parity error checks or detection of invalid characters

f. Predefined Process Symbol – represents programmed step not explicitly detailed in the program flowchart; used when a procedure needs to be repeated several times

g. On-page Connector – used to connect one part of a flowchart to another w/o drawing flowlines; denotes an entry or an exit from another part of the flowchart; used to change the reading sequence of a flowchart on the same page; aids in developing a clearer, better, organized, simplified flowchart

h. Flow Direction Indicators – show the direction of processing or data flow

i. Off-page Connector – used to designate entry to or exit from a page when a flowchart requires more than one page; used in complex flowcharts w/c often require detailed diagram

j. Flow Lines – show sequence in w/c flowchart symbols are to be read; indicate an arrowhead if the logic flow is from right to left or from bottom to top; if the logic flow is from top to bottom or from left to right, arrowhead isn’t required

Page 6: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

III. Rules in Flowchartinga. Use only one start and one stop per flowchart.

b. The logic flow of the solution is displayed from top to bottom and from left to right.

c. Use the appropriate symbol for each type of operation.

d. Use connectors when moving to another part of the flowchart rather than lines.

e. Questions in flowcharts are asked in a yes/no format. Complete both the yes side and the no side for each question asked in the flowchart.

f. Do not leave dead-ends--that is, a part of a question unanswered. Be sure to have only one start and only stop--that is, one way in and one way out of the flowchart.

IV. Examples of Algorithm and Flowcharta. The radius of a circle is equal to one unit. Draw a flowchart to compute the area

of the circle and print out the value of the radius and the area. Algorithm:

STEP 1. Define the value of the Radius (R) and PI.STEP 2. Calculate the area of the circle, AREA = PI*R*R.STEP 3. Print out the radius and area.

Flowchart:

a. b.c.

d. e.

f. g. j.h. k.

Page 7: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

b. Given three numbers A, B, C. Draw a flowchart to compute and print out sum, the average, and the product of A, B and C.

Algorithm:STEP 1. Read in the values of A, B, C. (or input)STEP 2. Determine the sum of the three values.STEP 3. Compute the average by dividing the sum by 3.STEP 4. Compute the product of the three values.STEP 5. Print out the computed values (sum, average and product).

Flowchart:

S T A R T

R = 1.0

PI = 3.1416

AREA = PI*R*R

PRINT R, AREA

E N D

Page 8: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

c. The Super Niners Manufacturing Company plans to give a year-end bonus to each employee. Draw a flowchart which will compute the bonus of an employee. Consider the folloinf criteria: If the employee's monthly salary is less than 10,000 pesos, the bonus is 50% of the salary; for employees with salaries greater than 10,000 pesos, the bonus is 10,000. Print out the name and corresponding bonus of the employee.

Algorithm:STEP 1. Read in the employee's name and salary.STEP 2. Test if the employee's salary is less than 10,000 pesos.STEP 3. If the monthly salary is less than 10,000 pesos, the bonus is 50% of the salary. However, if the monthly salary is greater than 10,000 pesos, the bonus is 10,000 pesos.STEP 4. Print out the employee's name and bonus.

Flowchart:

S TA R TREAD

A, B, CSUM = A + B + CAVE =

SUM/3PROD = A*B*CPRINT SUM, AVE,

PRODE N D

Page 9: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

d. Given two numbers X and Y. Draw a flowchart to determine the difference between X and Y. If X-Y is negative, compute R=X+Y; if X-Y is zero, compute R=2X+2Y; and if X-Y is positive, compute R=X*Y. Print out the values of X, Y and R.

Algorithm:STEP 1. Read in the values of X and Y.STEP 2. Subtract Y from XSTEP 3. If the difference between X and Y is negative, compute R=X+Y; if zero, compute R=2X+2Y; and if positive, compute R=X*Y.STEP 4. Print out the values of X, Y and R.

Flowchart:

V. “If-then-else” Statement

START

READ NAM

E, SALA

RY

IS THE

SALARY <10,000?

BONUS = SALARY

* 0.50

BONUS = 10,000A

Y N

PRINT NAME, BON

US

A

END

STARTREAD X,

YDIFF = X-Y

IF DIFF

R = X*Y

R = X+Y

R = 2X+2Y

PRINT X, Y, REND

A

AA

= 0

< 0 > 0

Page 10: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

The if-then-else statement is used to test a true/false expression and to take the THEN action if the statement is TRUE and the ELSE action if the statement is FALSE.

It is composed of a statement number, followed by the keyword IF and a true/false expression followed by another keyword THEN and an action to be performed, and followed by the last keyword ELSE with another action to be taken.

If the true/false statement is true, the THEN action is taken and the ELSE action is disregarded. The THEN action may be a single statement action of any type. If the true/false expression, the ELSE action is taken and the THEN action is disregarded. A line number may be used without a GO TO following the THEN or ELSE.

An IF-THEN-ELSE STATEMENT can usually be nested within other IF-THEN-ELSE STATEMENT, though the logic can be tricky and the results can turn out differently than the programmer may expect.

THE GENERAL FORM OF THE IF-THEN-ELSE STATEMENT:

LINE # IF true/false statement THEN action ELSE action

ILLUSTRATIVE EXAMPLES:

a. 40 IF X<5 THEN K=K+2 ELSE K=K+5b. 50 IF A=1 THEN 100 ELSE 150c. 60 IF Z<0 THEN 200 ELSE 10d. 70 IF DIFF<0.01 THEN PRINT “SMALL” ELSE PRINT “LARGE”e. 80 IF B<C THEN GOTO 95 ELSE PRINT “B>C”f. 90 IF K=(M+5) THEN L=5 ELSE IF K=(M+10) THEN L=6

VI. “For-to-step” StatementThe FOR-TO-STEP statement together with the NEXT statement forms an

alternative loop so that the sequence of statements following the FOR-TO-STEP statement up to the NEXT statement are executed over a specified number times.

The FOR-TO-STEP statement consist of statement number, the keyword FOR and a variable, followed by an expression, and the keywords TO and STEP each followed by an expression.

Since the FOR-TO-STEP statement specifies how times the loop will be executed, it must always be the first statement in the loop. Included in the first statement is a non-subscripted numeric variable, called the running variable, whose value changes each time the loop is determined by specifying initial and final values

Page 11: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

for the running variable. The running variable is always incremented by 1, each time the loop is repeated, until the variable equals the final value. The running variable can however be incremented by some value other than 1 if desired. This is accomplished by adding STEP clause to the statement. The initial value, final value and the increment can be constants, variables or numeric expressions.

THE GENERAL FORM OF THE FOR-TO-STEP STATEMENT:

LINE # FOR variable expression TO expression STEP expression

ILLUSTRATIVE EXPRESSIONS:

a. 10 FOR K=1 TO 100b. 20 FOR L=0 TO 25 STEP 2c. 30 FOR NO=5 TO 50 STEP 5d. 40 FOR X=100 TO 0 STEP -10e. 50 FOR M=0.5 TO 3.8 STEP 0f. 60 FOR J=M TO N STEP Kg. 70 FOR A=N1 TO N2 STEP N3h. 80 FOR F=B/2 TO (A+B) [2] STEP L+1

RULES APPLY IN THE FOR-TO-STEP LOOPa. Placement of the STEP clause is optional if the increment is 1.b. The running variable may appear in a statement within the loop, but

its value must and cannot be changed or altered.c. If the final and initial values of the running variable are equal and the

step size is non-zero, the loop will be executed once.d. The loop will not executed under the following conditions:1. The initial and final running variable values are equal and step size is

zero.2. The final value of the running variable is less the initial value and the

step size is positive.3. The final value of the running variable is greater than the initial value

and the step size is negative.e. Control can be transferred out the loop but not into one. Transfer out

can be accomplished by a GOTO, IF-THEN or ON-GOTO statement.

VII. “Next” Statement

Page 12: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

The NEXT statement is used together with FOR-TO-STEP statement to set up ad execute a program loop. Just as a loop always begins with a FOR-TO-STEP statement, it always ends with a NEXT statement. The complete loop is comprised of all statements included between the FOR-TO-STEP and the NEXT statements, and any number of statements may be placed between the two statements.

It consist of a statement number, followed by the keyword NEXT, followed by a running variable name. This running variable must be the same as the running variable that appears in the corresponding FOR-TO-NEXT statement.

Any number of FOR-TO-STEP loops may be nested. In this case, the innermost NEXT must always use the variable associated with the innermost FOR-TO-STEP and NEXT statements.

The NEXT statement increment the loop variable by the STEP size, and if the variable has not exceeded the final value, control is returned back to the FOR-TO-STEP statement.

Since the loop may be broken to GOTO or similar control statements at any time, the running variable controlling the loop may also be altered in statements other than the NEXT statement.

THE GENERAL FORM OF THE NEXT STATEMENT

LINE # NEXT variable

ILLUSTRATIVE EXAMPLES:

a. 150 NEXT Kb. 160 NEXT Ic. 170 NEXT Xd. 180 NEXT NO

VIII. Nested-If

The nested IF statement is a term used in computer programming to refer to a statement that is inside another IF statement . So when an IF statement has another IF statement as one of its possible processing branches, these IF statements are said to be nested.

“Nesting” IF function increases the flexibility of the function by increasing the number of possible outcomes. This nesting makes it possible for a single formula to take multiple decisions.

Page 13: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

Syntax of Excel Nested If formula:

=IF(Condition_1,Value_if_True_1,IF(Condition_2,Value_if_True_2,Value_if_False_2))

Here, ‘Condition_1’ refers to the condition used in the first IF.‘Value_if_True_1’ will be the result if first IF statement is True.‘Condition_2’ is the condition used in the second IF. The second IF will only come into pictue when the First IF statement results a False value.‘Value_if_True_2’ will be the result if second IF statement is True.‘Value_if_False_2’ will be the result if second IF statement is False.

Example of Nested IF’s in Excel

Example 1:

The company decides to give bonus to its employees. Criteria are as follows: 20% bonus to the North Region Employees, 30% to the South Region Employees, 40% to the East Region Employees and 50% to the West Region Employees.

In this case we can use Excel Nested IF formula to find the bonus for each

employee. The formula can be:

Page 14: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

=IF(B2="North","20%",IF(B2="South","30%",IF(B2="East","40%",IF(B2="West","5

0%", "Region is Invalid"))))

The formula is quite simple, it just checks if ‘B2’ (cell that contains region

details for first employee) is equal to “North”, then value should be 20%, if not

then check if B2 is equal to “South”, if yes then value should be 30%, if not move

on to next IF statement and so on.

Similarly for the second employee the formula would be:

=IF(B3="North","20%",IF(B3="South","30%",IF(B3="East","40%",IF(B3="West","5

0%", "Region is Invalid"))))

In case the Region does not match with any one of the IF conditions then

the output should be “Region is Invalid”. (As we can see in Masatoshi’s case.)

Example 2:

Students with scores below 40 are considered as “Fail”, scores between 41 and 60 are considered “Grade C”, scores between 61 and 75 are considered “Grade B” and scores between 76 and 100 are considered as “Grade A”

In the second example we have a table of students and their scores. Now based on their scores we have to give a grade to the students.

Page 15: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

In this scenario we can use a nested If formula as:

=IF(B2<=40,"Fail",IF(AND(B2>=41,B2<=60),"Grade

C",IF(AND(B2>=61,B2<=75),"Grade B",IF(AND(B2>=76,B2<=100),"Grade A"))))

This formula, again, just checks if B2 (cell containing the score of first

student) is less than or equal to 40, if true then the value should be “Fail” if not

then check the next IF condition and so on.

You can see that in the inner-most IF statement I haven’t used the

‘Value_if_False’ which is alright since all scores fall into a choice. In-case all the IF

conditions in this formula will result into a False value then the formula will

simply return a ‘FALSE’ keyword.

IX. PseudocodePseudocode is an English-like presentation of the steps needed to solve a

problem. It is written with a relaxed syntax that allows students solve a problem at a level that hides the detail while they concentrate on the problem requirements. in other words, it allows students to concentrate on the big picture.

In addition to being an excellent design tool, pseudocode is also language independent. Consequently, students can use the same pseudocode design to implement an algorithm in several different languages.

Flowcharts were the first design tool to be widely used, but unfortunately they do not very well reflect some of the concepts of structured programming. Pseudocode, on the other hand, is a newer tool and has features that make it more reflective of the structured concepts. Unfortunately, the narrative presentation is not as easy to understand and follow.

Advantages of Pseudocode1. Can be done easily on a word processor2. Easily modified3. Implements structured concepts well

Disadvantages of Pseudocode1. It's not visual

Page 16: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

2. There is no accepted standard, so it varies wide from company to company.

How We Store and Access Data1. READ Statement - tells the computer to get value from the input device

and store it in the names memory. The computer stores all program data into memory locations. It is perfectly able to understand like:

READ 19087,80976,10943,80764

but we would have a hard time with it. so we name our storage locations using words that are descriptive to us:

variable names will start with a lower case letter they will no spaces in them additional words in them will start with a capital names must be unique within the program consistent use of names.

2. WRITE Statement - is used to display information on the output device. To display words, enclose them in quotes.

3. ASSIGNMENT Statement- used when we need to compute a value in a program.

Calculation Symbols - to symbolizes the arithmetic operator( ) grouping / divide** or ^ exponent + add* multiply - subtract

SelectionWhen we have to make a choice between actions we almost always base

that choice on a test. The test uses phrases like "is less than" or "is equal to". There is a universally accepted set of symbols used to represent those phrases:

> greater than <= less than or equal to< less than >= graeter than or equal to= equal to < > not equal to

Rules for Pseudocode1. Write only one statement per line.

Page 17: [Group 1 - Written Report] Pseudocode, Algorithm and Flowchart

2. Capitalize initial keyword.3. Indent to show hierarchy.4. End multiline structures.5. Keep statements language independent.

#