1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.

Slides:



Advertisements
Similar presentations
Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.
Advertisements

Lecture 22: Arrays (cont). 2 Lecture Contents: t Searching in array: –linear search –binary search t Multidimensional arrays t Demo programs t Exercises.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Programming Searching Arrays. COMP102 Prog. Fundamentals: Searching Arrays/ Slide 2 Copyright © 2000 by Broks/Cole Publishing Company A division of International.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
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.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
Sequences Jordi Cortadella Department of Computer Science.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
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.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Reading from a file, Sorting, and a little Searching Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics,
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
CS150 Introduction to Computer Science 1
CS 1430: Programming in C++.
CS150 Introduction to Computer Science 1
C++ Programming Lecture 16 Arrays – Part III
Searching and Sorting Arrays
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS 1430: Programming in C++.
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
Chapter 9: Data Structures: Arrays
Life is Full of Alternatives
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
do/while Selection Structure
CS150 Introduction to Computer Science 1
Searching and Sorting Arrays
Life is Full of Alternatives
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
CS 1430: Programming in C++.
Presentation transcript:

1 11/12/04CS150 Introduction to Computer Science 1 More Arrays

2 11/12/04CS150 Introduction to Computer Science 1 Last Time  We o Learnt how to pass arrays to functions  Today we will o Start talking about searching arrays

3 11/12/04CS150 Introduction to Computer Science 1 Arrays used as input  What happens when we want to pass arrays to a function to be used only as output? We can’t pass it by value… o void large (int size, const int arry1[], const int arry2[], int arry3[]);  We can protect array arguments by putting const in front of them in prototype and function definition

4 11/12/04CS150 Introduction to Computer Science 1 Problem  Assume you have two arrays of doubles called vals1 and vals2. They both contain maxels elements. Write a C++ bool function identical that will accept both arrays and return true if both arrays are identical; otherwise, return false. The call to your function might be by a statement of the following form: if (identical (maxels, vals1, vals2)) cout << "Arrays are identical" << endl; else cout << "Arrays are not identical" << endl;

5 11/12/04CS150 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?

6 11/12/04CS150 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

7 11/12/04CS150 Introduction to Computer Science 1 Write the function findElement int findElement(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(index == -1) cout << "Element could not be found!" << endl; else cout << "Element was found at index " << index << endl; }

8 11/12/04CS150 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; }

9 11/12/04CS150 Introduction to Computer Science 1 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.

10 11/12/04CS150 Introduction to Computer Science 1 Function findIndexOfMin void findIndexOfMin(const int x[], int startIndex, int endIndex, int& index) { index = startIndex; for(int i=startIndex + 1; i <= endIndex; i++) if(x[i] < x[index]) index = i; }

11 11/12/04CS150 Introduction to Computer Science 1 Summary  In today’s lecture we covered o Searching arrays  Readings o Chapter 4