Docstoc

Computer Science 187

Document Sample
Computer Science 187 Powered By Docstoc
					CSC 107 -
Programming for
Science
Lecture 27:
Arrays as Parameters,
Searching, & Sorting
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
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
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’};
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];
      }
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
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
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
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
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);
Functions with Array Parameters

   CANNOT change value of array variable
     This   is like other variables, but…
   CAN change value of locations in array
     Functionscan 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(); }
      }
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
     Storeresults in very large arrays
     Search for data patterns
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!
Searching Through Array
 #define NOT_FOUND -1
 int findAndGetLoc(int[] arr, int n, int findIt) {
   int j;
   for (j = 0; j < n; j++) {
     if (arr[j] == findIt) { return j; }
     else if (arr[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]);
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
Speeding Up Searching
 #define NOT_FOUND -1
 int fastFindLoc(int[] arr, int n, int findIt) {
   int low = 0, high = n, mid;
   while (low <= high) {
     mid = ((high – low) / 2) + low;
     if (arr[mid] > findIt) {
       high = mid - 1;
     } else if (arr[mid] < findIt) {
       low = mid + 1;
     } else {
       return mid;
     }
   }
   return NOT_FOUND;
 }
Your Turn
   Get back into groups and do activity
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

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:5/11/2013
language:English
pages:18