Standard Algorithms. Many algorithms appear over and over again, in program after program. These are called standard algorithms You are required to know.

Slides:



Advertisements
Similar presentations
Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
Advertisements

Introduction to arrays
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Standard Algorithms. 4 Standard Algorithms Input Validation Finding the Maximum / Minimum Counting Occurrences Linear Search.
Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Pseudocode and Algorithms
Designing Algorithms Csci 107 Lecture 3. Administrativia Lab access –Searles 128: daily until 4pm unless class in progress –Searles 117: 6-10pm, Sat-Sun.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
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.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
A453 Exemplar Password Program using VBA
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Chapter 4: The Selection Process in Visual Basic.
Chapter 12: How Long Can This Go On?
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 1 Linear Search This algorithm allows the programmer.
Chapter 7 Array processing. Objectives To introduce arrays and the uses of arrays To develop pseudocode algorithms for common operations on arrays To.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
Array Processing.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
CSC 211 Data Structures Lecture 13
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Intermediate 2 Computing Unit 2 - Software Development.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
 Software Development Life Cycle  Software Development Tools  High Level Programming:  Structures  Algorithms  Iteration  Pseudocode  Order of.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Algorithms and Pseudocode
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Selection Using IF THEN ELSE CASE Introducing Loops.
Marr CollegeHigher Software DevelopmentSlide 1 Higher Computing Software Development Topic 4: Standard Algorithms.
while Repetition Structure
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Chapter 5: Control Structure
Standard Algorithms Higher Computing.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Standard Algorithms Input validation Finding the minimum
Introduction to pseudocode
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Introduction to Computer Programming IT-104
Presentation transcript:

Standard Algorithms

Many algorithms appear over and over again, in program after program. These are called standard algorithms You are required to know about 5 of these algorithms: Input Validation (Int 2) Linear search Counting occurences Finding the maximum value Finding the minimum value

Standard Algorithms Input validation checks user input to see if it’s within a valid number range. If it’s outside the valid range the algorithm asks for the number again until it falls within the acceptable range. What is Input Validation

Standard Algorithms Input Validation Pseudocode 1Get and store value 2Loop WHILE data is out with range 3 Display error message 4 Prompt user to re-enter value 5End loop

Standard Algorithms Input Validation VB6.0 code Number = InputBox (“Enter number between 1 and 10”) Do While number max MsgBox (“Must be num between “ & min & ” and “ & max) Number = InputBox (“Enter number between “ & min & ” and “ & max) Loop

Standard Algorithms What is Linear Search? Linear search is the simplest search method to implement and understand. Starting with an array holding 8 numbers with a pointer indicating the first item, the user inputs a search key. Scanning then takes place from left to right until the search key is found, if it exists in the list.

Standard Algorithms Linear Search (The search item is 76) Each item is checked to see if it 76, until 76 is found or the last item is checked.

1. Set found to false 2. Get search value 3. Start at first element on the list 4. Do while (not end of list) AND (found is false) 5.If current element = search value Then 6.Set found = true 7.Display found message 8.Else 9.Move to next element in the list 10.End If 11. Loop 12. If found = false Then 13.Display not found message 14. End If Standard Algorithms Linear Search Pseudocode

IsFound = false SearchValue = InputBox(“Please enter the value your are looking for”) Position = 0 Do While (Position <> UBound(List())) AND (found = false) If List(Position) = SearchValue Then IsFound = true MsgBox(“The item is at position ” & Position & “in the list”) Else Position = Position + 1 End If Loop If found = false Then MsgBox(“The item is not in the list”) End If Standard Algorithms Linear Search VB6.0 Code

Standard Algorithms What is Counting Occurrences Programs often have to count occurrences. Examples include counting the number of:  students who achieved particular marks in an exam  rainfall measurements greater than a particular level  words equal to a given search value in a text file. The basic mechanism is simple: 1.a counter is set to 0 2. a list is searched for the occurrence of the search value 3. every time the search value occurs, the counter is incremented

Standard Algorithms Counting Occurrences Algorithm 1.Set counter = 0 2.Get search value 3.Set pointer to start of the list 4.Do 5.If search item = list(position) Then 6.Add 1 to counter 7.End If 8.Move to next position 9.Until end of list 10.Display number of occurrences

Standard Algorithms Counting Occurrences VB6.0 Code Count = 0 Occurrence = Inputbox(“Please enter value to count”) Position = 0 Do If List(Position) = Occurrence Then Counter = Counter + 1 End If Position = Position + 1 Loop Until Position = UBound(List())

Standard Algorithms Maximum And Minimum Algorithms Computers are often used to find maximum and minimum values in a list. For example, a spreadsheet containing running times for videos might make use of a maximum algorithm to identify the video with the longest running time, or a minimum algorithm to identify the shortest running time. To find a maximum, we set up a variable which will hold the value of the largest item that has been found so far, usually the first element. If an element in the array exceeds this working maximum, we give the working maximum that value.

Standard Algorithms Maximum Pseudocode 1.Set maximum value to first item in this list 2.Set current position to 1 3.Do 4.If list(position) > maximum Then 5.set maximum equal to list(position) 6.End If 7.Move to next position 8.Until end of list 9.Display maximum value

Standard Algorithms Finding the Maximum VB6.0 Code Maximum = List(0) Position = 1 Do If List(Position) > Maximum Then Maximum = List(Position) End If Position = Position + 1 Loop Until Position = UBound(List()) Msgbox(“The maximum value is ” & Maximum)

Standard Algorithms Minimum Pseudocode 1.Set minimum value to first item in ths list 2.Set current position to 1 3.Do 4.If list(position) < minimum Then 5.set minimum equal to list(position) 6.End If 7.Move to next position 8.Until end of list 9.Display minimum value

Standard Algorithms Finding the Minimum VB6.0 Code Minimum = List(0) Position = 1 Do If List(Position) < Minimum Then Minimum = List(Position) End If Position = Position + 1 Loop Until Position = UBound(List()) Msgbox(“The minimum value is ” & Minimum)

Standard Algorithm Exam Questions An international athletics competition between eight countries has a number of events. The winning times are stored in a list in order of lane number like the one on the right. The stadium needs a program to help process the results. Q1.The program must find the fastest time for a race. Use pseudocode to design an algorithm to find the fastest time (4 marks) Q2. It is suggested that algorithm should find the lane number of the fastest time instead of the fastest time. Explain how this could be achieved. (1 mark) Lane Time (secs)

Exam Marking Scheme Set fastest to first time in list For rest of array items If array(current)<fastest then Set fastest to array(current) End if End loop In summary, 1 mark for each of the following: Setting initial value Loop (with end) for traversal of array Comparison of current element with maximum value (with end if) Assignment of new maximum value

Standard Algorithm Exam Questions NoTow is a company that runs a city centre car park. The company requires a piece of software that will calculate the number of cars on a particular day that spent more than three hours in the car park. The number of whole minutes each car is parked is stored in a list as shown on the right. Q3. Use pseudocode to design an algorithm to carry out this calculation (4 marks)

Exam Marking Scheme Set over3 = 0 For each car that day If duration >180 then Add one to over3 End if End loop 1 mark for initialising 1 mark loop with termination 1 mark for if..endif with correct condition 1 mark for keeping running total Note: End of if/loop may be implicit in clearly indented algorithm The value is in minutes so the condition is > 180