1 / 12

CSci 160 Lecture 33

CSci 160 Lecture 33. Martin van Bommel. Passing Arrays to Functions. Write a program to: Read in list of integers until sentinel 0 is entered Reverse the elements in the list Display list in reverse order int main(void) { int list[NElements]; GetIntegerArray(list);

jspiers
Download Presentation

CSci 160 Lecture 33

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. CSci 160Lecture 33 Martin van Bommel

  2. Passing Arrays to Functions • Write a program to: • Read in list of integers until sentinel 0 is entered • Reverse the elements in the list • Display list in reverse order int main(void) { int list[NElements]; GetIntegerArray(list); ReverseIntegerArray(list); PrintIntegerArray(list); }

  3. Problems with Method • Two issues: • Number of elements in array?? • NElements or how many entered before sentinel • GetIntegerArray and ReverseIntegerArray must change values of argument arrays in main • up until now, values of arguments cannot be changed outside function

  4. Generalize Number of Elements • Only wish to reverse and print actual number of elements, array contains more elements in declaration (maximum number) int list[MaxElements]; • Call actual number of elements effective size • Print and reverse must then be told size PrintIntegerArray(list, n);

  5. Function Prototypes • Could write as void PrintIntegerArray(int array[MaxElements], int n); • Better to write void PrintIntegerArray(int array[], int n); • Then can pass array of any size • Also void ReverseIntegerArray(int array[], int n);

  6. GetIntegerArray Prototype • GetIntegerArray has different structure • Don’t know effective size before call • GetIntegerArray determines effective size • Needs to know actual size to limit it • Also give it sentinel value for flexibility int GetIntegerArray(int array[], int max, int sentinel);

  7. Mechanics of Array Parameters • When variable used as parameter, only its value is passed • When array name used as parameter, only base address of array sent to function • Function can then operate on the array in place without copying its contents • Storage for parameter array is shared with actual argument array

  8. Implementing PrintIntegerArray void PrintIntegerArray(int array[], int n) { int i; for (i = 0; i < n; i++) { printf(”%d\n”, array[i]); } }

  9. int GetIntegerArray(int array[], int max, int sentinel) { int n=0, value; printf(” ? ”); scanf(”%d”, &value); while (value != sentinel) { if (n == max) Error(”Too many items”); array[n] = value; n++; printf(” ? ”); scanf(”%d”, &value); } return (n); }

  10. ReverseIntegerArray void ReverseIntegerArray(int array[], int n) { int i, limit; limit = n / 2; for (i = 0; i < limit; i++) { Swap values in array[i] and array[n - i - 1] } }

  11. Swap Array Elements • Try Swap(array[i], array[n-i-1]); • No, because passing values • Must use Swap(array, i, n-i-1);

  12. SwapIntegerElements void SwapIntegerElements(int array[], int p1, int p2) { int tmp; tmp = array[p1]; array[p1] = array[p2]; array[p2] = tmp; }

More Related