Download presentation
Presentation is loading. Please wait.
Published byJeremy Andrews Modified over 9 years ago
1
CSC 107 - Programming for Science Lecture 27: Arrays as Parameters, Searching, & Sorting
2
Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy to return. To make it easier, the bellboy pockets $2 and gives $1 back to each person. Each person paid $10 and got back $1. So they paid $9 each, totaling $27. The bellboy has $2, totaling $29. Where is the remaining dollar? $25 (manager) + $2 (bellboy) + $3 (customers) = $30
3
Today’s Goal After lecture should be familiar with using arrays in functions Passing arrays as parameters Returning arrays from parameters Using the values stored in the array Searching for data in an array Higher dimension arrays on Friday
4
Arrays Variable holding multiple pieces of data Really contains many different locations Locations are numbered sequentially from 0 Each location is like an individual variable Initial value is unknown New value set at assignment But location is not independent of array Can only be used by accessing array variable
5
Declaring an Array Variable Declaration includes type, name, and size Size should be literal value Variable names rules still apply Variable is an array of the type Each location (element) is of that type int sampleArray[10]; float armada[200] = {0.0f}; FILE* inputs[3] = {NULL, NULL, NULL}; char text[] = {‘h’,‘i’,‘ ’,‘m’,‘o’,‘m’};
6
Using An Array Use array location like a variable But must access through array variable Select element using braces ([ ]) Value inside braces must be an integer int i, fib[20]; fib[0] = 1; fib[1] = 1; for (i = 2; i < 20; i++) { fib[i] = fib[i – 1] + fib[i – 2]; }
7
Passing an Array Location Can use array elements like a variable Can pass to function like any other value sqrt(values[8]); pow(acceleration[4], 2); printf(“Speed = %lf”, 9.8 * v[t]); Element’s value cannot change No different than any other value Function only gets single value Cannot access or use any other elements
8
Variable Number of Parameters va_args allows variable number of parameters Important for scientific & statistical coding Using va_args not simple or easy Parameter must be processed separately Cannot go back after going to next parameter Real limit to usefulness of parameter
9
Passing Array As Parameter Could instead store data in an array But all values must be of same type Then pass the array as a parameter Will also need to pass array’s size Remember: program cannot determine size To pass array, put variable as argument Matching parameter must also be array Passes all locations of the array
10
Array As Parameter Parameter(s) must be listed as arrays type parameterName[] type is type of each element within array parameterName can be any legal name Cannot list size – C does not know it Argument must also be array Cannot pass single variable or value Can have zero or more array parameters Appear in any order among the parameters
11
Examples of Passing an Array double average(double data[], int n); double standardDev(double mean, double data[]); int[] add(int vector1[], int vector2[], int n); double holder; double a[5] = {3}; double b[7] = {1, 3, 3, 5, 3, 1, 5}; int gravity[3] = {0, 0, -10}; int cannonBall[3] = {60, 60, 60}; holder = average(a, 5) + average(b, 7); print(“Sigma = %lf\n”, standardDev(3, b)); add(gravity, {60, 60, 60}, 3); add(gravity, cannonBall, 3);
12
Functions with Array Parameters CANNOT change value of array variable This is like other variables, but… CAN change value of locations in array Functions can have “side-effects” Must be careful when passing & using arrays void makeRandomVector(int v[], int n) { int i; for (i = 0; i < n; i++) { v[i]=rand(); } }
13
Searching through an Array Computers are fast, have vast memories, and dumber than a post Bad for coming up with new designs, theories Helpful when testing new designs, theories Perform lots of experiments or experiments generating lots of data Store results in very large arrays Search for data patterns
14
Searching Through Array Must begin with ordered data Otherwise, will need to examine each location Data ordered from smallest to largest Start at location 0 and keep increasing by 1 If we find the item, stop & return location At location with value larger than datum, datum is not in array stop searching Reach end of array stop searching May end up examining every item!
15
Searching Through Array #define NOT_FOUND -1 int findAndGetLoc(int[] arr, int n, int findIt) { int j; for (j = 0; j findIt) { break; } } return NOT_FOUND; } int data[] = {0, 3, 5, 32, 12912, 34834, 92928}; int findMe = rand() % 92928; int location = findAndGetLoc(data, 7, findMe); if (location != NOT_FOUND) { printf(“Wow: data[%d] = %d\n”, location, data[location]); }
16
Smarter Way To Search Circle target by guessing midpoints Guess is too high, ignore locations above current midpoint Guess is too low, ignore locations with even smaller values Guess just right, return it! Each guess halves possible locations Stop when out of possible locations
17
Speeding Up Searching #define NOT_FOUND -1 int fastFindLoc(int[] arr, int n, int findIt) { int low = 0, high = n, mid; while (low findIt) { high = mid - 1; } else if (arr[mid] < findIt) { low = mid + 1; } else { return mid; } } return NOT_FOUND; }
18
Your Turn Get back into groups and do activity
19
For Next Lecture Continue lab #11 Continue week #11 weekly assignment Work on Programming Assignment #3 Prepare for midterm #2 Will be Monday – 11/13/06
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.