Download presentation

Presentation is loading. Please wait.

Published byMisael Hance Modified about 1 year ago

1
Introduction to Programming Algorithms Andreas Savva

2
2 What is an algorithm? An Algorithm is a detailed sequence of simple steps that are needed to solve a problem. 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. It is a step-by-step solution to a problem.

3
3 Mathematical Example Problem: What is the value of x in the following equation? Problem: What is the value of x in the following equation? 5x – 10 = 30 Algorithm (Solution): Algorithm (Solution): Step 1:5x = Step 2:5x = 40 Step 3:x = 40/5 Step 4:x = 8

4
4 Making a cake Step 1: Preheat oven to 200 degrees. Step 1: Preheat oven to 200 degrees. Step 2: Mix the eggs with the sugar. Step 2: Mix the eggs with the sugar. Step 3: Add the butter, flour, and the rest of the ingredients. Step 3: Add the butter, flour, and the rest of the ingredients. Step 4: Stir for 5 minutes. 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 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 6: Keep it in the pan for 15 minutes to cool down. Step 7: Serve with vanilla ice-cream. Step 7: Serve with vanilla ice-cream.

5
5 Algorithms Algorithm Step 1: Step 1: Move 3 squares and turn right. Step2: Step 2: Move 1 square and turn left. Step3: Step 3: Move 2 squares and turn left. Step4: Step 4: Move 2 squares and turn right. etc. The Problem The Problem: How will the robot go to the file folder?

6
6 Representation of Algorithms Verbal representation Verbal representation Pseudo-code Pseudo-code Flowchart Flowchart

7
7 Verbal representation of Algorithms Example: Write an algorithm to read and calculate the average of three numbers. Example: Write an algorithm to read and calculate the average of three numbers. Algorithm: 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.

8
8 Flowcharts - graphical representation of an algorithm Flow Start – Finish Input – Output Processing Choice Statements (Question, Decision) Sub-programs (Separate flowchart) Comments

9
9 Flowchart Start Read a Read b Read c Sum a + b + c Average Sum / 3 Display Average Finish G R abcabc Sum Average

10
10 Structures For the solution of a problem we use three basic structures: For the solution of a problem we use three basic structures: Sequential structure Sequential structure Conditional structure Conditional structure Repetitive structure Repetitive structure

11
11 Example 2 Write an algorithm to read two numbers and display the appropriate message if the two numbers are equal or not. Write an algorithm to read two numbers and display the appropriate message if the two numbers are equal or not. 1. Read a. 2. Read b. 3. If a = b then display that the two numbers are equal. 4. Else display that the two numbers are NOT equal.

12
12 Start Read a Read b Display: ”The two numbers are not equal” Finish a = b ? Display: ”The two numbers are equal” TRUEFALSE ConditionalStructure

13
13 Example 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. 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 = = 25 a = 3, b = 7, Sum = = 25 a = 1, b = 100, Sum = … = 5,050 a = 1, b = 100, Sum = … = 5, Read a, b 2. Sum 0 3. Counter a 4. If Counter > b then Goto 8 5. Sum Sum + Counter 6. Counter Counter Goto 4 8. Display Sum

14
14 Start Read a, b Finish i > b ? TRUE FALSE Sum 0 i a Display Sum Sum Sum + i i i + 1 RepetitiveStructure i is the Counter

15
15 Exercises Design a flowchart for the following problems: Design a flowchart for the following problems: 1.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. 2.Given 4 numbers X1, X2, X3, and X4, find and display the smaller one. 3.Given two numbers A and B. Calculate and display the remainder of their division B:A. (Hind: Keep subtracting A from B until you find a smaller number than A. i.e. for the division 20:6 it will be 20 – 6 = 14; 14 – 6 = 8; 8 – 6 = 2; so Remainder = 2).

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google