1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Analysis of Algorithms CS Data Structures Section 2.6.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Chapter 9 Data Structures: Arrays and Structs Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Lecture 22: Arrays (cont). 2 Lecture Contents: t Searching in array: –linear search –binary search t Multidimensional arrays t Demo programs t Exercises.
Data Structures Arrays and Structs Chapter The Array Data Type t Array elements have a common name –The array as a whole is referenced through.
CS0007: Introduction to Computer Programming Array Algorithms.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 CIS 205 Practice Test George Lamperti A word that has a predefined meaning in a C++ program and cannot be used as a variable name is known as.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
CS150 Introduction to Computer Science 1
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 1400 Chapter 7 ARRAYS. Array variables Simple variables can hold single values int x;// x can hold one integer float y; // y can hold one float Array.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
Arrays.
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
Search - CIS 1068 Program Design and Abstraction
Arrays.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Search - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 19/23/2015.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.
CS 2430 Day 1. Agenda Load NetBeans Introduction Syllabus Review some array operations.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Computer Programming for Engineers
Reading from a file, Sorting, and a little Searching Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics,
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Arrays as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
CS150 Introduction to Computer Science 1
C++ Arrays.
Arrays November 8, 2017.
CS 1430: Programming in C++.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
Chapter 9: Data Structures: Arrays
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
Presentation transcript:

1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays

2 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays  We search an array to find a particular element in an array  For example, we might like to search an array of student grades for all students who got higher than 90% (i.e. A’s)  How would we do this?

3 11/27/06CS150 Introduction to Computer Science 1 Sequential or Linear Search  Compare each element of the array with the value (or key) that we are searching for  This is called linear or sequential search  Linear Search Algorithm: o For each array element  If the current element contains the target  Return the subscript of the current element o Return -1

4 11/27/06CS150 Introduction to Computer Science 1 Write the function findElement void findElement(int [], int, int&); int main() { int grades[10]; int element, index = -1; for(int i = 0; i < 10; i++) cin >> grades[i]; cout << "Which element would you like to find?" << endl; cin >> element; findElement(grades, element, index); if(-1 == index) cout << "Element could not be found!" << endl; else cout << "Element was found at index " << index << endl; }

5 11/27/06CS150 Introduction to Computer Science 1 Function to find element void findElement( int ar[], int x, int & index) { for(int i = 0; i < 10; i++) if(ar[i] == x) index = i; }

6 11/27/06CS150 Introduction to Computer Science Problem  Write a function to return the index of the smallest element in a subarray  A subarray is a section of an array. The subarray is determined by its starting and ending indexes  The function will have the following arguments: o The array, o The starting index of the subarray, o The ending index of the subarray, o The index of the smallest element.

7 11/27/06CS150 Introduction to Computer Science 1 Function findIndexOfMin void findIndexOfMin(const int x[], int startIndex, int endIndex, int& index) { You fill in the rest }

8 11/27/06CS150 Introduction to Computer Science 1 Random Number Generation Revisited  Remember, the library contains a function for generating random numbers  The statement used to produce integers in the range is o int x = rand() % 6;  To simulate the role of a dice we would use the statement o int x = 1 + rand() % 6

9 11/27/06CS150 Introduction to Computer Science Random Number Generation  Write a program that will simulate the roll of a dice 6000 times and show the frequency in which each side appeared Face Frequency

10 11/27/06CS150 Introduction to Computer Science 1 Practice  Read in a list of last names (strings) and exam scores (ints) and allow the user to find the names of people who scored between two numbers. Input: Smith97 Jones88 Franklin93 Query: Smith Franklin Declare the data structures Write code to read in the data (assume at most 24 students) Write a function to perform the query