Sei sulla pagina 1di 5

Algorithms and flowcharts

Algorithms and flowcharts


Introduction
An algorithm is a step-by-step procedure for carrying out a particular task. Such a task may consist of baking a cake or preparing a salad (a recipe), building an model airplane, getting to a particular destination, deciding whether an applicant is qualified to join a university course or calculating an employees monthly salarie. In computing, algorithms are a particularly useful tool for designing computer programs. Since computers can understand and carry out only a small number of simple instructions, complex tasks (such as games, wordprocessors, etc.) and even straightforward ones (such as finding the largest number in a list) must be broken down in terms of these simple instructions. There are various ways of expressing an algorithm: Plain English and structured English as found instruction manuals meant to be read by humans. Pseudocode - similar to structured English but contains some elements used in computer programming such as arithmetic symbols, and input/ouput commands Flowcharts diagrams made up of symbols connected by arrows Programming language algorithms written using one of the many programming languages available are meant to be followed (executed) by a computer As an example of these various techniques, consider the task of finding the area of a rectangle: To find the area, first measure the length and width of the rectangle and then multiply them together. Plain English 1. Measure the length and width of the rectangle 2. Multiply the length by the width to obtain the area. Structured English 1: Get side A 2: Get side B 3: Calculate Area = A x B
Pseudocode int a, b, area; Keyboard.readInt(a); Keyboard.readInt(b) area = a * b; println (area); Programming language (java)

Start Input A Input B Area = A*B Output Area Stop

Flowchart

J. Borg

Algorithms and flowcharts

Flowcharts
There are six main symbols used in flowcharts. These are shown in the table below. Symbol name Terminal
START STOP

Symbol

Description Used to indicate the beginning and end of the flowchart The rectangle indicates a process or calculation, such as: adding or multiplying two numbers together. This diamond-shaped symbol contains a question having a yes or no answer (or true or false). Depending on the answer, the corresponding arrow is followed. Note that the yes and no arrows are clearly marked

Process
do something

Decision
?
yes no

Procedure
task

This symbol contains a task whose exact details are not shown in the flowchart, perhaps it has been described in some other flowchart already. The parallelogram specifies that the computer must accept some input, or output some result. This symbol is used to connect two pieces of a flowchart if it does not fit on a single page (or to avoid drawing lots of arrows)

Input/Output

Input

Output

Connector
A

Example 1 1. Flowchart for a program to calculate the area of a square

START

Input side L
Calculate Area

A=LxL

Output area A

STOP

J. Borg

Algorithms and flowcharts

Example 2 2. Flowchart for a program to find the smaller of two numbers:


START

Input first number: N1

Input second number: N2

Output N2 is larger

no

yes N1 > N2 ?

Output N1 is larger

STOP

Condition operators (for decision symbols) Operator A=B A != B or not (A = B) or !(A = B) A>B AB or A >= B Meaning equal to not equal to Read as is A equal to B? is A not equal to B?

higher than higher than, or equal to

is A higher than B ? is A higher than, or equal to B ?

A<B AB or A =< B

lower than lower than, or equal to

is A lower than B ? is A lower than, or equal to B ?

Note that conditional statements can be combined together using AND and OR functions: (A = B) AND (A > 0) This is read as: is A equal to B, and is A higher than 0?.

J. Borg

Algorithms and flowcharts

Exercises Basic 1. Draw a flowchart for a program that asks the user to enter the length of the radius of a circle. Then, the program calculates the circumference (C) and area (A) of the circle, and outputs the results. Draw a flowchart for a program that asks the user to enter two numbers, N1 and N2. The program will then find the average (AVG) of the numbers and display on the screen. Draw a flowchart for a program that asks the user to enter an amount (D) dollars. The program will then convert the dollar amount to euros (E) using the rate of 0.71 ($1 = 0.71), and display the result. Draw a flowchart for a program that asks the user to enter her year of birth (YOB). The program will then calculate the users age.

2.

3.

4.

Decision 5. Use a flowchart to design an algorithm that checks whether a number (NUM) entered by the user is between 0 and 99. If this is the case, the algorithm outputs OK; otherwise, an ERROR message is displayed. Membership to a club is available only to over 13 year olds. Design a flowchart for a program that asks the user to enter the year of birth (YOB) of a prospective member, calculates her age (as in exercise 4) and then outputs a message stating whether she is eligible to join the club. Draw a flowchart for a program that: Asks the user to enter a mark M between 0 and 100 Outputs a grade according to the following specifications: Mark (input) 0 49 50-79 80-100 Grade (output) C B A

6.

7.

8.

Draw a flowchart for a program that asks the user to enter the year and then outputs whether the year is a leap year. Note: a particular year is a leap year if it is exactly divisible by 4 (leaving no remainder). E.g.: 1996, 2004 and 2008 are leap years. 1998, 2002 and 2010 are not leap years. Hint: Use the mod operator - %

9.

Draw a flowchart for a program that prompts the user to enter two numbers, N1 and N2. The program will then decide whether N2 is a factor of N1, and output the result accordingly. For example, if the user enters 9 for N1 and 3 for N2, the program outputs 3 is a factor of 9. On the other hand, if the user enters 10 for N1 and 4 for N2, the program outputs 4 is not a factor of 10. Hint: use the mod operator - %
4 J. Borg

Algorithms and flowcharts

10. What is the purpose of the following algorithm?

11

Use the following symbols to draw a flowchart for a program that asks the user to input two numbers (N1 and N2) and outputs one of the following:
1 START

N1 is larger N2 is larger Numbers are equal


2
Input first number: N1

3 N1 = N2 7
Output Numbers are equal

4
Output N1 is larger

5
Input second number: N2

6 N1 > N2 ?

8 STOP

9
Output N2 is larger

J. Borg

Potrebbero piacerti anche