Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Arrays as Function Parameters

2 CSCE 1062 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

3 CSCE 1063 Bubble Sort #include 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; }

4 CSCE 1064 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

5 CSCE 1065 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;

6 CSCE 1066 Listing 9.4 Function sameArray

7 CSCE 1067 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);

8 CSCE 1068 Listing 9.5 Function 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]; }

9 CSCE 1069 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

10 CSCE 10610 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

11 CSCE 10611 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.

12 CSCE 10612 Array Search Function 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

13 CSCE 10613 Listing 9.9 The function linSearch

14 CSCE 10614 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

15 CSCE 10615 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; }

16 CSCE 10616 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

17 CSCE 10617 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

18 CSCE 10618 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

19 CSCE 10619 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]); } }

20 CSCE 10620 #include 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; } 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; }

21 CSCE 10621 Next lecture we will revise


Download ppt "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."

Similar presentations


Ads by Google