Upload
loreen-arnold
View
220
Download
2
Embed Size (px)
Citation preview
ALGORITHMS
PROGRAMMING PAPER 2 AS
PSPD Using C
Design
Slide 2 of 40
What it’s all about
Problem Solving
Algorithm
Pseudo codes
Flowcharts
PSPD Using C
Design
AT THE END OF THIS YOU SHOULD BE ABLE TO : Understand Problem
definitions Solve these using Algorithms Solve these using Flowcharts Be able to identify, define and
understand various programming terms
Slide 3 of 40
PSPD Using C
Design
Slide 4 of 40
Key Terms
You should know what these terms are and when to use them
• program
• pseudocode
• flowchart
• algorithm
PSPD Using C
Design
PROBLEM SOLVING TECHNIQUES
Here you will learn about: What problem solving is The software development method of problem solving using computers
Basic algorithm control structures
The sequence structure The selection structure The repetition structure
Slide 5 of 40
PSPD Using C
Design
PROBLEM SOLVING TECHNIQUES
What do they translate to :
Apply the software development method to solve problems
Difference between the Algorithm & the Flowchart
Knowing about the control structures
Slide 6 of 40
PSPD Using C
Design
WHAT DO YOU DO FIRST
First :
Read the question and understand it
UNDERSTAND THE PROBLEM
What is the objective?
What are the data?
What is the condition?
Is it possible to satisfy the condition?
Is the condition sufficient to determine the unknown?
Slide 7 of 40
PSPD Using C
Design
PROBLEM SOLVING
Second:
Find the connection between the data and the unknown.
Auxiliary problems may be devised if needed.
DEVISING A PLANHave you seen it before?
Or have you seen the same problem in slightly different form?
Do you know a related problem?
Split the problem into smaller, simple sub-problems.
If you cannot solve the proposed problem try to solve first some related problem. Or solve more general problem. Or special case of the problem. Or solve the part of the problem.
Slide 8 of 40
PSPD Using C
Design
PROBLEM SOLVING
Third:
Carry out your plan.
CARRYING OUT THE PLAN
Carrying out your plan of the solution, check each step.
Fourth:
Examine the solution obtained.
WHAT DID YOU DO, THINK BACK
Can you derive the result differently? Can you use the result, or the method, for some other problem?
Slide 9 of 40
PSPD Using C
Design
SLIGHTLY OFF TRACK…
Slide 10 of
40
The software development method
The software development method consists of the following steps:
Requirements specification
Analysis
Design
Implementation
Testing and verification
Documentation
PSPD Using C
Design
ALGORITHMIC PROBLEM SOLVING
Algorithmic problem:
Any problem whose solution can be expressed as a set of executable instructions.
Algorithm:
A well defined computational procedure consisting of a set of instructions, that takes some value or set of values, as input, and produces some value or set of values, as output.
Slide 11 of
40
PSPD Using C
Design
CHARACTERISTICS OF AN ALGORITHM
Slide 12 of
40
Each step of an algorithm must be exact, preciously and unambiguously described.
It must terminate, i.e. it contains a finite number of steps.
It must be effective, i.e.., produce the correct output.
It must be general, i.e.. to solve every instance of the problem.
PSPD Using C
Design
CHARACTERISTICS OF AN ALGORITHM
Slide 13 of
40
An Algorithm is implemented in some programming language.
program = Algorithm + Data Structures.
Data Structures refer to the types of data used and how the data are organized in the program.
An algorithm is usually presented in the form of some pseudo-code, which is a mixture of English statement,some mathematical notations,and selected keywords from a programming language.
PSPD Using C
Design
CHARACTERISTICS OF AN ALGORITHM
Slide 14 of
40
PROBLEM:
You are required to design a complete system which will enable the sum of two values to be calculated.
An Algorithm should emphasize the WHAT’s and not the HOW’s. Consider the problem below:
PSPD Using C
Design
PROBLEM SOLVING
Slide 15 of
40
To grapple with this problem, we have to understand the problem from the human perspective.
A question to ask yourself is this,
“How Would You Calculate the Sum of Two Values?”
PSPD Using C
Design
WHAT ARE VARIABLES?
Slide 16 of
40
Variables are memory locations within the computer which allows pieces of data to be stored.
The word variable comes from the word vary, which means that whatever you place within a variable can be changed.
A variable can be viewed as a container used to store things.
Data (for example, name, age, salary) can be stored in these containers.
PSPD Using C
Design
DEFINING DIAGRAM
Slide 17 of
40
Now that we have an exact idea about how the problem is solved, let us represent this in a clearer manner, using the defining diagram.
Input Processing Output
Value1
Value2
Sum
PSPD Using C
Design
LIST THE STEPS
Slide 18 of
40
The next step is to identify the actual processing steps required to convert the input to become the output.
Input Processing Output
Value1
Value2
Sum1) Read Value1, Value2
2) Calculate Sum
3) Display Sum
PSPD Using C
Design
ALGORITHM DEVELOPMENT
Slide 19 of
40
Once the defining diagram has been developed, the next logical step is to develop the algorithm (which is much more detailed).
Input Processing Output
Value1
Value2
Sum1) Read Value1, Value2
2) Calculate Sum
3) Display Sum
The developed processing steps have to be more detailed in the algorithm.
PSPD Using C
Design
ALGORITHM DEVELOPMENT
Slide 20 of
40
The basic mathematical operators used in algorithms are as follows:-
+ addition
- subtraction
* multiplication
/ division
= assignment
( ) brackets for grouping calculations
PSPD Using C
Design
ALGORITHM DEVELOPMENT
Slide 21 of
40
Example of an algorithm (using pseudocodes) which can be used to carry out the tasks outlined in the defining diagram is as follows:-
1) Read Value1, Value2
2) Calculate
Sum = Value1 + Value2
3) Display Sum
PSPD Using C
Design
PSEUDOCODING
Slide 22 of
40
A Pseudocode language is semiformal, English-like language with a limited vocabulary that can be used to design and describe algorithms.
The pseudocode language can be used for:
Designing algorithms
Communicating algorithms as programs
Implementing algorithms as programs
Debugging logic errors in program
PSPD Using C
Design
PSEUDOCODE FOR THE CONTROL STRUCTURES
Slide 23 of
40
The Sequence Control Structure:
The sequence control structure is a series of steps or statements that are executed in the order in which they are written in an algorithm.
For Example:
read taxable income
read filing status
compute income tax
PSPD Using C
Design
CONT’D
Slide 24 of
40
The Selection Control Structure:
The selection control structure defines two courses of action, depending on the outcome of a condition. A condition is an expression that, when evaluated, computes to either true or false.
Syntax is: if condition
then-part
else
else-part
end-if
PSPD Using C
Design
DECISION MAKING
Slide 25 of
40
Being able to mimic the way the human brain works, the computer also has the ability to make decisions.
Decision making can be represented in pseudocodes using the IF...THEN construct.
IF (expression) THEN::
ENDIF
PSPD Using C
Design
DECISION MAKING
Slide 26 of
40
IF (expression) THEN::
ENDIF
The expression is a comparison between two values which evaluates to either true of false.
Statements are placed here.
PSPD Using C
Design
DECISION MAKING
Slide 27 of
40
Example:-
We are looking for a job which pays more than Rs. 4000.
IF (Salary>4000) THENSay "I Will Take The Job!!"
ENDIF
Example of an Expression
PSPD Using C
Design
DECISION MAKING
Slide 28 of
40
Commonly used relational operators in expressions:-
> Greater Than
< Less Than
= Equals To
< > Not Equals To
>= Greater Than or Equals To
<= Less Than or Equals To
( ) Brackets used for prioritising certain calculations
PSPD Using C
Design
DECISION MAKING
Slide 29 of
40
Since all expressions works out to be either true or false, what the IF..THEN statement represents is a two-state condition.
For example,
A potential employer is waiting for you to give a reply (on the spot) about the job offer with a salary of Rs.2000. Your decision would be to only take a job worth more than RM4000. What would you say?
IF (Salary>4000) THENSay “YES!”
ELSESay “NO!”
ENDIF
PSPD Using C
Design
DECISION MAKING
Slide 30 of
40
Certain conditions may give rise to more than one expression being evaluated. These are known as compound expressions.
Example:-
You are interested in taking up a job which pays more than Rs.4000 and that the company must also provide a credit card.
IF (Salary>4000) And (CreditCard=YES) THENTake Job!!
ENDIF
PSPD Using C
Design
DECISION MAKING
Slide 31 of
40
Compound expressions can be represented using the following operators:-
AND Every expression must evaluate to be true in order for the whole expression to be true.
OR As long as any one of the expression can be true, the entire IF statement will be true.
NOT The inverse (opposite) of the entire expression.
PSPD Using C
Design
DECISION MAKING
Slide 32 of
40
IF statements can be nested, that is, placed within another IF statement.
This is used in situations when the expression is more complex than the simple decisions (as seen earlier).
PSPD Using C
Design
DECISION MAKING
Slide 33 of
40
IF (Salary>4000) And (CreditCard=YES) THENSay “Yes I Will Take The Job!!”
ENDIF
For example, this statement.........
can be represented like this.........IF (Salary>4000) THEN
IF (CreditCard=YES) THENSay “Yes I Will Take The Job!!”
ELSESay “No Credit Card?”Say “Sorry!!”
ENDIFELSE
Say “Not Enough Pay!!”ENDIF
........ whereby more possibilities can be represented.
PSPD Using C
Design
DECISION MAKING
Slide 34 of
40
For good practice...........
IF (Salary>4000) THENIF (CreditCard=YES) THEN
Say “Yes I Will Take The Job!!”ELSE
Say “No Credit Card?”Say “Sorry!!”
ENDIFELSE
Say “Not Enough Pay!!”ENDIF
........ ensure that statements are properly indented to indicate block of statements which belong together.
PSPD Using C
Design
CONT’D
Slide 35 of
40
For Example:
if a is greater than b then
print “A is greater”
else
print “B is greater”
end if
PSPD Using C
Design
CONT’D
Slide 36 of
40
Repetition Control Structure:
The repetition control structure specifies a block of one or more statements that are repeatedly executed until a condition is satisfied.
Syntax is:
while condition
loop-body
end-while
PSPD Using C
Design
ITERATIVE STATEMENTS
Slide 37 of
40
Looping constructs (also known as repetition or iteration constructs) are a kind of construct found in pseudocodes which allows statements (or a group of statements) to be repeated.
The main reason why looping constructs are provided is because most of the problems which we encounter everyday requires some degree of repetition.
PSPD Using C
Design
ITERATION EXAMPLE
Slide 38 of
40
An example of a process which is iterative:-
Processing Pay of teachers by the school is very much an iterative process as the person processing the payroll applies the same calculations for each employee to produce the pay slip.
PSPD Using C
Design
TYPES OF LOOPS
Slide 39 of
40
The looping constructs available in pseudocodes are as follows:-
DOWHILE...ENDDO
FOR…NEXT
REPEAT...UNTIL
PSPD Using C
Design
DO WHILE LOOP
Slide 40 of
40
The format of the DOWHILE...ENDDO construct is shown below:-
DOWHILE (expression):::
ENDDO
Group of statements
An expression which determines whether the loop will continue.
PSPD Using C
Design
FOR LOOP
Slide 41 of
40
The format of the FOR...NEXT construct is shown below:-
FOR (initialze TO expression) STEP increment:::
ENDDO
Group of statements
An expression which determines whether the loop will continue.
PSPD Using C
Design
REPEAT…UNTIL
Slide 42 of
40
The format of the REPEAT...UNTIL construct is shown below:-
REPEAT:::
UNTIL (expression)
Group of statements
An expression which determines whether the loop will continue.
PSPD Using C
Design
LOOPING CONSTRUCTS
Slide 43 of
40
Take a look at the following example:-
You are required to develop a complete system which will allow the total payroll to be calculated.
The system is required to read in the amount to be paid for each employee.
The moment the system receives an input value of -99, the system is required to stop and display the total payroll.
PSPD Using C
Design
LOOPING CONSTRUCTS
Slide 44 of
40
Input Processing Output
Salary Total1) Read Salary
2) Calculate Total
3) Display Total
The Defining Diagram
PSPD Using C
Design
LOOPING CONSTRUCTS
Slide 45 of
40
Algorithm (Using Pseudocodes)
1) Display "Enter Salary"
2) Read Salary
3) Total = 0
4) DOWHILE (Salary<>-99)
Total = Total + Salary
Display "Enter Salary"
Read Salary
ENDDO
5) Display "Total Payroll = ", Total
PSPD Using C
Design
CONT’D
Slide 46 of
40
Example:
Dowhile (income is less than 50000)
print “Enter taxable income;should be
greater than or equal to 50000”
read income
Enddo
PSPD Using C
Design
PROGRAM FLOWCHARTS
Slide 47 of
40
As humans are more inclined towards understanding diagrams and pictures rather than words, pseudocodes tends to become tedious to understand if too lengthy.
Program flowcharts, because they are represented graphically, makes understanding easier.
PSPD Using C
Design
PROGRAM FLOWCHARTS
Slide 48 of
40
The following are the commonly used symbols for drawing program flowcharts.
terminator off-page connector
process storage
decisionmaking
document
input/output connector
arrowheads
PSPD Using C
Design
PROGRAM FLOWCHARTS
Slide 49 of
40
Begin
Read Value1,Value2
CalculateSum = Value1 + Value2
DisplaySum
End
PSPD Using C
Design
PROGRAM FLOWCHARTS
Slide 50 of
40
Begin
Read Amount
End
Amount>20.00?
CalculateActual=Amount * 0.80
CalculateActual=Amount
NOYES
PSPD Using C
Design
FLOWCHARTING
Slide 51 of
40
Another technique used in designing and representing algorithms.
Alternative to pseudocoing
A pseudocode description is verbal, a flowchart is graphical in nature.
Definition:
A flowchart is a graph consisting of geometrical shapes that are connected by flow lines.
PSPD Using C
Design
SEQUENCE STRUCTURE
Slide 52 of
40
Pseudocode: Flowchart:
statement_1
statement_2
------------
statement_n
Statement -1
Statement -2
Statement -n
PSPD Using C
Design
SELECTION STRUCTURE
Slide 53 of
40
Pseudocode: Flowchart:
if condition
then-part
else
else-part
end_if
condition
else-part then-part
truefalse
PSPD Using C
Design
SELECTION STRUCTURE
Slide 54 of
40
Pseudocode: Flowchart:
if condition
then-part
end_if
condition
then-part
true
false
Y
N
PSPD Using C
Design
REPETITION STRUCTURE
Slide 55 of
40
Pseudocode: Flowchart:
while condition
loop-body
end-while
condition loop-body
F
TY
N
PSPD Using C
Design
SUMMARY
Problem Solving– the process of transforming the description of a problem to its solution.
To Solve complex problems, we use computers as a tool and develop computer programs that give us solutions.
A commonly used method for problem solving using computers is the software development method,which consists of six steps.
Slide 56 of
40
PSPD Using C
Design
SUMMARY
An algorithm is a sequence of a finite number of steps arranged in a specific logical order that, when executed, produce the solution for a problem.
A pseudocode language is a semiformal,English-like language with a limited vocabulary that can be used to design and describe algorithms.
Slide 57 of
40
PSPD Using C
Design
SUMMARY
Any algorithm can be described in terms of three basic control structures.They are the sequence,selection and repetition structures.
The top-down stepwise refinement of algorithms is a fundamental problem-solving strategy.
A Flowchart is a graphical representation of an algorithm.
Slide 58 of
40
PSPD Using C
Design
Slide 59 of
40
Quick Review Question
1. State the difference between the Dowhile – Enddo structure and the Repeat – Until structure.
2. Write an algorithm that will display the first hundred even numbers using the Do-While loop.