Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4: Algorithm Design

Similar presentations


Presentation on theme: "Chapter 4: Algorithm Design"— Presentation transcript:

1 Chapter 4: Algorithm Design

2 Lesson outcome At the end of this chapter, student should be able to:
Listing three basic types of Control Structure Listing and explain the rules in writing identifiers, variables and constants. Understand how to trace a detailed algorithm. Apply algorithm in a simple problem solving. Write a detailed algorithm. The heart of programming process lies in planning the program’s logic…Because, during this phase of programming process, the programmer plans the steps to the program, deciding what steps to include and how to order them logically…

3 Data Data are raw materials, entered into a computer to be processed or manipulated to produce meaningful information. A few types of data are text, graphic, still image, video and audio. Text data type consists of number or numeric, letter ( small or capital letter ) and special symbol or special character. 45 data 45cm square - represents the size of the room 45kg - represents a weight information information

4 Data Text data also can be broken into two types: - numeric / number
- alphanumeric Numeric data can be used in an aritmatic calculation. Numeric data: integer and real number. Integer number is a whole number, not involving decimal places. A real number involves decimal places. Examples of integer are 123, 45, 765 and etc. Examples of real number are 1.23, 45.0, 76.5 and etc.

5 Data Alphanumeric data cannot be used in arithmetic calculation.
Alphanumeric data: a single character and a string. A single character is a primitive data type that can be used to store a single character such as a letter, number, punctuation mark, or other symbol. A string is a composite data type, a sequence of characters. The string can be broken down into characters. An example of data of a string is “DM52”. It can be broken down to separate characters of ‘D’, ‘M’, ‘5’ and ‘2’.

6 Variable A variable is used to represent memory.
A variable is simply a name that we create and declare in a program to refer to memory. The variable name is also called data name or identifier. A variable is capable to store a datum at a time. The content in the variable is changeable.

7 Variable Assume that we want to involve data about mark, average, total, grade and name in a program. Each variable has the values of 90, 70.0, 210, A and Siti Alia respectively. Real number Mark 90 Average 70.0 Name Siti Alia Grade A Total 210 Memory cell Integer string Single character Representation of how data stored in computer memory

8 How To Give Name To A Variable In An Algorithm?
A variable is given a name so that it is easy to refer to it when writing an algorithm. There is no specific way to create a variable or data name. The suggestions are: The name represents what the content of the variable is. For example, if we want to store data about the total. The suggested names are sum or total. Do not create a long name. You can use abbreviation, but make sure the name has a meaning. For example, to store data about a student’s name. The abbreviation that can be used is std_name but not sn.

9 Simple Statements Simple statements: input statement, output statement and assignment statement. The input statement is used to allow the user to enter the data. The output statement is used to display the information. The assignment statement is used to perform the calculation.

10 Output statement The purpose of an output statement is to display information to the computer screen. Usually the display takes the following forms: Display a message - to display any message that we want the user to see on the screen. - Example: Display the value of variable(s) - to display the content of a variable to the screen. Display “what ever message to write ”; Display “Welcome to Kuala Lumpur ”; Display average; Remember that the content of variable(s) is actually located in the memory. The output statement will read this content and display it on the screen. Display “The average is :”, average; a short message before display the value of the variable(s) Note: We can use words like display, print or output to represent output statement.

11 Input statement Purpose is to read the data entered by the user and then put the value(s) to the variables mentioned. The data is usually obtained from outside of the computer. We have to use the word get, read or input to implement input statement.

12 Input statement The format to write input statement are as follows:
to read one data : Read variable OR Input variable to read many data: Read variable1, variable2,... OR Input variable1 , variable2,... Example: Display “Enter your name: “ Read name Note: Output statement is usually used together with input statement. The purpose is to guide the user as to what kind of data she or he has to enter. Read length Read width Read one data Read length, width Read many data

13 Assignment Statement The purpose of the statement is to assign data or value to a variable. In this aspect, assignment statement is like an input statement because both statements give value to a variable. Input statement - the value comes from outside of the computer, usually it comes from the user. Assignment statement - the values are directly assigned to the variable or the result of a calculation in the program.

14 Assignment Statement We can give value to the variable that store a numeric value using three ways: Assign a fixed value to a variable. Copy value of a variable to another variable. A result of a calculation using formula. i. initialize count to zero ii. count←0 iii. count = 0 iv. set count to zero v. set grade with ‘A’ vi. set name to “Hawa Adam” single quotes (‘ ‘) - used together with single character data type double quotes (“ “)are used together with string data type vii. set number2 with number1 viii. number2 ← number1 ix. number1 = number2 x. area = length x width xi. multiply variable length with variable width and store the result in variable area xii. add 5 to variable cnt xiii. cnt = cnt + 5

15 Assignment Statement Arithmetic Operator: Operator Operation +
Addition - Subtraction * Multiplication / Division % Modulus (Remainder) -- Decrement by 1 ++ Increment by 1

16 How To Write A Detailed Algorithm Using Pseudocode?
A detailed algorithm is described based on the expected screen and a basic algorithm. All problems discussed in the previous chapter can be solved using simple statements (input, output and assignment statements). Detailed algorithm = basic algorithm + expected screen

17 How To Write A Detailed Algorithm Using Pseudocode?
Detailed Pseudocode OR

18 How To Write A Detailed Algorithm Using Flowchart?
Start Start length, width calculate area area End “Enter a length:” Length “Enter a width:” width OR area←length x width “The area of a rectangle :” area End

19 Control Structures In simple term, control structure is the type of algorithm. The structure could be sequential, selection and repetition. Each of these is needed for a different purpose within a program.

20 Sequential control structure
The most common form of control structure – a basic as well as a default program. Each step is carried out in order of their position and is only done once. A sequence is basically where the code just follows one linear path until it reaches the end. Can contain any number of events, but there is no change 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 Note: Chapter 4 is focused on sequential structure

21 Selection control structure
This control structure allows the program to choose between one of two or more alternative paths of instructions. Decides a particular answer from a set of variable answers and carries out the steps that proceeds it. With this structure, you ask a question, and depending on the answer, you take one of two courses of action. Then, no matter which path you follow, you continue on with the next event.

22 … Selection control structure
Note that, it is perfectly correct for one branch of the selection to be a “do nothing” branch: if someCondition is true then do oneProcess

23 Repetition control structure
You may hear programmers refer to looping as repetition or iteration. This is basically where a program will repeat a set of instructions until it reaches a certain point where an event has occurred (the condition is met) In a loop, you ask a question; if the answer requires an action, you perform the action and ask the original question again. If the question’s answer requires that the action be taken again, you take the action and then ask the original question again. Some programmers call this structure a do while: DOWHILE condition is true statement block ENDDO

24 Combination of three structures
All logic program can be solved using only these three structures (sequence, selection and repetition). The three structures, can be combined in an infinite number of ways. For example: there can be a sequence of steps followed by a selection or repetition followed by sequence. Step A Sequence Step B do step A do step B if condition C is true then do step D else do step E endif while condition F is true then do step G No Yes C? Selection Step E Step D Yes F? Step G Repetition No

25 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.

26 Tracing an algorithm – example
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 ?

27 Tracing an algorithm What we have to do are:
List all the variables. The variables in this algorithm are length, width and area. The computer executes statement by statement. When the computer executes the first statement, the message Enter a length: appears on the screen. When the second statement is executed, the computer waits for the user to enter data for the variable length. When the user enters 7, this value will be stored into location length. When the computer executes the third statement, the message Enter a width: appears on the screen. When the computer executes the forth statement, it waits for the user to enter a data for the variable width. When the user enters 5, this value will be stored at location width. When the computer executes the fifth statement, it calculates 7 x 5 and the result will be stored into variable area. When the computer executes the sixth statement, the message The area of a rectangle: will be displayed on the computer screen, follows by 35 (the content of variable area).

28 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

29 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 ?

30 Assume that the user enters 60 90 90 60 80 70 80 90
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.

31 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

32 The end…


Download ppt "Chapter 4: Algorithm Design"

Similar presentations


Ads by Google