Title: Introduction to Programming
1Introduction to Programming
Andreas Savva
2What is an algorithm?
- An Algorithm is a detailed sequence of simple
steps that are needed to solve a problem. - It is a step-by-step solution to a problem.
3Mathematical Example
- Problem What is the value of x in the following
equation? - 5x 10 30
- Algorithm (Solution)
- Step 1 5x 30 10
- Step 2 5x 40
- Step 3 x 40/5
- Step 4 x 8
4Making a cake
- Step 1 Preheat oven to 200 degrees.
- Step 2 Mix the eggs with the sugar.
- Step 3 Add the butter, flour, and the rest of
the ingredients. - Step 4 Stir for 5 minutes.
- Step 5 Put the mixture into a baking pan and
cook it in a 175 degrees for 40 minutes. - Step 6 Keep it in the pan for 15 minutes to cool
down. - Step 7 Serve with vanilla ice-cream.
5Algorithms
Algorithm Step 1 Move 3 squares and turn
right. Step 2 Move 1 square and turn left. Step
3 Move 2 squares and turn left. Step 4 Move 2
squares and turn right. etc.
The Problem How will the robot go to the file
folder?
6Representation of Algorithms
- Verbal representation
- Pseudo-code
- Flowchart
7Verbal representation of Algorithms
- Example Write an algorithm to read and calculate
the average of three numbers. - Algorithm
- Step 1 Ask for the first number.
- Step 2 Ask for the second number.
- Step 3 Ask for the third number.
- Step 4 Add the three numbers to find their sum.
- Step 5 Divide the sum by 3.
- Step 6 Display the result.
8Flowcharts- graphical representation of an
algorithm
Start Finish
Input Output
Processing
Sub-programs (Separate flowchart)
Choice Statements (Question, Decision)
Comments
9Flowchart
Start
Read a
Read b
Read c
Sum ? a b c
Average ? Sum / 3
Display Average
Finish
10Structures
- For the solution of a problem we use three basic
structures - Sequential structure
- Conditional structure
- Repetitive structure
11Example 2
- Write an algorithm to read two numbers and
display the appropriate message if the two
numbers are equal or not. - Read a.
- Read b.
- If a b then display that the two numbers are
equal. - Else display that the two numbers are NOT equal.
12Start
Conditional Structure
Read a
Read b
a b ?
TRUE
FALSE
Display The two numbers are not equal
Display The two numbers are equal
Finish
13Example 3
- Write an algorithm to read two numbers a and b
and find the sum of all the numbers between a and
b inclusive. i.e. - a 3, b 7, Sum 3 4 5 6 7 25
- a 1, b 100, Sum 1 2 3 100 5,050
- Read a, b
- Sum ? 0
- Counter ? a
- If Counter gt b then Goto 8
- Sum ? Sum Counter
- Counter ? Counter 1
- Goto 4
- Display Sum
14Start
Repetitive Structure
Read a, b
Sum ? 0 i ? a
i gt b ?
TRUE
FALSE
Sum ? Sum i i ? i 1
Display Sum
Finish
15Exercises
- Design a flowchart for the following problems
- Calculate the weekly salary of an employee, when
the employee-name, working-hours, and
rate-per-hour are given. If the working-hours
exceed 40, the rate-per-hour is double than the
normal hour. - Given 4 numbers X1, X2, X3, and X4, find and
display the smaller one. - Given two numbers A and B. Calculate and display
the remainder of their division BA. (Hind Keep
subtracting A from B until you find a smaller
number than A. i.e. for the division 206 it will
be 20 6 14 14 6 8 8 6 2 so
Remainder 2).