Sie sind auf Seite 1von 26

CSC115

Chapter 2: Algorithm Discovery and Design


Overview:
¾ Develop the idea of algorithm and
algorithmic problem solving
¾ Come up with a formal way of representing
algorithms.
¾ Apply the algorithmic ideas to problems
that are of interest to Computer
Scientists.
¾Searching lists
¾Finding maximum and minimum
¾Matching patterns
CSE115 1
Representing Algorithms
¾How do we represent/specify
algorithms?
3 Options:
¾Natural language
¾Formal high-level Programming Language
¾Something in between

CSE115 2
Representing Algorithm
¾ Natural Language:
¾ Write algorithms the way we speak or write in our
everyday life
¾ Unstructured, free flowing
¾ Effective for essays, horrible for algorithms
¾ Why natural language is not suitable?
¾ Extremely verbose which makes the algorithm
unstructured and because of the lack of structure it
becomes difficult to locate specific sections of an
algorithm
¾ Too rich in interpretation which makes it ambiguous
¾ Does not provide the required accuracy and precision for
algorithms

CSE115 3
Algorithm for adding two m-
digit numbers

CSE115 4
Algorithm in Natural Language
¾ Example: The addition algorithm
Initially set the value of the variable carry to 0 and the value of
the variable i to 0. When these initiations have been completed,
begin looping until the value of the variable i becomes greater
than m-1. First add together the values of the two digits ai and bi
the current value of carry digit to get the result called ci Now
check the value of ci to see whether it is greater than or equal to
10. If ci is greater than or equal to 10, then reset the value of
carry to 1 and reduce the value of ci by 10; otherwise, set the
value of carry to zero. When you are done with that operation,
add 1 to i and begin the loop all over again. When the loop has
completed execution, set the leftmost digit of the result cm to the
value of carry and print out he final result, which consists of the
digits cm cm-1…c0. After printing the result, the algorithms is
finished and it terminates.
CSE115 5
Representing Algorithms
¾ Formal High-Level Programming Language (C, C++,
Pascal):
¾ The algorithm will eventually be implemented in a
computer; so write it in the language the computer
understands.
¾ Problems:
¾ During the initial phase of the design, the language should
be at a highly abstract level. But Programming language
immediately deals with specific details such as punctuation,
grammar and syntax.
¾ Instead of writing “set the value of carry to 0” in C++ its written as
Carry = 0; this may arise different questions like is this
instruction setting Carry to 0? or asking if Carry is equal to 0?
Why there is a semicolon at the end of the instruction etc.
¾ The semicolons and capitalization formalities of the
programming language hampers the thought process of a
computer scientist

CSE115 6
Algorithm in Programming Language
¾ Example: The addition algorithm
{
int i, m, Carry;
int a[100], b[100], c[100];
cin >> m;
for (int j = 0; j <= m-1; j++)
cin >> a[j] >> b[j];
Carry = 0;
i = 0;
while ( i < m)
{
c[ i ] = a[ i ] + b[ i ] +| Carry; if (c[ i ] >= 10) {
…….
…….

CSE115 7
Representing Algorithms
¾ Natural languages are not suitable
¾ Neither are formal programming languages
¾ Look for something in the middle
¾ Pseudocode
¾ A special set of English language constructs
modeled to look like the statements available in
most programming languages.
¾ Simple, highly readable and no fixed
grammatical rule
¾ Very flexible to adapt our own personal way of
thinking and problem solving
¾ Example: Set the value of carry to 0

CSE115 8
Pseudocode
¾ Pseudocode is used to design and represent
algorithms
¾ Pseudocode constructs will be used to
define sequential, conditional and
repetitive (loop) operations
¾ 3 basic parts of Sequential operations:
¾ Computation
¾ Input
¾ Output

CSE115 9
Pseudocode (Sequential Instr.)
¾ Sequential Operations (Computation)
¾ The instruction for performing a computation and saving the
result is as follows:
Example: Set the value of “variable” to “arithmetic expression”
set the value of carry to 0
¾ Evaluate the “arithmetic expression” (Arithmetic expression means solving the
arithmetic operation( +,-,/,* etc.)), get the result and store it in the indicated
variable
¾ Variable:
¾ A named storage location that can hold a data value
¾ It’s like a mailbox where you can store a value and from which you
can retrieve the value back
¾ Example:
¾ Set the value of ci to (ai + bi + carry)
¾ Set the value of i to i+1 or
¾ Add a and b to get c
¾ Set the value of c to a+b
¾ Pseudocode is not a precise set of notational rules to be
memorized and rigidly followed
CSE115 10
Pseudocode (Sequential Instr.)
¾ Sequential Instructions (Input & output)
¾ Input operations allow the computing agent to receive data
values from the outside world that it may then use in later
instructions.
Input
Outside Computing
World Agent
Output

¾ Output operations allow the computing agent to send


results to the outside world for display. Example:
¾ Input: Get values for “variable”, “variable”…
¾ Get the value of r, the radius of the circle
¾ Output: Print the values of “variable”, “variable”..
¾ Print the value of the area
¾ Print the message “Sorry, no answers were computed”

CSE115 11
A simple Algorithm with sequential
operation
¾ Compute Average Miles per gallon (ver. 1)

1 Get values for gallons used, starting mileage, ending mileage


2 Set the value of distance driven to (ending mileage – starting
mileage)
3 Set the value of average miles per gallon to (distance driven /
gallons used)
4 Print the value of average miles per gallon
5 Stop

CSE115 12
Pseudocode (Conditional and
Iterative)
¾ Control Operations: Conditional and
Iterative operations are together called
control operation. It allows us to alter the
normal sequential flow of control in an
algorithm.
¾ Conditional Operations & Iterative Operations
¾ Sequential algorithms are straight line algorithms
¾ Follows a straight line of execution from top to
bottom and stops
¾ But Real world problems require branching
¾ Conditional statements are “question asking”
operations
¾ Iterative operations are Repetition of a block of
instructions
¾ The algorithmic primitive used for expressing the
idea of iteration is called looping

CSE115 13
Pseudocode (Control Instr.)
¾ Conditional Statements
¾ Allows the algorithm to ask a question and take
an action based on the answer
¾ The most common conditional primitive is
if/then/else
If “ a true/false condition” is true then
first set of algorithmic operations
Else (or otherwise)
second set of algorithmic operations

CSE115 14
Pseudocode (Control Instr.)
¾ Conditional Statements (Example)
If (ci >= 10) then
set the value of ci to (ci -10)
set the value of carry to 1
Else (or otherwise)
set the value of carry to 0

CSE115 15
Example of If/then/Else
¾ Average Miles per Gallon Algorithm (ver. 2)
1 Get values for gallons used, starting mileage, ending mileage
2 Set the value of distance driven to (ending mileage – starting mileage)
3 Set the value of average miles per gallon to (distance driven / gallons
used)
4 Print the value of average miles per gallon
5 If average miles per gallon is greater than 25.0 then
6 Print the message ‘You are getting good gas mileage.’
Else
7 Print the message ‘You are not getting good gas mileage.’
8 Stop

CSE115 16
Pseudocode (Iterative Operations)
¾Iterative Operations (Loops)
¾ Repetitive execution of a block of instructions
¾ The real power of computer comes from doing
the same calculation many, many times
¾ The algorithmic primitive that we use to
express the idea of iteration is called looping
¾ Example:
while (“a true/false condition”) do step i through step j
step i: operation
step i+1: operation
.
.
step j: operation
CSE115 17
Iterative Operations
¾ Looping
¾ Two main part in a loop
¾ Loop Body (block of operations of a loop)
¾ Continuation Condition: Controls the execution of the
loop
¾ If the continuation condition is true, the loop body will be
executed
¾ If the continuation condition is false, execution of the
loop body terminates and the algorithm proceeds to the
statement immediately following the loop.
¾ Infinite loop: When the continuation condition never
becomes false.
¾ It is a fundamental violation of algorithm properties

CSE115 18
Loop Example
¾Counting the squares
1 Set the value of count to 1
2 While (count<=100) Repeat step 3 to step 5
3 Set square to (count X count)
4 Print the values of count and square
5 Add 1 to count
¾The condition is checked at the end of
each loop body execution
¾If step 5 is not there and the algorithm
specifies the loop to continue over step
3 and 4 only, what will happen?
CSE115 19
Variation of Loops
¾ The line numbers of the loop body can be omitted by using
“End of the Loop” construct. Example

While (“a true/false condition”)


Operation
……
…….
Operation
End of the loop

¾ The loop body is delimited not by explicit step numbers but


by the two lines that read, “While….” and “ end of the loop”
¾ This is known as pretest loop because the continuation
condition is tested at the beginning of the loop and
therefore it is possible for the loop body never to be
executed.

CSE115 20
Example of the pretest loop
• Set integer counter to 1
• While (counter<=10)
• Print counter
• Add 1 to counter
• End of the loop
• stop

CSE115 21
Posttest loop
¾ In the posttest loop, the true/false continuation condition is
checked at the end of the loop body. This version of the
iteration first performs all the algorithmic operations
contained in the loop body. Only then does it evaluate the
true false condition specified at the end of the loop. If this
condition is false then the loop is finished and if it is true
then the entire loop body is executed again
¾ In this iteration the loop body must be executed at least
once . Example
Do
Operation
Operation
………
While (“a true/false conditions”)
End of the loop
CSE115 22
Example of the posttest loop
• Set integer counter to 1
• Do
• Print counter
• Add 1 to counter
• While (counter<=10)
• End of the loop
• stop
CSE115 23
example

CSE115 24
Summary of Pseudocode Instructions
¾ Sequential Operations
¾ Computation
Set the value of “variable” to “arithmetic expression”
¾ Input/Output
Get the value for “variable”, “variable” …..
Print the value of “variable”, “variable” …..
Print the message ‘message’ …..
¾ Conditional Operations
If “a true/false condition” is true then
first set of algorithmic operations
Else
second set of algorithmic operations

CSE115 25
Summary of Pseudocode Instructions
¾ Iterative Operations
º while “a true/false condition” is true Repeat step i through step j
step i: operation
..
step j: operation
º pretest loop: while “a true/false condition” is true Repeat
operation
..
operation
End of the loop
º Posttest loop; Do
Operation
Operation
………
While (“a true/false conditions”) is false
End of the loop
CSE115 26

Das könnte Ihnen auch gefallen