1 / 24

Arrays Part II Array Function Arguments

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:

helenaz
Télécharger la présentation

Arrays Part II Array Function Arguments

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. ArraysPart IIArray Function Arguments

  2. 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.

  3. 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!

  4. 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

  5. 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.

  6. Semantics Arrays are alwaysPBR, so the Formal Argument simply renames the Actual Argument upon invocation.

  7. 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); }

  8. 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); }

  9. 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); }

  10. 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); }

  11. 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); }

  12. 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; }

  13. 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]; } }

  14. 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]; } }

  15. 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; }

  16. 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; }

  17. 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; }

  18. 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 }

  19. Common Functions Remove from Partial Array

  20. 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; }

  21. 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; }

  22. 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; } } }

  23. 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; } } }

  24. Vocabulary See Vocabulary at the end of the First Set of Slides on Arrays

More Related