Lecture # 8 ALGORITHMS AND FLOWCHARTS. Algorithms The central concept underlying all computation is that of the algorithm ◦ An algorithm is a step-by-step.

Slides:



Advertisements
Similar presentations
ALGORITHMS AND FLOWCHARTS
Advertisements

Computers and Scientific Thinking David Reed, Creighton University Algorithms and Programming Languages 1.
Chapter 1 - An Introduction to Computers and Problem Solving
Chapter 2 - Problem Solving
Reference :Understanding Computers
ME 142 Engineering Computation I Fundamentals of Procedural Computer Programming.
Chapter 2 - Problem Solving
Basics of Computer Programming Web Design Section 8-1.
Chapter 2- Visual Basic Schneider
Defining Control Structures
Programming Fundamentals (750113) Ch1. Problem Solving
The Program Design Phases
Review Algorithm Analysis Problem Solving Space Complexity
Algorithm & Flowchart.
ALGORITHMS AND FLOWCHARTS
CSCI-100 Introduction to Computing Algorithms Part I.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Algorithms and Programming
PROGRAMMING LANGUAGES Prof. Lani Cantonjos. PROGRAM - set of step-by-step instructions that tells or directs the computer what to do. PROGRAMMING LANGUAGE.
Programming Concepts Chapter 3.
Flowcharts. Problem Solving Computer programs are written to solve problems or perform tasks Programmers translate the solutions or tasks into a language.
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.
Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
Algorithms & Flowchart
Basic problem solving CSC 111.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
Structured Programming (4 Credits)
Visual Basic Flowcharts October 10, Turn in your vocabulary words before you leave!
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '081 Chapter 2 - Problem Solving 2.1 Program Development Cycle 2.2 Programming Tools.
Problem, Problem Solving, Algorithm & Flow Charts –Part 1 Presented By Manesh T Course:101 CS 101CS Manesh T1.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
FLOWCHARTING AND ALGORITHMS
Introduction to Flowcharts
An Introduction to Programming with C++1 Beginning the Problem- Solving Process Tutorial 2.
Chapter One Problem Solving
ALGORITHMS AND FLOWCHARTS
Flow Charts Basic Flow Chart Symbols Few sample flowcharts Rules
GC101 Introduction to computers and programs
Chapter One Problem Solving
Basics of Computer Programming
Chapter 2- Visual Basic Schneider
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
Basics of Computer Programming
Algorithm Algorithm is a step-by-step procedure or formula or set of instruction for solving a problem Its written in English language or natural language.
Basics of Computer Programming
Introduction to Computer Programming
Basics of Computer Programming
Programming Logic n Techniques
ALGORITHMS AND FLOWCHARTS
Unit# 9: Computer Program Development
Programming Fundamentals (750113) Ch1. Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
ALGORITHMS AND FLOWCHARTS
Chapter 2- Visual Basic Schneider
Introduction to Algorithms and Programming
Chapter 2- Visual Basic Schneider
ME 142 Engineering Computation I
My Take on the Largest Number Algorithm
Algorithms and Programming Languages
Programming Fundamentals (750113) Ch1. Problem Solving
Introduction to Programming
Programming Fundamentals (750113) Ch1. Problem Solving
Computers and Scientific Thinking David Reed, Creighton University
Basic Concepts of Algorithm
Presentation transcript:

Lecture # 8 ALGORITHMS AND FLOWCHARTS

Algorithms The central concept underlying all computation is that of the algorithm ◦ An algorithm is a step-by-step sequence of instructions for carrying out some task Programming can be viewed as the process of designing and implementing algorithms that a computer can carry out A programmer’s job is to:  create an algorithm for accomplishing a given objective, then  translate the individual steps of the algorithm into a programming language that the computer can understand 2

Informal definition of an algorithm used in a computer

Algorithms in the Real World The use of algorithms is not limited to the domain of computing ◦ e.g., recipes for baking cookies ◦ e.g., directions to your housekeeper There are many unfamiliar tasks in life that we could not complete without the aid of instructions ◦ in order for an algorithm to be effective, it must be stated in a manner that its intended executor can understand  a recipe written for a master chef will look different than a recipe written for a college student ◦ as you have already experienced, computers are more demanding with regard to algorithm specifics than any human could be 4

Designing & Analyzing Algorithms 4 steps to solving problems 1.Understand the problem 2.Devise a plan 3.Carry out your plan 4.Examine the solution 5 EXAMPLE: finding the oldest person in a room full of people 1. understanding the problem  initial condition – room full of people  goal – identify the oldest person  assumptions a person will give their real birthday if two people are born on the same day, they are the same age if there is more than one oldest person, finding any one of them is okay 2. we will consider 2 different designs for solving this problem

Algorithm 1 Finding the oldest person (algorithm 1) 1.line up all the people along one wall 2.ask the first person to state his or her name and birthday, then write this information down on a piece of paper 3.for each successive person in line: i.ask the person for his or her name and birthday ii.if the stated birthday is earlier than the birthday on the paper, cross out old information and write down the name and birthday of this person when you reach the end of the line, the name and birthday of the oldest person will be written on the paper 6

Algorithm 2 Finding the oldest person (algorithm 2) 1.line up all the people along one wall 2.as long as there is more than one person in the line, repeatedly i.have the people pair up (1 st with 2 nd, 3 rd with 4 th, etc) – if there is an odd number of people, the last person will be without a partner ii.ask each pair of people to compare their birthdays iii.request that the younger of the two leave the line when there is only one person left in line, that person is the oldest 7

Algorithm Analysis Determining which algorithm is "better" is not always clear cut ◦ it depends upon what features are most important to you  if you want to be sure it works, choose the clearer algorithm  if you care about the time or effort required, need to analyze performance Algorithm 1 involves asking each person’s birthday and then comparing it to the birthday written on the page ◦ the amount of time to find the oldest person is proportional to the number of people ◦ if you double the amount of people, the time needed to find the oldest person will also double Algorithm 2 allows you to perform multiple comparisons simultaneously ◦ the time needed to find the oldest person is proportional to the number of rounds it takes to shrink the line down to one person  which turns out to be the logarithm (base 2) of the number of people ◦ if you double the amount of people, the time needed to find the oldest person increases by the cost of one more comparison 8 the words algorithm and logarithm are similar – do not be confused by this algorithm: a step-by-step sequence of instructions for carrying out a task logarithm: the exponent to which a base is raised to produce a number e.g., 2 10 = 1024, so log 2 (1024) = 10

Algorithm Analysis (cont.) When the problem size is large, performance differences can be dramatic For example, assume it takes 5 seconds to compare birthdays ◦ for algorithm 1:  100 people  5*100 = 500 seconds  200 people  5*200 = 1000 seconds  400 people  5*400 = 2000 seconds...  1,000,000 people  5*1,000,000 = 5,000,000 seconds ◦ for algorithm 2:  100 people  5*  log  = 35 seconds  200 people  5*  log  = 40 seconds  400 people  5*  log  = 45 seconds...  1,000,000 people  5*  log 2 1,000,000  = 100 seconds 9

Big-Oh Notation To represent an algorithm’s performance in relation to the size of the problem, computer scientists use what is known as Big-Oh notation ◦ Executing an O(N) algorithm requires time proportional to the size of problem  given an O(N) algorithm, doubling the problem size doubles the work ◦ Executing an O(log N) algorithm requires time proportional to the logarithm of the problem size  given an O(log N) algorithm, doubling the problem size adds a constant amount of work Based on our previous analysis: ◦ algorithm 1 is classified as O(N) ◦ algorithm 2 is O(log N) 10

Algorithm for finding the largest integer among five integers

Defining actions in FindLargest algorithm

FindLargest refined

Generalization of FindLargest

Three constructs

Flowchart Flowcharts are used as graphical representations of algorithms Uses symbols to represent each step of the algorithm. The order in which the steps are carried out is indicated by connecting “flow lines”

Flowcharting Symbols Terminal-- This represents the beginning or end of a procedure or program. Process--This symbol represents a processing task within a program. Such as computation, data manipulation etc… Decision---This represents a point in the logic flow of the program where the program will follow one of two paths.

Input-output---Used to indicate that some input or output operation is being performed such as printing, reading files, writing files etc… Flowlines---These lines illustrate the direction of execution within a program. All of the above structures are connected with flowlines. Annotation--This is used as a comment in a flowchart.

Start/ Stop Symbol Input/ Output Symbol Processing Box Decision Box Flow Lines On- Page Connector Off- Page Connector

Example 1 Draw a flowchart to find the sum of first 50 natural numbers

Example 2 Draw a flowchart to find the largest of three numbers A,B, and C

Example 3 Draw a flowchart for computing factorial N (N!) Where N! = 1 ´ 2 ´ 3 ´ …… N