Array (continue).

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Arrays Chapter 6.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
 2006 Pearson Education, Inc. All rights reserved Arrays.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 Pearson Education, Inc. All rights reserved Arrays.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Arrays. Introduction This chapter serves as an introduction to the important topic of data structures. Arrays are data structures consisting of related.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
(continue) © by Pearson Education, Inc. All Rights Reserved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
7 Arrays.
24 Searching and Sorting.
Chapter 19 Searching, Sorting and Big O
Presentation transcript:

Array (continue)

7.4.7 Using Arrays to Summarize Survey Results Problem: Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning excellent). Place the 40 responses in an integer array and summarize the results of the poll. C++ has no array bounds checking to prevent the computer from referring to an element that does not exist. Thus, an executing program can “walk off” either end of an array without warning. You should ensure that all array references remain within the bounds of the array.

7.4.8 Static Local Arrays and Automatic Local Arrays A program initializes static local arrays when their declarations are first encountered. If a static array is not initialized explicitly by you, each element of that array is initialized to zero by the compiler when the array is created.

©1992-2010 by Pearson Education, Inc. All Rights Reserved.

©1992-2010 by Pearson Education, Inc. All Rights Reserved.

©1992-2010 by Pearson Education, Inc. All Rights Reserved.

7.5 Passing Arrays to Functions To pass an array argument to a function, specify the name of the array without any brackets. When passing an array to a function, the array size is normally passed as well, so the function can process the specific number of elements in the array. Otherwise, we would need to build this knowledge into the called function itself or, worse yet, place the array size in a global variable. C++ passes arrays to functions by reference—the called functions can modify the element values in the callers’ original arrays. The value of the name of the array is the address in the computer’s memory of the first element of the array. Because the starting address of the array is passed, the called function knows precisely where the array is stored in memory.

7.5 Passing Arrays to Functions (cont.) Although entire arrays are passed by reference, individual array elements are passed by value exactly as simple variables are. Such simple single pieces of data are called scalars or scalar quantities. To pass an element of an array to a func-tion, use the subscripted name of the array element as an argument in the function call.

©1992-2010 by Pearson Education, Inc. All Rights Reserved.

Searching Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1). (Assumptions: no duplicate entries in the array)

Search Example: Return: 3 X= 40 Number 18 20 17 40 22 27 30 0 1 2 3 4 5 6 Return: 3

Search Example: Return: NOT_FOUND (-1) X= 32 Number 18 20 17 40 22 27 30 0 1 2 3 4 5 6 Return: NOT_FOUND (-1)

Searching We will count the number of comparisons the algorithms make to analyze their performance. The ideal searching algorithm will make the least possible number of comparisons to locate the desired data. Two separate performance analyses are normally done: one for successful search and another for unsuccessful search.

Searching We will count the number of comparisons the algorithms make to analyze their performance. The ideal searching algorithm will make the least possible number of comparisons to locate the desired data. Two separate performance analyses are normally done: one for successful search (x is found) and another for unsuccessful search (x is not found)

Linear Search Search the array from the first to the last position in linear progression. int linearSearch (const int array[], int key, int arraySize) { for (int i=0; i< arraySize; i++) if (array[i] == key) return i; return -1; }

Linear Search Performance We analyze the successful and unsuccessful searches separately. Successful Search Best Case: 1 comparison Worst Case: N comparisons (N – array size) Unsuccessful Search Best Case = Worst Case: N comparisons Note: In the case of unsuccessful search, all elements in the array are compared against the search value, so the total number of comparisons is N. However, the actual number of times the while loop is executed is N+1 because we need one more go around to determine that loc becomes equal to number.length.

“Just in time review” 1. How many comparisons do we need to find x=6 using linear search on the following array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10 a. 6 b. 7 c. 8 d. 9

“Just in time review” 1. How many comparisons do we need to find x=6 using linear search on the following array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10 a. 6 b. 7 c. 8 d. 9

“Just in time review” 2. How many comparisons do we need to find x=-8 using linear search on the following array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10 a. 0 b. 1 c. 2 d. 3

“Just in time review” 2. How many comparisons do we need to find x=-8 using linear search on the following array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10 a. 0 b. 1 c. 2 d. 3

“Just in time review” 3. How many comparisons do we need to find x=12 using linear search on the following array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10 a. Undefined b. 0 c. 10 d. Infinity ( )

“Just in time review” 3. How many comparisons do we need to find x=12 using linear search on the following array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10 a. Undefined b. 0 c. 10 d. Infinity ( )

7.8 Sorting Arrays with Insertion Sort Sorting data placing the data into some particular order such as ascending or descending an intriguing prob-lem that has attracted some of the most intense research efforts in the field of computer science.

7.8 Sorting Arrays with Insertion Sort (cont.) Insertion sort—a simple, but inefficient, sorting algorithm. The first iteration of this algorithm takes the second element and, if it’s less than the first element, swaps it with the first element (i.e., the program inserts the second element in front of the first element). The second iteration looks at the third element and inserts it into the correct position with respect to the first two elements, so all three elements are in order. At the ith iteration of this algorithm, the first i elements in the original array will be sorted.

©1992-2010 by Pearson Education, Inc. All Rights Reserved.

©1992-2010 by Pearson Education, Inc. All Rights Reserved.

©1992-2010 by Pearson Education, Inc. All Rights Reserved.