Chapter 4: Algorithm Design

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Advertisements

Program Design and Development
Chapter 2: Input, Processing, and Output
Pseudocode.
Chapter 1 Program Design
Structured Program Development in C
Pseudocode.
Chapter 3 Planning Your Solution
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
ALGORITHMS AND FLOWCHARTS
DCT 1123 Problem Solving & Algorithms
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Chapter 2 - Algorithms and Design
Input, Output, and Processing
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Algorithms and Pseudocode
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
ALGORITHMS AND FLOWCHARTS
Chapter 2 Variables.
Chapter 4 – C Program Control
Topics Designing a Program Input, Processing, and Output
REPETITION CONTROL STRUCTURE
Chapter 2: Input, Processing, and Output
Variables, Expressions, and IO
Introduction To Flowcharting
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
ALGORITHM Basic CONCEPTS of Basic Concepts of Algorithm
Siti Nurbaya Ismail Senior Lecturer
ALGORITHMS AND FLOWCHARTS
Print slides for students reference
Control Structure Senior Lecturer
Designing and Debugging Batch and Interactive COBOL Programs
Control Structure Senior Lecturer
Chapter 4: Algorithm Design
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 8 JavaScript: Control Statements, Part 2
Control Structure Senior Lecturer
Chapter 2 - Introduction to C Programming
Structured Program
Chapter 4 - Program Control
Chapter 2 Variables.
Chapter 3 - Structured Program Development
Algorithm Discovery and Design
Iteration: Beyond the Basic PERFORM
ALGORITHMS AND FLOWCHARTS
Introduction to Primitive Data types
3 Control Statements:.
Algorithm Discovery and Design
` Structured Programming & Flowchart
Chapter 3 - Structured Program Development
Chapter 2 - Introduction to C Programming
Chapter 5: Control Structure
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Chapter 3: Selection Structures: Making Decisions
Flowcharts and Pseudo Code
Topics Designing a Program Input, Processing, and Output
ICT Gaming Lesson 2.
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Chapter 2: Input, Processing, and Output
Unit 3: Variables in Java
Basic Concepts of Algorithm
Chapter 2 Variables.
Introduction to C Programming
CHAPTER 6 Testing and Debugging.
Presentation transcript:

Chapter 4: Algorithm Design

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…

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

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.

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

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.

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

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.

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.

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.

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.

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

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.

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

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

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

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

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

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.

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

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.

… 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

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

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

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.

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 ?

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

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

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 60 90 90 60 80 70 80 90. What will happen when we trace the above algorithm ?

Assume that the user enters 60 90 90 60 80 70 80 90 Assume that the user enters 60 90 90 60 80 70 80 90. 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 60+90+90/3 =80 60+80+70+90/4 =72.5 0.2*80 + 0.2*72.5 =30.5 30.5+ 0.6*90 =84.5

The end…