0% found this document useful (0 votes)
124 views36 pages

Algorithm Design and Analysis: Lecturer: M. Sohaib Ajmal

Here is the pseudocode to compute Fibonacci numbers till 50: 1. Define variables current, previous, number and n = 50 2. Set current = previous = 1 3. For i from 3 to n: a) number = current + previous b) previous = current c) current = number 4. Print current This uses the property that each Fibonacci number is the sum of the previous two numbers to iteratively compute the sequence up to the 50th term.

Uploaded by

Sohaib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views36 pages

Algorithm Design and Analysis: Lecturer: M. Sohaib Ajmal

Here is the pseudocode to compute Fibonacci numbers till 50: 1. Define variables current, previous, number and n = 50 2. Set current = previous = 1 3. For i from 3 to n: a) number = current + previous b) previous = current c) current = number 4. Print current This uses the property that each Fibonacci number is the sum of the previous two numbers to iteratively compute the sequence up to the 50th term.

Uploaded by

Sohaib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

LECTURE 2

ALGORITHM DESIGN AND ANALYSIS

Lecturer: M. Sohaib Ajmal


A computer is a blank canvas
Waiting for you to instruct.
Plan out with pseudocode
Then program to construct.
How does the
game know to
collect these?
How does the
game know this
will destroy?

How does the


game make
Mario grow?

How does the


How does the game know to
game know to break this?
walk on this?
How do I get Minion Stuart to move to
D1?

A B C D

4
Move right 3 squares
How do I get Minion Stuart to move to D1 then
to D4?

A B C D

4
Move right 3 squares
Move down 3 squares
How do I get Minion Stuart to move to D1 then
to D4, then move to A4 and finally to A1?

A B C D

3 Move right 3 squares


Move down 3 squares
4 Move left 3 squares
Move up 3 squares
What have we just done
 You have created a series of instructions to solve a given problem

 This is called an Algorithm

 When we write it in a list of instructions it is called Pseudocode

 Computer Programmers use pseudocode to help plan out the code


they will need.
 For game making

 Creating websites

 Control software – robots / machinery

 ANYTHING where planning is needed = pseudocode is used to


layout the tasks/actions
Learning Objectives
 Identify the sequence of actions to solve a
problem

 Determine the order of a sequence to correctly


solve a problem

 Demonstrate the use of pseudocode to solve a


problem
Algorithm Solution written in
pseudocode
1. Move forward 100
2. Turn right 90 degrees
3. Move forward 100
4. Turn right 90 degrees
5. Move forward 100
6. Turn right 90 degrees
7. Move forward 100
8. Turn right 90 degrees
CHECKPOINT

What have we done so


far?

• Created an series of actions


to solve a given problem.
• This is called an
Algorithm

• Written the list of actions in


a numbered sequence
• This is called
Pseudocode
In your workbooks I want you to think about how to get Minion
Stuart to move around the squares and collect the bananas.
You will need to move him and also add actions needed to pick
up the items.
Your
A B C D
Turn
1

2
Extension = Can
you add a
‘question to say if
3 the minion
reaches a
banana?
4
Algorithm Solution written in
pseudocode
1. Move right 2 squares
A B C D 2. Pick up banana
3. Move right 1 square
1 4. Move down 1 square
5. Pick up banana
2
6. Move left 2 squares
7. Pick up banana
3
8. Move down 1 square
9. Move right 1 square
4
10. Pick up banana
11. Move down 1 square
12. Move left 2 squares
13. Pick up banana
Can you ask a question?
 When we think about the way Minion Stuart
moves across could we ask a question as he
moves from one side to the other?

1. Move across 3 squares


2. If you reach a banana
a. Pick it up Ask the question – try and
start it with an ‘IF’

Ask the question – try and


start it with an ‘IF’
Can we ask a question?

A B C D

4
Possible algorithm in
pseudocode
1. Move right 3
A B C D squares
2. IF Minion reaches
1
a banana
2 a. THEN pick it up
3. IF Minion reaches
3
an apple
4 a. THEN leave it
This would continue
This form of pseudocode would help a game
designer plan out the code they would need to to cover the whole
write to create it.
board
Finally

Can you tell me in your own


words what pseudocode is?
Algorithms
• The word algorithm comes from the name of a Persian
mathematician Abu Ja’far Mohammed ibn-i Musa al
Khowarizmi.

• In computer science, this word refers to a special


method useable by a computer for solution of a problem.
The statement of the problem specifies in general terms
the desired input/output relationship.

• For example, sorting a given sequence of numbers into


nondecreasing order provides fertile ground for
introducing many standard design techniques and
analysis tools.
The problem of sorting
Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Example of Insertion Sort
Exercise: Create an array of size 10 and assign random values to
each element of the array and print.
The random values are generated by another function which we
do not implement but it is just
invoked to complete our need
 Int arr;
 arr= new int[10];
 For( int i=0; i<arr.size; i++) {
 Arr[i] = random();
 Print(arr[i]);
 }
 Example: Compute Fibonacci numbers till
50.
Int n, current, previous, number
If(n<2) {
print (n);
} if(n== 2) {
print (n-1);
} else {
current= previous= 1;
for (int i=3; i<n; i++) {
number= current + previous;
previous= current;
current= number;
}
print (number);
}

You might also like