Algorithm Programming
Algorithm Programming
MCA-102(N2)
Programming In C 1
What is an algorithm?
… a well-defined procedure that allows an agent
to solve a problem.
Example algorithms
Cooking a dish
Making a peanut-butter jelly sandwich
Shampooing hair
Programming a VCR
Making a pie
Programming In C 2
Example
Is this an algorithm?
An algorithm must:
Programming In C 4
Example
Problem: Find and print the 100th prime number
A prime number is a whole number not evenly divisible
by any other number other than 1 and itself
Algorithm (?):
1. Generate a list of all primi numbers L1, L2, L3,
…
2. Sort the list into ascending order
3. Print out the 100th element in this list
Is this an algorithm?
Programming In C 5
Algorithm for Programming
a VCR
Step 1: If the clock and calendar are not correctly set, then go
to page 9 of the instruction manual and follow the instructions
before proceeding
Step 2: Place a blank tape into the VCR tape slot
Step 3: Repeat steps 4 through 7 for each program that you
wish to record, up to a maximum of 10 shows
Step 4: Enter the channel number that you wish to record, and
press the button labeled CHAN
Step 5: Enter the start time and press TIME-START
Step 6: Enter the end time and press END-TIME
Step 7: This completes the programming of one show. If you
do not wish to program anything else press END-PROG
Step 8: Press te button labeles TIMER. Your VCR is ready to
record.
Programming In C 6
Types of Operations
Basic operations
Wet hair
Rinse
Turn on VCR
Conditional operations
If batter is too dry add water
Repeat/looping operations
Repeat step 1 and 2 three times
Repeat steps 2,3,4,…10 until batter becomes
soft.
Programming In C 7
Algorithm
How to come up with an algorithm?
That is, how to discover an algorithm
underlying a problem?
Problem solving
Programming In C 8
Example
Problem: Given two positive integers, compute their greatest
common divisor
Euclid’s algorithm:
Step 1: Get two positive integer values from the user
Programming In C 9
Coming up with
algorithms..
How do people think????
Puzzle:
Before A, B, C and D ran a race they made the following
predictions:
A predicted that B would win
B predicted that D would be last
C predicted that A would be third
D predicted that A’s prediction would be correct.
Only one of these predictions was true, and this was the
prediction made by the winner.
In what order did A, B, C, D finish the race?
Programming In C 10
Example
Problem: Adding two n-digit numbers
7597831 +
1287525
-------------------
8885356
Programming In C 12
Pseudocode
Pseudocode = English but looks like programming
Good compromise
Simple, readable, no rules, don’t worry about punctuation.
Programming In C 13
Pseudocode
Basic (primitive) operations
Read the input from user
Conditional operations
Execute an operation if a condition is true
Repeat operations
Execute a block of operation multiple times
until a certain condition is met
Programming In C 14
Variables
Variable
A named memory location that can store a value
Think of it as a box into which you can store a
value, and from which you can retrieve a value
Examples:
i M
Example of operations
Set the value of i to 3
Set the value of M to i*3 + 12
Set the value of i to i+10
Programming In C 15
A model for visualizing an algorithm
Algorithm
Variables
Operations
Programming In C 16
Primitive operations
Get input from user
Get the value of x from user
Programming In C 17
Example 1
Problem: For any three numbers input by the user,
compute their sum and average and output
them
Programming In C 18
Example 2
Problem: Given any value of radius from the user,
compute and print the circumference of a circle
with that radius
Algorithm in pseudocode:
variables: r, c
1. Get the value of r from user
2. Set c to 2 * pi * r
3. Print “The circumference of your circle is “ c
Programming In C 19
What is a flowchart?
Logic diagram to describe each step
that the program must perform to
arrive at the solution.
Programming In C 20
Continue flowchart
Programmer prepares flowchart
before coding.
Most common flowchart symbols are:
Logic
Direction Beginning/ Input/output comparison data
of end of a task operation operations. manipulation
logic flow Have one operations
entry and
e.g arithmetic
two exit
points
Programming In C 21
Purpose of Flowcharting:
An aid in developing the logic of a
program.
Verification that all possible
conditions have been considered in a
program.
Provides means of communication
with others about the program.
A guide in coding the program.
Documentation for the program.
Programming In C 22
Example
Olive Peach has a fruit stall in the local
market
She wants a program which will calculate a
customer’s bill, given the cost per kilo of the
fruit purchased and the number of kilos bought
The program should then print out the bill
This problem will only deal with one purchase
per customer
Input will be from the keyboard; output will be
to the screen using appropriate headers.
Programming In C 23
Solution (1)
Analyse
identify the problem
Calculate the cost of fruit, given cost per kilo and the number of
kilos bought
Design a solution (Do this on paper)
A typical purchase might be bananas:
Cost per kilo – 90c
Kilos bought - 2
Total cost of purchase = 90 * 2 = €1.80c
Programming In C 24
Solution (2)
Design the output to be displayed
Olive Peach – Fruit Merchant
The Old Market
Cost per kilo = $0.90c
Kilos bought = 2
Total cost = $1.80c
Sketch the interface design (form)
Implement your solution in VB
Debug and test
Document each stage
Programming In C 25
Olive Peach: Flow
Diagram
Start
Get cost
per kilo
Get
weight
Calculate cost
Display bill
Programming In C 26
Desk Checking
The process of testing the flowchart with
different data as input, and checking the
output.
Programming In C 27
What is a Pseudocode?
A program design technique that
uses English words.
Has no formal syntactical rules.
Programming In C 28
Olive Peach: Pseudo
Code (1)
Steps
1 Get cost per kilo
2 Get weight (in kilos)
Programming In C 29
Olive Peach: Pseudo
Code (2)
Steps
1 Get cost per kilo
1.1 Prompt user to enter cost
1.2 Read in the cost from the keyboard
2 Get weight
2.1 Prompt user to enter weight
2.2 Read in the weight from the keyboard
3 Calculate total cost
3.1 Set total cost equal to weight multiplied by cost
4 Display the bill
4.1 Display bill headings
4.2 Display bill details (the cost, weight and total cost)
Programming In C 30
What is a Hierarchy
Chart?
Shows the overall program’s
structure.
Describes what each part, or module,
of the program does.
Also how each module relates to
other modules in the program.
Programming In C 31
Example of Hierarchy
Chart:
Olive Peach
program
Calculate
Get cost Get weight Display bill
total cost
per kilo
Programming In C 32
More than 1 item: Flow
Chart Start
Initialize item=1
total cost = 0
No
Are there more items
Yes
Get next cost per kilo
Calculate cost
Increment item
Programming In C 33
More than 1 item
:Pseudocode
Determine the total cost if more than
one item:
Do while there are more items
Get the next cost per kilo
Get the next weight
Calculate the cost
add it to total cost
Increment items
Loop
Display total cost
Programming In C 34