Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.

Similar presentations


Presentation on theme: "CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the."— Presentation transcript:

1 CSCI 161 Lecture 14 Martin van Bommel

2 New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the average Now - what if we want to know how many of them are above average? –Must be able to go through them all again –Need to be able to store them in memory

3 Arrays Collection of individual data values Two characteristics –An array is ordered - elements numbered. –An array is homogeneous - all values same type Two properties must be defined –Element type - type of value for elements –Array size - number of elements in array

4 Array Declaration elementtype arrrayname [ size ]; #define NumScores 10 int scores[NumScores]; scores 0 1 2 3 4 5 6 7 8 9

5 Array Selection To refer to element of array, specify array name and position of element (subscript) scores[2] = 25; To set all elements of array for (i=0; i<NumScores; i++) { scores[i] = 0; } To initialize elements of array in declaration int primes[]={2,3,5,7,11,13,17,19};

6 Changing Numbers Program Add an array for the values (assume 100) #define MaxScores 100 int scores[MaxScores]; Store values in array scores[counter] = value; Check if value > average if (scores[i] > average)... See aboveavg.cpp

7 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() { int list[MaxElements]; GetIntArray(list); ReverseIntArray(list); PrintIntArray(list); }

8 Problems with Method Two issues: –Number of elements in array?? MaxElements or how many entered before sentinel –GetIntArray and ReverseIntArray must change values of argument arrays in main up until now, values of arguments cannot be changed outside function

9 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 PrintIntArray(list, n);

10 Function Prototypes Could write as void PrintIntArray(int array[MaxElements], int n); Better to write void PrintIntArray(int array[], int n); Then can pass array of any size Also void ReverseIntArray(int array[], int n);

11 GetIntArray Prototype GetIntArray has different structure Do not know the effective size before call GetIntArray determines effective size Needs to know actual size to limit it Also give it sentinel value for flexibility int GetIntArray(int array[], int max, int sentinel);

12 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

13 Implementing PrintIntegerArray void PrintIntArray(int array[], int n) { int i; for (i = 0; i < n; i++) { cout << array[i] << endl; } }

14 int GetIntArray(int array[], int max, int sentinel) { int n = 0, value; cout << ” ? ”; cin >> value; while (value != sentinel) { if (n >= max) cout << ”Too many items”; else array[n] = value; n++; cout << ” ? ”; cin >> value; } return n; }

15 ReverseIntArray void ReverseIntArray(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] } }

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

17 SwapIntElements void SwapIntElements(int array[], int p1, int p2) { int tmp = array[p1]; array[p1] = array[p2]; array[p2] = tmp; }


Download ppt "CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the."

Similar presentations


Ads by Google