Sei sulla pagina 1di 74

LOGICAL PROBLEM SOLVING

FIND THE MISSING NUMBER


 5 – 4 = 23
8 – 1 = 63
6 – 16 = 32
9 – 9 = 78
3–9=6
3 – 81 = ?
WHAT IS THE DIFFERENCE BETWEEN
ALGORITHM, PROGRAM, PSEUDOCODE AND
FLOWCHART?
PROBLEM SOLVING
• Every problem consist of mainly three parts:
Input, Processing and Output

• Example: Add two numbers


• Steps
1. Read/Input two numbers
2. Add these two numbers
3. Display/Output the result of addition
ALGORITHM
 An Algorithm is a solution to a problem that is
independent of any programming language
 An Algorithm is
a finite sequence of steps
 each step shall be clearly stated and
understandable
 for each input, it shall terminate in
finite time with output
Program
Program:
• A set of instruction written in a programming
language that a computer can execute so that
the machine acts in a predetermined way.
ALGORITHMS & PROGRAMS

 An Algorithm is a solution to a problem that is


independent of any programming language.

While

 A program is an algorithm expressed using a specific


set of instructions from any programming language.
Algorithm Example
• Maximum of two numbers
Steps:
1.Read/input two numbers
2.Compare two numbers
3.Print the Greater number

• Average of three numbers


Steps:
1.Read/input three numbers
2.Add three numbers
3. Divide the sum by 3.
4.Print the result of division
Pseudocode

– Pseudocode consists of short readable and


formally-styled natural language used to
explain specific tasks within a program's
algorithm

– Mixture of English language statement


and a programming language (like C)
code
INTRODUCTION TO
FLOWCHARTING
FLOWCHART
 A graphical representation of the sequence of
operations in an information system or program
FLOWCHART (CONTD..)
A Flowchart
 shows logic of an algorithm
 emphasizes individual steps and their
interconnections
 e.g. control flow from one action to the next
FLOWCHART SYMBOLS
Name Symbol Use in Flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made.


The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Flow line Denotes the direction of logic flow in the program


Rounded
START Rectangle

BASIC FLOWCHART SYMBOLS Display


message “How
many hours
did you work?”

 Notice there are three Read Hours

types of symbols in Display

this flowchart:
message “How
much do you Parallelogr
get paid per am
 rounded rectangles hour?”

 parallelograms Read Pay Rate

 a rectangle
Multiply

 Each symbol Hours by Pay


Rectangle Rate. Store
result in
represents a different Gross Pay.

type of operation. Rounded


Display Gross
Pay
Rectangle
END
START Terminal

BASIC FLOWCHART SYMBOLS Display


message “How
many hours
did you work?”

 Terminals Read Hours

 represented by Display
message “How
rounded rectangles much do you
get paid per
 indicate a starting or hour?”

ending point Read Pay Rate

Multiply
Hours by Pay
START Rate. Store
result in
Gross Pay.

Display Gross
Pay
END Terminal
END
START

BASIC FLOWCHART SYMBOLS Display


message “How
many hours
did you work?”

 Input/Output Read Hours

Operations Display
message “How Input/Outp
 represented by much do you
get paid per ut
parallelograms hour?” Operation

 indicate an input or Read Pay Rate

output operation Multiply


Hours by Pay
Display Rate. Store
result in
message “How Gross Pay.
Read Hours
many hours Display Gross
Pay
did you
work?” END
START

BASIC FLOWCHART SYMBOLS Display


message “How
many hours
did you work?”

 Processes Read Hours

 represented by Display
message “How
rectangles much do you
get paid per
 indicates a process such hour?”

as a mathematical Read Pay Rate

computation or variable
Multiply
assignment Process
Hours by Pay
Rate. Store
Multiply Hours result in
by Pay Rate. Gross Pay.

Store result in Display Gross


Pay
Gross Pay.
END
Stepping START

Through
STEPPING the
THROUGH THE Display
message “How
Output
Operation

FLOWCHART
Flowchart
many hours
did you work?”

Read Hours

Variable Contents: Display


message “How
Hours: ? much do you
Pay Rate: ? get paid per
hour?”
Gross Pay: ?
Read Pay Rate

Multiply
Hours by Pay
Rate. Store
result in
Gross Pay.

Display Gross
Pay

END
Stepping START

Through
STEPPING the
THROUGH THE Display
message “How

FLOWCHART
Flowchart
many hours
did you work?”

Input Read Hours


Operation
(User types Display
40) message “How
much do you
get paid per
hour?”

Read Pay Rate

Multiply
Hours by Pay
Rate. Store
Variable Contents: result in
Hours: 40 Gross Pay.

Pay Rate: ? Display Gross


Gross Pay: ? Pay

END
Stepping START

Through
STEPPING the
THROUGH THE Display
message “How

FLOWCHART
Flowchart
many hours
did you work?”

Read Hours

Display
message “How
Output much do you
Operation get paid per
hour?”

Read Pay Rate

Multiply
Hours by Pay
Variable Contents: Rate. Store
result in
Hours: 40 Gross Pay.
Pay Rate: ? Display Gross
Gross Pay: ? Pay

END
Stepping START

Through
STEPPING the
THROUGH THE Display
message “How

FLOWCHART
Flowchart
many hours
did you work?”

Read Hours
How
much do
you get
Display
paid per
message “How
hour? 20 much do you
get paid per
hour?”

Input Read Pay Rate


Operation
(User types Multiply
20) Hours by Pay
Variable Contents: Rate. Store
result in
Hours: 40 Gross Pay.
Pay Rate: 20 Display Gross
Gross Pay: ? Pay

END
START

STEPPING THROUGH THE Display

FLOWCHART
message “How
many hours
did you work?”

Read Hours

Display
message “How
much do you
get paid per
hour?”

Read Pay Rate

Multiply
Process: The Hours by Pay
Rate. Store
Variable Contents: product of
result in
Hours: 40 40 times 20 Gross Pay.
is stored in
Pay Rate: 20 Gross Pay Display Gross
Gross Pay: 800 Pay

END
START

STEPPING THROUGH THE Display

FLOWCHART
message “How
many hours
did you work?”

Read Hours

Display
message “How
much do you
get paid per
hour?”

Read Pay Rate

Multiply
Hours by Pay
Rate. Store
Variable Contents: result in
Hours: 40 Gross Pay.
Pay Rate: 20 Output Display Gross
Gross Pay: 800 Operation Pay

END
FOUR FLOWCHART STRUCTURES
 Sequence
 Decision

 Repetition

 Case
SEQUENCE STRUCTURE
a series of actions are performed in
sequence
 The pay-calculating example was a
sequence flowchart.
EXAMPLE
 Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
EXAMPLE
 Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudocode:
 Input the length in feet (Lft)
 Calculate the length in cm (Lcm) by multiplying
LFT with 30
 Print length in cm (LCM)
SOLUTION
Algorithm Flowchart

 Step 1: Input Lft


START
 Step 2: Lcm  Lft x 30
 Step 3: Print Lcm Input
Lft

Lcm  Lft x 30

Print
Lcm

STOP
EXAMPLE
Write an algorithm and draw a
flowchart that will read the two sides
of a rectangle and calculate its area.
Pseudocode
 Input the width (W) and Length (L) of a rectangle

 Calculate the area (A) by multiplying L with W

 Print A

Algorithm
 Step 1: Input W,L
 Step 2: AL x W
 Step 3: Print A
FLOWCHART

START

Input
W, L

ALxW

Print
A

STOP
EXAMPLE
 Writean algorithm and draw a flowchart
that will calculate the roots of a quadratic
equation
ax 2  bx  c  0
EXAMPLE
Pseudocode:
 Input the coefficients (a, b, c) of the quadratic
equation
 Calculate d
 Calculate x1
 Calculate x2
 Print x1 and x2
SOLUTION
START

 Algorithm:
Input
 Step 1: Input a, b, c a, b, c
 Step 2: d  sqrt ( b*b- 4ac )
 Step 3: x1  (–b + d) / (2*a) d  sqrt(b x b – 4 x a x c)

 Step 4: x2  (–b – d) / (2*a) x1 (–b + d) / (2 x a)


 Step 5: Print x1, x2
X2  (–b – d) / (2 x a)

Print
x1 ,x2

STOP
DECISION STRUCTURE
 Oneof two possible actions is taken,
depending on a condition.
DECISION STRUCTURE
 A new symbol, the diamond, indicates a yes/no
question. If the answer to the question is yes, the
flow follows one path. If the answer is no, the
flow follows another path

NO YES
DECISION STRUCTURE
 The flowchart segment below shows how a
decision structure is expressed in C as an if/else
statement.

Flowchart C Code

NO YES if (x < y)
x < y? a = x * 2;
else
Calculate a Calculate a a = x + y;
as x plus y. as x times
2.
EXAMPLE
 Write an algorithm and draw a flowchart to
calculate marks of a student in 4 subjects and
assign the grade as “Pass” if average marks are
greater than 50 otherwise assign “Fail” grade.
SOLUTION
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0

PRINT PRINT
“PASS” “FAIL”

STOP
RELATIONAL OPERATORS

Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
 Greater than or equal to
 Less than or equal to
 Not equal to
EXAMPLE 5
 Write an algorithm that reads two values,
determines the largest value and prints the
largest value with an identifying message.
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX  VALUE1
else
MAX  VALUE2
endif
Step 3: Print “The largest value is”, MAX
FLOWCHART
START

Input
VALUE1,VALUE2

Y is
N
VALUE1>VALUE2

MAX  VALUE1 MAX  VALUE2

Print
“The largest value is”,
MAX

STOP
NESTED IF
 One of the alternatives within an IF–THEN–
ELSE statement
 may involve further IF–THEN–ELSE statement
EXAMPLE
 Write an algorithm that reads three numbers
and prints the value of the largest number.
ALGORITHM
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX  N1 [N1>N2, N1>N3]
else
MAX  N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX  N2 [N2>N1, N2>N3]
else
MAX  N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX
CASE STRUCTURE

 Oneof several possible actions is taken,


depending on the contents of a variable.
CASE STRUCTURE

 Thestructure below indicates actions to


perform depending on the value in
years_employed.
CASE
years_employed

1 2 3 Other

bonus = bonus = bonus = bonus =


100 200 400 800
CASE STRUCTURE

If years_employed = If years_employed =
2, bonus is set to 3, bonus is set to
200 400
If years_employed = If years_employed is
CASE
1, bonus is set to years_employed any other value,
100 bonus is set to 800

1 2 3 Other

bonus = bonus = bonus = bonus =


100 200 400 800
EXAMPLE

 Write an algorithm and draw a flowchart to


a) read an employee name (NAME), overtime hours
worked (OVERTIME), hours absent (ABSENT) and
b) determine the bonus payment (PAYMENT).
EXAMPLE 7 (CONTD..)
Bonus Schedule
OVERTIME – Bonus Paid
(2/3)*ABSENT
>40 hours $50
>30 but  40 hours $40
>20 but  30 hours $30
>10 but  20 hours $20
 10 hours $10
ALGORITHM
Step 1: Input NAME,OVERTIME,ABSENT
Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT ← 50
else if (OVERTIME–(2/3)*ABSENT > 30) then
PAYMENT ← 40
else if (OVERTIME–(2/3)*ABSENT > 20) then
PAYMENT ← 30
else if (OVERTIME–(2/3)*ABSENT > 10) then
PAYMENT ←20
else
PAYMENT ← 10
endif
Step 3: Print “Bonus for”, NAME “is $”, PAYMENT
REPETITION STRUCTURE
 A repetition structure represents part of the
program that repeats. This type of structure is
commonly known as a loop.
REPETITION STRUCTURE
 Notice the use of the diamond symbol. A loop
tests a condition, and if the condition exists, it
performs an action. Then it tests the condition
again. If the condition still exists, the action is
repeated. This continues until the condition no
longer exists.
REPETITION STRUCTURE
 In the flowchart segment, the question “is x < y?”
is asked. If the answer is yes, then Process A is
performed. The question “is x < y?” is asked
again. Process A is repeated as long as x is less
than y. When x is no longer less than y, the
repetition stops and the structure is exited.

YES
x < y? Process
A
REPETITION STRUCTURE
 The flowchart segment below shows a repetition
structure expressed in C as a while loop.

Flowchart C Code

while (x < y)

YES x++;
x < y? Add 1 to
x
CONTROLLING A REPETITION STRUCTURE
 The action performed by a repetition structure
must eventually cause the loop to terminate.
Otherwise, an infinite loop is created.
 In this flowchart segment, x is never changed.
Once the loop starts, it will never end.
 QUESTION: How can this
flowchart be modified so
YES
it is no longer an infinite x < y? Display x
loop?
CONTROLLING A REPETITION STRUCTURE
 ANSWER: By adding an action within the
repetition that changes the value of x.

YES
x < y? Display x Add 1 to
x
A PRE-TEST REPETITION STRUCTURE
 This type of structure is known as a pre-test
repetition structure. The condition is tested
BEFORE any actions are performed.

YES
x < y? Display x Add 1 to
x
A PRE-TEST REPETITION STRUCTURE
 In a pre-test repetition structure, if the condition
does not exist, the loop will never begin.

YES
x < y? Display x Add 1 to
x
A POST-TEST REPETITION STRUCTURE
 This flowchart segment shows a post-test
repetition structure.
 The condition is tested AFTER the actions
are performed. Display x
 A post-test repetition structure always
performs its actions at least once. Add 1 to
x

YES
x < y?
A POST-TEST REPETITION STRUCTURE
 The flowchart segment below shows a post-test
repetition structure expressed in C as a do-while
loop

C Code
Display x
do
{
Flowchart println(“%d”,x);
Add 1 to x++;
x } while (x < y);

YES
x < y?
CONNECTORS
 Sometimes a flowchart will not fit on one page.
 A connector (represented by a small circle) allows
you to connect two flowchart segments.

A
CONNECTORS

•The “A” connector indicates


that the second flowchart A
STAR
segment begins where the first T
segment ends.

END
A
EXAMPLE 7
Draw a flowchart to find the sum of all the even numbers between
1 and 20 inclusive and then display the sum
Is Count %
2=0?
QUESTION FOR PRACTICE

Draw a flowchart using this information to display the grade of steel


taking hardness, content and strength as user inputs.
MODULES
 A program module (such as a function in C) is
represented by a special symbol.
MODULES

STAR
•The position of the module symbol T
indicates the point the module is
executed. Read Input.

•A separate flowchart can be


constructed for the module.
Call
calc_pay
function.

Display
results.

END
MODULE EXAMPLE
ADVANTAGES OF FLOWCHARTS
 Communication
 Effective analysis

 Proper documentation

 Efficient Coding

 Proper Debugging

 Efficient Program Maintenance


LIMITATION OF FLOWCHARTS
 Complex logic
 Alterations and Modifications

 Reproduction

Potrebbero piacerti anche