1 / 21

Arrays as Function Parameters

Arrays as Function Parameters. Outline. Passing an array argument (section 9.3) Reading part of an array (section 9.4) Searching and sorting arrays (section 9.5) Finding the smallest value in an array Array search Sorting an array in ascending order. Bubble Sort. #include <iostream>

lwoodson
Download Presentation

Arrays as Function Parameters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays as Function Parameters

  2. Outline • Passing an array argument (section 9.3) • Reading part of an array (section 9.4) • Searching and sorting arrays (section 9.5) • Finding the smallest value in an array • Array search • Sorting an array in ascending order CSCE 106

  3. Bubble Sort #include <iostream> using namespace std; void exchange(float& a1, float& a2); void main() { float x[] = {74.1, 45.2, 83.5, 16.6, 7.8}; for (int i=0; i<4; i++) for (int j = i+1; j<5; j++) if (x[i] < x[j]) exchange(x[i], x[j]); for (j = 0; j<5; j++) cout << “ ” << x[j]); } void exchange(float& a1, float& a2) { float temp = a1; a1 = a2; a2 = temp; } CSCE 106

  4. Passing an Array Argument • Arrays are always passed by reference • Pass entire array to a function by writing just its name (no subscripts or brackets) in the argument list of the function call (actual parameters) • In function definition and prototype, use empty square brackets ([ ]) to identify array • Use keyword const to indicate that array argument cannot be changed by function CSCE 106

  5. Example 1 – Comparing 2 Arrays const int MAX_SIZE = 5; float x[MAX_SIZE ]; float y[MAX_SIZE ]; . . . if (sameArray(x, y, MAX_SIZE)) cout << “Arrays are identical.” << endl; else cout << “Arrays are different.” << endl; CSCE 106

  6. Listing 9.4Function sameArray CSCE 106

  7. Example 2 – Adding 2 Arrays const int MAX_SIZE = 5; float x[MAX_SIZE ] = {1.8, 2.2, 3.4, 5.1, 6.7}; float y[MAX_SIZE ] = {2.0, 4.5, 1.3, 4.0, 5.5}; float z[MAX_SIZE]; . . . addArray(MAX_SIZE, x, y, z); CSCE 106

  8. Listing 9.5Function addArray // File: addArray.cpp // Stores the sum of a[i] and b[i] in c[i] // Sums pairs of array elements with subscripts ranging from 0 // to size – 1 // Pre: a[i] and b[i] are defined (0 <= i <= size-1) // Post: c[i] = a[i] + b[i] (0 <= i <= size-1) void addArray (int size, // IN: the size of the arrays const float a[], // IN: the first array const float b[], // IN: the second array float c[]) // OUT: result array { // Add corresponding elements of a and b and store in c for (int i = 0; i < size; i++) c[i] = a[i] + b[i]; } CSCE 106

  9. Reading Part of an Array • Sometimes it is difficult to know how many elements will be in an array • 34 students in one section • 28 students in another section • Always allocate enough space for largest possible amount needed (e.g. 40 for students in a section) • Remember to start reading with index [0] • Must keep track of how many elements used CSCE 106

  10. Searching and Sorting Arrays • Two common array processing problems • Searching • Sorting • E.g. • look for a particular score, highest score, etc. • rearrange an array of scores in increasing order CSCE 106

  11. Array Search – Function Algorithm 1. For each array element 1.1 If the current element contains the target 1.2 Return the subscript of the current element 2. Return -1. CSCE 106

  12. Array SearchFunction Analysis (Interface) • Input arguments • int items[ ] // array to search • int size // number of items in array • int target // item to find • Output arguments • none • Returns • if found, subscript of first location in array • if not found, -1 CSCE 106

  13. Listing 9.9The function linSearch CSCE 106

  14. Finding the Smallest Value 1. Assume the first element is smallest so far and save its subscript 2. For each array element after the first one 2.1 If the current element < the smallest so far 2.1.1 Save the subscript of current element CSCE 106

  15. Function findIndexOfMin // Finds the subscript of the smallest value in a subarray. int findIndexOfMin(float z[], int start, int end) { // local data int minIndex, // index of the smallest element i; // Assume the first element of subarray is the smallest minIndex = start; for (i = start + 1; i<=end; i++) if (z[i] < z[minIndex]) minIndex = i; // Returns the subscript of the smallest value in the subarray. return minIndex; } CSCE 106

  16. Sorting an Array in Ascending Order • Many programs execute more efficiently if data is in order before processing starts • Possible to order in either ascending or descending arrangement • Selection sort just one of many ways to do this • reuses previous components/functions of search and swap CSCE 106

  17. Selection Sort - Algorithm 1. Starting with the first item in the array (subscript 0) and ending with the next-to-last-item: 1.1 Set i equal to the subscript of the first item in the subarray to be processed in the next steps 1.2 Find the subscript (minSub) of the smallest item in the subarray with subscripts ranging from i through n-1 1.3 Exchange the smallest item found in step 1.2 with item i CSCE 106

  18. Selection Sort Function Analysis • Input arguments float items[ ] // array to sort int n // number of items to sort • Output arguments float items [ ] // original array sorted • Local variables int i // subscript of first element int minSub // subscript of smallest item CSCE 106

  19. Function selSort void selSort(float y[], int n) { int minSub; for (int i=0; i<n-1; i++) { // Find index of smallest item in unsorted section. minSub = findIndexOfMin(y, i, n-1); // Exchange items at position minSub and i. exchange(y[minSub], y[i]); } } CSCE 106

  20. void selSort(float y[], int n) { int minSub; for (int i=0; i<n-1; i++) { minSub = findIndexOfMin(y, i, n-1); exchange(y[minSub], y[i]); } } int findIndexOfMin(float z[], int start, int end) { int minIndex, int i; minIndex = start; for (i = start + 1; i<=end; i++) if (z[i] < z[minIndex]) minIndex = i; return minIndex; } #include <iostream> using namespace std; void exchange(float& a1, float& a2); void selSort(float y[], int n); int findIndexOfMin(float[], int, int); void main() { const int size = 4; float x[] = {74.1, 45.2, 83.5, 16.6}; selSort(x, size); } void exchange(float& a1, float& a2) { float temp; temp = a1; a1 = a2; a2 = temp; } CSCE 106

  21. Next lecture we will revise CSCE 106

More Related