Presentation is loading. Please wait.

Presentation is loading. Please wait.

ALGORITHM Basic CONCEPTS of Basic Concepts of Algorithm

Similar presentations


Presentation on theme: "ALGORITHM Basic CONCEPTS of Basic Concepts of Algorithm"— Presentation transcript:

1 ALGORITHM Basic CONCEPTS of Basic Concepts of Algorithm
Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences Universiti Teknologi MARA Kedah (e): (u):

2 Previously… A program MUST be systematically and properly designed before coding begins. This design process results in the construction of an ALGORITHM.

3 Algorithm design At the end of this chapter you will be able to;
List the characteristics of an algorithm List the algorithm presentation Differentiate between pseudocode and flowchart Understand how computer executes an algorithm Know how to trace an algorithm Construct basic algorithm using pseudocode and flowchart to solve problems

4 Algorithm Is a sequence of steps that tells how to solve a particular problem. An algorithm must be: Step-by-step method for solving a problem Must be a well defined and well ordered sequence of instruction. All instruction must be able to perform Must produce a result Must have a finite number of steps Most agree that algorithm has something to do with defining generalized processes to get “output” from the “input”.

5 Algorithm & Logical Thinking
To develop the algorithm, the programmer needs to ask: What data has to be fed into the computer? What information do I want to get out of the computer? Logic: Planning the processing of the program. It contains the instructions that cause the input data to be turned into the desired output data.

6 Algorithm Therefore, we can conclude that:
Algorithm a step‐by‐step problem solving process in which a solution is arrived in a finite amount of time OR An algorithm is a finite list of sequential steps specifying actions that if performed result in solution of a specific problem.

7 From Algorithm to Program
a set of instructions which are commands to computers to perform specific operations on data a sequence of instructions describing how to do a task (or process)

8 History of algorithm The word algorithm comes from the name of the 9th century Persian Muslim mathematician Abu Abdullah Muhammad ibn Musa Al- Khwarizmi. The word algorism originally referred only to the rules of performing arithmetic using Hindu-Arabic numerals but evolved via European Latin translation of Al-Khwarizmi's name into algorithm by the 18th century. The use of the word evolved to include all definite procedures for solving problems or performing tasks.

9 Algorithm Representation
Two types of representation: Graphically – flowchart Textual – pseudocode Flowchart Graphical or symbolic representation to present steps in algorithm. Each symbol has a specific meaning containing a short description of a step. The flowchart symbols are linked together with arrows showing flow direction Pseudo code consist of short English phrases used to describe steps in algorithm. Start with start or begin and ends with stop or end

10 Flowchart Symbol and Description
Name Symbol Description Lozenge Indicates the starting or ending of flowchart (start / stop : terminal ) Connector Used to connect break in the flowchart Parallelelogram Use for input and output operation (data) Rectangle Indicates any interval operation (process) Diamond Use for asking questions that can have either TRUE of FALSE / YES or NO (decision / selection) Module Sub solution to solve a part of entire problem Control flow Shows flow direction

11 Flowchart Logic Structure
Sequence Structure Selection Structure Loop Structure DO WHILE (test condition) LOOP (statement) No Yes ELSE (statement) IF (test condition) THEN No Yes statement

12 Sequence Structure With sequence structure, an action or event is performed in order, one after another. A sequence can contain any number of events but there is no chance to branch off and skip any of the events. Once you start a series of events in a sequence, you must continue step‐by‐step until the sequence ends.

13 Sequence Structure – Example 1
To find the area of a rectangle The formulas: area = length * width Flowchart Example Start length, width area = length x width area End Problem definition Data Formula Information Input variable: length width area = length x width Output: area Algorithm Step / Solution: 1. get input 2. calculate area, area = length x width 3. display area

14 Pseudo Code Example OR OR To find the area of a rectangle
The formulas: area = length * width Pseudo Code Example Problem definition Data Formula Information Input variable: length width area = length x width Output: area Start Read length Read width area = length x width Display area of a rectangle End Algorithm Start Input length Input width Calculate area of a rectangle Output area of a rectangle End Step / Solution: 1. get input 2. calculate area, area = length x width 3. display area of rectangle OR Start Prompt the user to enter a length of a rectangle Prompt the user to enter a width of a rectangle Calculate the area of a rectangle Display the area of a rectangle End OR

15 Sequence Structure - Example 2
Problem Design a software to calculate the sum of two numbers entered by user. Data Formula Information Input variable: num1 num2 sum = num1 + num2 Output: sum Algorithm Step / Solution algorithm: 1. get num1, num2 2. calculate sum = num1 + num2 3. display sum

16 Sequence Example Pseudo Code Simple Design Modular Design
Start Read num1 Read num2 Calculate sum sum = num1 + num2 Print sum End Modular Design Start get_input() calculate_sum() display_output() End Module get_input() Read num1 Read num2 Return Module calculate_sum() sum = num1 + num2 Module display_output() Print sum

17 Sequence Example Flowchart Simple Design Modular Design Sub-Module
Start start Read num1, num2 sum = num1 + num2 Print sum end Start Read num1 Read num2 Return get_input() Start calculate_sum() calculate sum display_output() Return Start End Print sum Return

18 Why Algorithm is Important?
Describe the steps needed to perform a computation Important for writing efficient code code that execute faster & which uses less memory

19 Tracing an algorithm The purpose to trace an algorithm is to understand how computer executes the given algorithm. We also can see what will happen in the computer memory and what is the result or information of the algorithm. Step to trace a given algorithm: list all the variables in the algorithm create locations for each of them execute each statement and at the same time change the value of variables involved.

20 Tracing an algorithm Example 1
Start 1. Display “Enter a length : “ 2. Read length 3. Display “Enter a width : “ 4. Read width 5. area  length x width 6. Display “The area of a rectangle : “ , area End Assume that the user enters 7 and 5. What will happen when we trace the above algorithm ?

21 Tracing an algorithm Example 1 length width area 7 5 7*5=35
Start 1. Display “Enter a length : “ 2. Read length 3. Display “Enter a width : “ 4. Read width 5. area  length x width 6. Display “The area of a rectangle : “ , area End Assume that the user enters 7 and 5. What will happen when we trace the above algorithm ? The following diagram shows activities in the computer memory: length width area 7 5 7*5=35

22 Tracing an algorithm Example 2 Start
Display “Enter marks for 3 tests :” Read test1, test2, test3 Display ” Enter marks for 4 quizzes :” Read qz1, qz2, qz3, qz4 Display ” Enter final exam mark :” Read final-exam test-mark  ( test1 + test2 + test3)/3 quiz-mark ( qz1 + qz2 + qz3 + qz4 )/4 carry-mark  20% x test-mark + 20% x quiz-mark final-mark  carry-mark + 60% x final-exam Display message “Final mark : “, final-mark End Assume that the user enters What will happen when we trace the above algorithm ?

23 Tracing an algorithm Example 2
Assume that the user enters What will happen when we trace the above algorithm ? The following diagram shows what will happen in the computer memory. test1 test2 test3 quiz1 quiz2 quiz3 quiz4 finalExam testMark quizMark carryMark finalMark 60 90 80 70 /3 =80 /4 =72.5 0.2*80 + 0.2*72.5 =30.5 30.5+ 0.6*90 =84.5

24 Write A Basic Algorithm Using Pseudocode
Using words to explain the steps to solve problem Start  Start Content  activities/statements/instructions  normally the arrangement of activities/statements/instructions  start with input data  follow by data manipulation(processing statements)  ends with output/print/display the result/information End  End

25 Guidelines In Designing Algorithm in Pseudocode
Firstly, write a basic algorithm before proceeding with the detailed algorithm.  get idea on how to solve the problem read, input, prompt, get  to accept input from user calculate, compute, =  data manipulation (perform processing) display, output, print, write  to convey information

26 EXERCISES

27 Exercise 1 Based on the following picture, answer the given question.
Write a pseudocode to calculate the shaded area in the following area. Assume that the user enters value as in the picture. What will happen when we trace the above algorithm ? 35 17 23 8

28 shaded_area = total_area – bright_area
Pseudocode Start Read Length(t) Read Width(t) Read Length(b) Read Width(b) Calculate total_area = Width(t)*Length(t) Calculate bright_area = Width(b)*Length(b) Calculate shaded_area = total_area-brigth_area Display shaded_area End Start Read lt, wt,lb,wb Calculate total_area = lt*wt bright_area = lb*wb shaded_area = total_area – bright_area Display shaded_area End

29 Exercise 2 Ask the user to enter a time in unit second. Convert the value into hour, minute and second. Write a pseudocode and trace the algorithm, assume user enter 3740

30 Exercise For each of the problem, analyze it using IPO table, then write the algorithm (pseudo code and flowchart). Problem 1: Calculate the area of one circle. Problem 2: Calculate the temperature in Celsius. Temperature in Fahrenheit will be entered. The formula is; Temperature in Celsius = 5/9 (temperature in Fahrenheit – 32) Problem 3: Calculate the final mark for a student who took CS118 paper. The final mark is the total of on-going assessment (carry mark) and 60% of final exam. The carry mark is calculated as follow: Carry mark = 20% of test mark + 20% of quiz mark

31 EXERCISE Chapter 3: Exercise 3B all


Download ppt "ALGORITHM Basic CONCEPTS of Basic Concepts of Algorithm"

Similar presentations


Ads by Google