ALGORITHM Basic CONCEPTS of Basic Concepts of Algorithm

Slides:



Advertisements
Similar presentations
CS 240 Computer Programming 1
Advertisements

PSEUDOCODE & FLOW CHART
Flow Control Analysis & Design Tool: Flowcharts
Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK
Chapter 2 - Problem Solving
INTRODUCTION TO PROGRAMMING
ITEC113 Algorithms and Programming Techniques
Chapter 2- Visual Basic Schneider
1 Pertemuan 01 Algorithm Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Chapter 3 Planning Your Solution
The Program Design Phases
Algorithm & Flowchart.
Fundamentals of C programming
Chapter 1 Pseudocode & Flowcharts
ALGORITHMS AND FLOWCHARTS
Design the program Create a detailed description of program –Use charts or ordinary language (pseudocode) Identify algorithms needed –Algorithm: a step-by-step.
PROGRAMMING LANGUAGES Prof. Lani Cantonjos. PROGRAM - set of step-by-step instructions that tells or directs the computer what to do. PROGRAMMING LANGUAGE.
Describe the Program Development Cycle. Program Development Cycle The program development cycle is a series of steps programmers use to build computer.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
© 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Stewart Venit ~ Elizabeth Drake Developing a Program.
Software Life Cycle What Requirements Gathering, Problem definition
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
ALGORITHM List of instructions for carrying out some process step by step. A sequence of instructions which has a clear meaning and can performed with.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
1 Program Planning and Design Important stages before actual program is written.
LESSON 1 Introduction to Programming Language. Computer  Comprised of various devices that are referred to as HARDWARE.  The computer programs that.
Algorithms and Pseudocode
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.
CSE 110: Programming Language I Matin Saad Abdullah UB 404.
Algorithms and Flowcharts
CHAPTER 2 GC101 Program’s algorithm 1. COMMUNICATING WITH A COMPUTER  Programming languages bridge the gap between human thought processes and computer.
Learning outcomes 5 Developing Code – Using Flowcharts
CS1010 Programming Methodology
GC101 Introduction to computers and programs
Introduction to Programming / chapter 3 / COM1022
INTRODUCTION TO PROBLEM SOLVING
Lesson 2 Flowcharting.
Unit 3: ALGORITHMS AND FLOWCHARTS
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
GC211Data Structure Lecture2 Sara Alhajjam.
Chapter 2- Visual Basic Schneider
Algorithms and Flowcharts
Algorithms An algorithm is a sequence of steps written in the form of English phrases that specific the tasks that are performed while solving the problem.It.
Lecture 2 Introduction to Programming
Introduction To Flowcharting
Introduction to Computer Programming
ALGORITHMS AND FLOWCHARTS
Control Structure Senior Lecturer
Control Structure Senior Lecturer
Lecture 2: Logical Problems with Choices
Chapter 4: Algorithm Design
Unit# 9: Computer Program Development
Control Structure Senior Lecturer
ALGORITHMS AND FLOWCHARTS
Chapter 2- Visual Basic Schneider
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Faculty of Computer Science & Information System
Chapter 2- Visual Basic Schneider
ME 142 Engineering Computation I
Chapter 4: Algorithm Design
Understanding Problems and how to Solve them by using Computers
Lecture 6 Architecture Algorithm Defin ition. Algorithm 1stDefinition: Sequence of steps that can be taken to solve a problem 2ndDefinition: The step.
ICT Gaming Lesson 2.
Introduction to Programming
Basic Concepts of Algorithm
WRITING AN ALGORITHM, PSEUDOCODE, AND FLOWCHART LESSON 2.
Introduction to Programming
Presentation transcript:

ALGORITHM Basic CONCEPTS of Basic Concepts of Algorithm Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences Universiti Teknologi MARA Kedah (e): sitinurbaya@kedah.uitm.edu.my (u): https://sitinur151.wordpress.com

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

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

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

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.

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.

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)

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.

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

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

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

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.

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

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

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

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

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

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

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

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

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

Tracing an algorithm Example 2 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

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

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

EXERCISES

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

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

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

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

EXERCISE Chapter 3: Exercise 3B all