Standard Algorithms. 4 Standard Algorithms Input Validation Finding the Maximum / Minimum Counting Occurrences Linear Search.

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
Software Development Sub Procedures, Functions and Parameters.
 Control structures  Algorithm & flowchart  If statements  While statements.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Standard Algorithms. Many algorithms appear over and over again, in program after program. These are called standard algorithms You are required to know.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.
Program Design and Development
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.
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.
A453 Exemplar Password Program using VBA
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 2 - Algorithms and Design
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
First tutorial.
6.3 List Boxes and Loops Some Properties, Methods, and Events of List Boxes List Boxes Populated with Strings List Boxes Populated with Numbers Searching.
Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 1 Linear Search This algorithm allows the programmer.
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
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 - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Cosc175/testing1 Testing Software errors are costly GIGO Preventing Errors –Echo checking –Range and limit checking –Defensive programming.
Chapter Six: Working With Arrays in Visual Basic.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
I Power Higher Computing Software Development High Level Language Constructs.
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.
September 26, 2011 Sorting and Searching Lists. Agenda Review quiz #3 Team assignment #1 due tonight  One per team Arcade game Searching  Linear  Binary.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
ALGORITHMS.
Intermediate 2 Computing Unit 2 - Software Development.
Homework #2: Functions and Arrays By J. H. Wang Mar. 24, 2014.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
 Software Development Life Cycle  Software Development Tools  High Level Programming:  Structures  Algorithms  Iteration  Pseudocode  Order of.
Advanced Higher Computing Science Standard Algorithms.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
Programming Constructs Notes Software Design & Development: Computational Constructs, Data Types & Structures, Algorithm Specification.
Data Structures Arrays and Lists Part 2 More List Operations.
PROGRAMMING INFORMATION. PROCEDURES IN PYTHON Procedures can make code shorter, simpler, and easier to write. If you wanted, for example, to use a certain.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Higher Computing Science Answering programming questions in the exam.
Marr CollegeHigher Software DevelopmentSlide 1 Higher Computing Software Development Topic 4: Standard Algorithms.
7 - Programming 7J, K, L, M, N, O – Handling Data.
Higher Computing Science
Higher Computing Science Standard Algorithms
Higher Computing Science
while Repetition Structure
Chapter 5: Control Structure
New Structure Recall “average.cpp” program
Standard Algorithms Higher Computing.
Control Structure Senior Lecturer
Standard Algorithms Input validation Finding the minimum
Applied Discrete Mathematics Week 6: Computation
Algorithm Discovery and Design
` Structured Programming & Flowchart
Console.WriteLine(“Good luck!”);
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Presentation transcript:

Standard Algorithms

4 Standard Algorithms Input Validation Finding the Maximum / Minimum Counting Occurrences Linear Search

Standard Algorithms Other than input validation the other 3 standard algorithms are ways of extracting different pieces of information from an array

Input Validation Input validation can be achieved in two ways: Restrict the data the user can input by only presenting them with a limited set of possibilities such as a drop box or pull down menu, Accept the input but reject any which does not meet the restrictions imposed by the software and ask them to input it again. This second approach is the one we are going to use.

Input Validation PROCEDURE GetValidInput() RECEIVE userInput FROM (INTEGER) KEYBOARD WHILE userInput upperLimit DO SEND "Input must be between "& lowerLimit & " and " & upperLimit TO DISPLAY RECEIVE userInput FROM (INTEGER) KEYBOARD END WHILE END PROCEDURE

Input Validation PROCEDURE GetValidInput() REPEAT RECEIVE userInput FROM (INTEGER) KEYBOARD IF userInput upperLimit THEN SEND "Input must be between " & (STRING) lowerLimit & " and " & (STRING) upperLimit TO DISPLAY END IF UNTIL userInput >= lowerLimit AND userInput <= upperLimit END PROCEDURE This algorithm is less efficient than the previous one because the input is being checked twice.

Input validation (String) PROCEDURE GetValidInput() SEND ["Please enter Y or N"] TO DISPLAY RECEIVE userInput FROM (STRING) KEYBOARD WHILE userInput ≠ ["Y"] AND userInput ≠ ["N"] DO SEND "Input must be Y or N " TO DISPLAY RECEIVE userInput FROM (STRING) KEYBOARD END WHILE END PROCEDURE

Input Validation with boolean flag PROCEDURE GetValidInput() validInput = false REPEAT RECEIVE userInput FROM (STRING) KEYBOARD IF length(userInput) < lengthLimit THEN validInput = true ELSE SEND "Input must be less than " & (STRING) lengthLimit & " characters" TO DISPLAY END IF UNTIL validInput = true END PROCEDURE

Input Validation with boolean flag In this version of the algorithm the boolean variable validInput is set to false at the beginning, and the conditional loop only terminates once it has been set to true. This version of the algorithm is useful if you want to check a number of different conditions in the input string

Input Validation (Function) FUNCTION ValidItem(lowerLimit, upperLimit)RETURNS INTEGER RECEIVE userInput FROM (INTEGER) KEYBOARD WHILE userInput upperLimit DO SEND "Input must be between "& lowerLimit " and " &upperLimit" TO DISPLAY RECEIVE userInput FROM (INTEGER) KEYBOARD END WHILE RETURN userInput END FUNCTION

Input Validation (Function) We could call this function with actual parameters, 1 and 50 to return a number between 1 and 50: numberToUse = ValidItem(1,50) or we could call it with the actual parameters 1 and inputRange which is a variable which has a value assigned elsewhere in the program: RECEIVE inputRange FROM (INTEGER) KEYBOARD numberToUse = ValidItem(1,inputRange) This call would return a value between 1 and inputRange.

Iterating through arrays Finding the maximum / minimum value Counting occurrences Linear search

Finding the Maximum SET largest TO the value of the first item in the array FOR each of the remaining items in the array DO IF item_value > largest THEN SET largest TO item_value END IF END FOR print largest

Finding the Maximum PROCEDURE FindMax() SET maximumValue TO numbers[0] FOR counter FROM 1 TO 9 DO IF maximumValue < number THEN SET maximumValue TO number END IF END FOR SEND "The largest value was "& (STRING) maximumValue TO DISPLAY END PROCEDURE

Finding the Maximum PROCEDURE FindMax() SET maximumValue TO numbers[0] FOR counter FROM 1 TO 9 DO IF maximumValue < number THEN SET maximumValue TO number END IF END FOR SEND "The largest value was "& (STRING) maximumValue TO DISPLAY END PROCEDURE

Finding the Minimum SET smallest TO the value of the first item in the array FOR each of the remaining items in the array IF item_value < smallest THEN SET smallest TO item_value END IF END FOR print smallest

Finding the Minimum PROCEDURE FindMin() SET minimumValue TO numbers[0] FOR counter FROM 1 TO 9 DO IF minimumValue > numbers[counter] THEN SET minimumValue TO numbers[counter] END IF END FOR SEND "The smallest value was " & (STRING) minimumValue TO DISPLAY END PROCEDURE

Finding the Minimum PROCEDURE FindMin() SET minimumValue TO numbers[0] FOR counter FROM 1 TO 9 DO IF minimumValue > numbers[counter] THEN SET minimumValue TO numbers[counter] END IF END FOR SEND "The smallest value was " & (STRING) minimumValue TO DISPLAY END PROCEDURE

Counting Occurrences Ask for target_value to count SET number_found TO zero FOR every item in the array IF equal to target_value THEN add 1 to number_found END FOR Print number_found

Counting Occurrences PROCEDURE CountOccurrences() RECEIVE itemToFind FROM (INTEGER) KEYBOARD SET numberFound TO 0 FOR EACH number FROM numbers DO IF number = itemToFind THEN SET numberFound TO numberFound + 1 END IF END FOREACH SEND "There were " & (STRING) numberFound) & "occurrences of " & (STRING) itemToFind & " in the list" TO DISPLAY END PROCEDURE

Counting Occurrences PROCEDURE CountOccurrences() RECEIVE itemToFind FROM (INTEGER) KEYBOARD SET numberFound TO 0 FOR EACH number FROM numbers DO IF number = itemToFind THEN SET numberFound TO numberFound + 1 END IF END FOREACH SEND "There were " & (STRING) numberFound) & "occurrences of " & (STRING) itemToFind & " in the list" TO DISPLAY END PROCEDURE

Counting Occurrences PROCEDURE CountOccurrences() RECEIVE itemToFind FROM (INTEGER) KEYBOARD SET numberFound TO 0 FOR counter FROM 0 TO 9 DO IF number[counter] = itemToFind THEN SET numberFound TO numberFound + 1 END IF END FOR SEND "There were " & (STRING) numberFound) & "occurrences of " & (STRING) itemToFind & " in the list" TO DISPLAY END PROCEDURE

Linear Search Input target_value from user SET found to false SET Counter TO 0 REPEAT IF array[counter] equals target_value THEN SET found to true END IF add 1 to counter UNTIL found IF Counter > list_length THEN print "Not found" Print "Found at position" counter

Linear Search PROCEDURE linearSearch() RECEIVE itemToFind FROM (INTEGER) KEYBOARD SET found TO false SET arraySize TO highestIndex SET counter TO 0 REPEAT IF numbers[counter] = itemToFind THEN SET found to true END IF SET counter TO counter + 1 UNTIL found OR counter > arraySize IF found THEN SEND (STRING) itemToFind &" found at position" & (STRING) counter - 1) TO DISPLAY ELSE SEND "Item not found" TO DISPLAY END IF END PROCEDURE

Linear Search PROCEDURE linearSearch() RECEIVE itemToFind FROM (INTEGER) KEYBOARD SET found TO false SET arraySize TO highestIndex SET counter TO 0 REPEAT IF numbers[counter] = itemToFind THEN SET found to true END IF SET counter TO counter + 1 UNTIL found OR counter > arraySize IF found THEN SEND (STRING) itemToFind &" found at position" & (STRING) counter - 1) TO DISPLAY ELSE SEND "Item not found" TO DISPLAY END IF END PROCEDURE

Linear Search PROCEDURE linearSearch() RECEIVE itemToFind FROM (INTEGER) KEYBOARD SET found TO false SET arraySize TO highestIndex SET counter TO 0 REPEAT IF numbers[counter] = itemToFind THEN SET found to true END IF SET counter TO counter + 1 UNTIL found OR counter > arraySize IF found THEN SEND (STRING) itemToFind &" found at position" & (STRING) counter - 1) TO DISPLAY ELSE SEND "Item not found" TO DISPLAY END IF END PROCEDURE

Linear Search PROCEDURE linearSearch() RECEIVE itemToFind FROM (INTEGER) KEYBOARD SET found TO false SET arraySize TO highestIndex SET counter TO 0 REPEAT SET found TO numbers[counter] = itemToFind SET counter TO counter + 1 UNTIL found OR counter > arraySize IF found THEN SEND (STRING) itemToFind &" found at position" & (STRING) counter - 1) TO DISPLAY ELSE SEND "Item not found" TO DISPLAY END IF END PROCEDURE

Example from SQA paper 2010 A subprogram in building management software is used to find the range of temperatures in a building in one day. The temperature is recorded every 15 minutes within a 24 hour period and stored in a list. Use pseudocode to design one algorithm to find both the highest and lowest temperatures in this list.

Example from SQA paper 2010 PROCEDURE findMaxMin() SET min TO temp[0] SET max TO temp[0] FOR counter FROM 0 TO 95 If temp[counter] > max THEN SET max TO temp[counter] If temp[counter] < min THEN SET min TO temp[counter] END FOR SEND "Maximum = " & max TO DISPLAY SEND "Minimum = " & min TO DISPLAY END PROCEDURE

Example from SQA paper 2011 RightIT, a software company, is currently developing a cash machine program for a bank. The cash machine will offer five options to customers. The options selected during a day are stored as a list. The bank would like the software to calculate the number of times the mobile top-up option appears on this list. Use pseudocode to design an algorithm to carry out this calculation.

Example from SQA paper 2011 PROCEDURE countTopups() SET total TO 0 FOR EACH item FROM options IF item = mobile_top_up THEN SET total TO total + 1 END IF END FOR SEND "There were "& total&" mobile topups" to DISPLAY END PROCEDURE

Another Counting Occurrences example At the end of each round of an archery competition, those players who have a score which is the same or greater than the qualifying score are selected for the next round. The software is required to count the number of players who have qualified. Use pseudocode to design an algorithm to carry out this calculation. State the data structure which will be used to store the players’ scores.

Another Counting Occurrences example Data structure is an integer array PROCEDURE countQualifiers() SET Number_qualified TO 0 FOR each archer_score FROM scores IF archer_score >= qualification_score THEN SET Number_qualified TO Number_qualified +1 ENDIF END FOR SEND "Number of qualifiers = "& Number_qualified TO DISPLAY END PROCEDURE

Linear search example Customers are given a unique ID number when they register with their employer. Customer ID’s are created using the first 3 letters of the customers surname and the last 3 letters of their postcode. This ID is stored in a 1-D array. For example: Mr J Brown with a postcode of EH15 6NW has a customer ID of BRO6NW Using a design notation with which you are familiar, write an algorithm that would find a particular customer ID from the array.

Linear search example PROCEDURE findEmployee() SET found TO false SET counter TO 0 RECEIVE target_ID FROM (STRING) KEYBOARD REPEAT SET found TO employee[counter] = target_ID SET counter TO counter + 1 UNTIL found OR counter > total_employees IF found THEN SEND ["Found at position " & counter] TO DISPLAY ELSE SEND ["Not found"] TO DISPLAY END IF END PROCEDURE

Linear search example PROCEDURE findEmployee() SET found TO false SET counter TO 0 RECEIVE target_ID FROM KEYBOARD REPEAT SET found TO employee[counter] = target_ID SET counter TO counter + 1 UNTIL found OR counter > total_employees IF found THEN SEND ["Found at position " & counter] TO DISPLAY ELSE SEND ["Not found"] TO DISPLAY END IF END PROCEDURE

Cycle Race: What Data Structure? Race times Names Nationalities Qualifiers

Cycle Race: What Data Structure? Race times: real array Names: string array Nationalities:string array Qualifiers:boolean array

Cycle Race: What standard algorithm? Find the best race time Find how many cyclists were from Scotland Find the name of the winner Find Bradley’s time Find how many qualified

Best race time –Find the minimum from race_times array Name of winner –match to names array Cyclists from Scotland –Count occurrences from nationalities array Bradley’s time –Linear search names array then match to race_times array Qualifiers –Count occurrences from qualifiers array

Cycle Race The preferred application for this set of problems is a database – manipulating arrays is exactly what is happening behind the scenes in a database application.