Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1 Chapter 6 Arrays.

Slides:



Advertisements
Similar presentations
Chapter 5 Functions.
Advertisements

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 7 Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 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
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays (continued)
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 7 Single-Dimensional.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
1 Chapter 7 Single-Dimensional Arrays. 2 Arrays Array is a data structure that represents a collection of the same types of data elements. A single-dimensional.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Liang, Introduction to Java Programming1 Arrays Gang Qian Department of Computer Science University of Central Oklahoma.
Chapter 6 Arrays.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 6 Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 10 Lists 1.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Single-Dimensional Arrays and C-Strings.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Arrays.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays 1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Single-Dimensional Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Chapter 7: Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays DDC 2133 Programming II.
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
Chapter 10 Lists.
Chapter 6 Arrays.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays.
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Chapter 10 Lists.
Chapter 5 Arrays Introducing Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays and C-Strings
Chapter 6 Arrays.
Single-Dimensional Arrays
Chapter 6 Arrays.
Chapter 23 Searching and Sorting
Chapter 5 Arrays.
Introducing Arrays Array is a data structure that represents a collection of the same types of data. From resourses of Y. Daniel Liang, “Introduction.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Presentation transcript:

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Passing Arrays to Functions Just as you can pass single values to a function, you can also pass an entire array to a function. PassArrayDemo.cpp #include using namespace std; void printArray(int list[], int arraySize); // function prototype int main() { int numbers[5] = {1, 4, 3, 6, 8}; printArray(numbers, 5); system("PAUSE"); return 0; } void printArray(int list[], int arraySize) { for (int i = 0; i < arraySize; i++) { cout << list[i] << " "; }

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Passing Size along with Array Normally when you pass an array to a function, you should also pass its size in another argument. So the function knows how many elements are in the array.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Pass-by-Reference Passing an array variable means that the starting address of the array is passed to the formal parameter. The parameter inside the function references to the same array that is passed to the function. No new arrays are created. This is pass- by-reference. PassByReferenceDemo.cpp

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved PassByReferenceDemo.cpp #include using namespace std; void m(int, int []); int main() { int x = 1; // x represents an int value int y[10]; // y represents an array of int values y[0] = 1; // Initialize y[0] m(x, y); // Invoke m with arguments x and y cout << "x is " << x << endl; cout << "y[0] is " << y[0] << endl; system("PAUSE"); return 0; } void m(int number, int numbers[]) { number = 1001; // Assign a new value to number numbers[0] = 5555; // Assign a new value to numbers[0] }

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved const Parameters Passing arrays by reference makes sense for performance reasons. If an array is passed by value, all its elements must be copied into a new array. For large arrays, it could take some time and additional memory space. However, passing arrays by reference could lead to errors if your function changes the array accidentally. To prevent it from happening, you can put the const keyword before the array parameter to tell the compiler that the array cannot be changed. The compiler will report errors if the code in the function attempts to modify the array. ConstArrayDemo.cpp Compile error

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved const Parameters ConstArrayDemo.cpp Compile error

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Returning an Array from a Function Can you return an array from a function using a similar syntax? For example, you may attempt to declare a function that returns a new array that is a reversal of an array as follows: // Return the reversal of list int[] reverse(const int list[], int size) This is not allowed in C++.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Modifying Arrays in Functions, cont. However, you can avoid this restriction by passing two array arguments in the function, as follows: // newList is the reversal of list void reverse(const int list[], list newList[], int size) ReverseArray.cpp

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved ReverseArray.cpp #include using namespace std; // newList is the reversal of list void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } void printArray(const int list[], int size) { for (int i = 0; i < size; i++) cout << list[i] << " "; }

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved ReverseArray.cpp int main() { int size = 6; int list[] = {1, 2, 3, 4, 5, 6}; int newList[6]; reverse(list, newList, size); cout << "The original array: "; printArray(list, 6); cout << endl; cout << "The reversed array: "; printArray(newList, 6); cout << endl; return 0; }

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Function void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 0 and j = 5

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (= 0) is less than 6

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 0 and j = 5 Assign list[0] to result[5]

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } After this, i becomes 1 and j becomes 4

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (=1) is less than 6

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 1 and j = 4 Assign list[1] to result[4]

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } After this, i becomes 2 and j becomes 3

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (=2) is still less than 6

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 2 and j = 3 Assign list[i] to result[j]

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } After this, i becomes 3 and j becomes 2

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (=3) is still less than 6

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 3 and j = 2 Assign list[i] to result[j]

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } After this, i becomes 4 and j becomes 1

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (=4) is still less than 6

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 4 and j = 1 Assign list[i] to result[j]

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } After this, i becomes 5 and j becomes 0

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (=5) is still less than 6

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 5 and j = 0 Assign list[i] to result[j]

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } After this, i becomes 6 and j becomes -1

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (=6) < 6 is false. So exit the loop.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Searching Arrays Searching is the process of looking for a specific element in an array; for example, discovering whether a certain score is included in a list of scores. Searching is a common task in computer programming. There are many algorithms devoted to searching. In this section, two commonly used approaches are discussed, linear search and binary search.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Linear Search F The linear search approach compares the key element, key, sequentially with each element in the array list. F The method continues to do so until the key matches an element in the list or the list is exhausted without a match being found. F If a match is made, the linear search returns the index of the element in the array that matches the key. F If no match is found, the search returns -1.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Linear Search Animation animation KeyList

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved From Idea to Solution int[] list = {1, 4, 4, 2, 5, -3, 6, 2}; int i = linearSearch(list, 4); // returns 1 int j = linearSearch(list, -4); // returns -1 int k = linearSearch(list, -3); // returns 5 Trace the function

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Binary Search For binary search to work, the elements in the array must already be ordered. Without loss of generality, assume that the array is in ascending order. e.g., The binary search first compares the key with the element in the middle of the array.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Binary Search, cont. F If the key is less than the middle element, you only need to search the key in the first half of the array. F If the key is equal to the middle element, the search ends with a match. F If the key is greater than the middle element, you only need to search the key in the second half of the array. Consider the following three cases:

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Binary Search KeyList animation

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Binary Search, cont.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Sorting Arrays F Sorting, like searching, is also a common task in computer programming. F It would be used, for instance, if you wanted to display the grades from Listing 6.2, AssignGrade.cpp, in alphabetical order. F Many different algorithms have been developed for sorting. This section introduces two simple sorting algorithms: selection sort and insertion sort.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved F Selection sort finds the largest number in the list and places it last. It then finds the largest number remaining and places it next to last, and so on until the list contains only a single number. F Figure 6.17 shows how to sort the list {2, 9, 5, 4, 8, 1, 6} using selection sort. Selection Sort

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Selection Sort

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved #include using namespace std; void selectionSort(double [], int); void printArray(double list[], int arraySize); // function prototype int main() { double list[] = {2, 9, 5, 4, 8, 1, 6}; selectionSort(list, 7); printArray(list, 4); system("PAUSE"); return 0; } void printArray(double list[], int arraySize) { for (int i = 0; i < arraySize; i++) { cout << list[i] << " "; } void selectionSort(double list[], int arraySize) { for (int i = arraySize - 1; i >= 1; i--) { // Find the maximum in the list[0..i] double currentMax = list[0]; int currentMaxIndex = 0; for (int j = 1; j <= i; j++) { if (currentMax < list[j]) { currentMax = list[j]; currentMaxIndex = j; } // Swap list[i] with list[currentMaxIndex] if necessary; if (currentMaxIndex != i) { list[currentMaxIndex] = list[i]; list[i] = currentMax; } SelectionSort.cpp

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved void selectionSort(double list[], int arraySize) { for (int i = arraySize - 1; i >= 1; i--) { // Find the maximum in the list[0..i] double currentMax = list[0]; int currentMaxIndex = 0; for (int j = 1; j <= i; j++) { if (currentMax < list[j]) { currentMax = list[j]; currentMaxIndex = j; } // Swap list[i] with list[currentMaxIndex] if necessary; (list[i] is in the right place) if (currentMaxIndex != i) { list[currentMaxIndex] = list[i]; // list[i] is in the right place list[i] = currentMax; } SelectionSort.cpp