240 likes | 260 Vues
Arrays Part II Array Function Arguments. Passing Arrays between Functions. Sometimes we need to: 1. Pass an array to a function as input. 2. Pass an array to a function to modify. 3. Return an array from a function This does not work with what we now know! Instead:
E N D
Passing Arrays between Functions Sometimes we need to: 1. Pass an array to a function as input. 2. Pass an array to a function to modify. 3. Return an array from a function This does not work with what we now know! Instead: - declare an array in the invoking function - pass it to the invoked function using #2.
Passing Arrays between Functions C/C++ never passes arrays By Value! If it did, a COPY of the entire array would have to be made each time the function is invoked. This is so inefficient, it simply is not done!
Syntax: Array as a Formal Argument Function Declaration Syntax: returnTypefuncName( type ident [] ) { } Place a pair of square brackets with nothing between after the identifier (name of the array argument). Never use the & with an array...it is already PBR
Syntax: Array as an Actual Argument Function Invocation Syntax: { type id [size]; // array declaration ... funcName(id); // invocation } Use only the local name of the array...no need for any brackets.
Semantics Arrays are alwaysPBR, so the Formal Argument simply renames the Actual Argument upon invocation.
Semantics Example: constint SIZE = 5; void init(int a[]) { for (inti=0; i<SIZE; i++) a[i] = 0; } int main() { int b[5]; init(b); }
Semantics Example: constint SIZE = 5; void init(int a[]) { for (inti=0; i<SIZE; i++) a[i] = 0; } int main() { int b[5]; init(b); }
Semantics Example: a simply renames b constint SIZE = 5; void init(int a[]) { for (inti=0; i<SIZE; i++) a[i] = 0; } int main() { int b[5]; init(b); }
Semantics Example: constint SIZE = 5; void init(int a[]) { for (inti=0; i<SIZE; i++) a[i] = 0; } int main() { int b[5]; init(b); }
Semantics Example: constint SIZE = 5; void init(int a[]) { for (inti=0; i<SIZE; i++) a[i] = 0; } int main() { int b[5]; init(b); }
Common Functions Initialize // initialize ALL elements to 0 constint MAX = 100; void init(double a[], int&num) { for (inti=0; i < MAX; i++) { a[i] = 0.0; num = 0; }
Common Functions Populate (User Input) // ask the user to enter values void askUserPopulate(double a[], intnum) { for (inti=0; i < num; i++) { cout<< "Enter a value: "; cin>> a[i]; } }
Common Functions Sum // return the sum of the values in the array double sum(double a[], intnum) { double sum = 0.0; for (inti=0; i < num; i++) { sum += a[i]; } }
Common Functions Max // return the highest value in the array double max(double a[], intnum) { double m = a[0]; // start with first ele for (inti=1; i < num; i++) { if (a[i] > m) m = a[i]; } return m; }
Common Functions Min // return the lowest number in the array double min(double a[], intnum) { double m = a[0]; // start with first ele for (inti=1; i < num; i++) { if (a[i] < m) m = a[i]; } return m; }
Common Functions Print // print all the values on one line void print(double a[], intnum) { for (inti=0; i < num; i++) { cout<< a[i] << ” ”; } cout << endl; }
Common Functions Append to a Partial Array // append new value to array, if enough room void append(double a[], int n, double newVal) { if (num < MAX) {// check if room available a[num] = newValue; num ++; } // else array is full, can not append }
Common Functions Remove from Partial Array
Common Functions Remove from a Partial Array // remove r’thele from the array and return it double remove(double a[], int n, int r) { double rem=0.0; // some invalid/empty value if (r>=0 && r<num) { // ensure r "in range" rem = a[r]; // save a copy num--; // one less item in list for (inti = r; i < num; i++) a[i] = a[i+1]; a[num] = EMPTY_VALUE; // optional } // else r is out of range, can't remove return rem; }
Common Functions Linear Search // find INDEX where srchValfound; -1=not found int search(double a[], intnum, double srchVal){ intfound = -1; // assume not found for (inti=0; i<num; i++) { if (a[i] == s) // found it! found = i; // remember WHERE found } return found; }
Common Functions (Easy to Memorize) Bubble Sort void sort(double a[], intnum) { for (inti=0; i<num-1; i++) { for (int j=i+1; j<num; j++) if (a[i] > a[j]) { // low to hi // swap a[i] with a[j] double t = a[i]; a[i] = a[j]; a[j] = t; } } }
Common Functions (Easy to Memorize) Bubble Sort void sort(double a[], intnum) { for (inti=0; i<num-1; i++) { for (int j=i+1; j<num; j++) if (a[i] <a[j]) { // hi to low // swap a[i] with a[j] double t = a[i]; a[i] = a[j]; a[j] = t; } } }
Vocabulary See Vocabulary at the end of the First Set of Slides on Arrays