170 likes | 191 Views
CSCI 125 & 161 / ENGR 144 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 average Now - what if we want to know how many of them are above average? Must be able to go through them all again
E N D
CSCI 125 & 161 / ENGR 144 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 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
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
Array Declaration elementtype arrrayname [ size ]; #define NumScores 10 int scores[NumScores]; scores 0 1 2 3 4 5 6 7 8 9
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};
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
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); }
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
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);
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);
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);
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
Implementing PrintIntegerArray void PrintIntArray(int array[], int n) { int i; for (i = 0; i < n; i++) { cout << array[i] << endl; } }
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; }
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] } }
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);
SwapIntElements void SwapIntElements(int array[], int p1, int p2) { int tmp = array[p1]; array[p1] = array[p2]; array[p2] = tmp; }