## PIC10A Lecture 14

### Partially-filled Arrays

• Difficult to know exact array size needed
• Must declare to be largest possible size
• Must then keep ‘track’ of valid data in array
• Additional ‘tracking’ variable needed
• `int numberUsed;`
• Tracks current number of elements in array
Example
 ```//Shows the difference between each of a list of golf scores and their average.#include using namespace std;const int MAX_NUMBER_SCORES = 10; void fillArray(int a[], int size, int& numberUsed); //Precondition: size is the declared size of the array a. //Postcondition: numberUsed is the number of values stored in a. //a through a[numberUsed-1] have been filled with //nonnegative integers read from the keyboard. double computeAverage(const int a[], int numberUsed);//Precondition: a through a[numberUsed-1] have values; numberUsed > 0.//Returns the average of numbers a through a[numberUsed-1].void showDifference(const int a[], int numberUsed);//Precondition: The first numberUsed indexed variables of a have values.//Postcondition: Gives screen output showing how much each of the first //numberUsed elements of the array a differ from their average.int main( ){ int score[MAX_NUMBER_SCORES], numberUsed; cout << "This program reads golf scores and shows\n" << "how much each differs from the average.\n"; cout << "Enter golf scores:\n"; fillArray(score, MAX_NUMBER_SCORES, numberUsed); showDifference(score, numberUsed); return 0;}void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } double computeAverage(const int a[], int numberUsed) { double total = 0; for (int index = 0; index < numberUsed; index++) total = total + a[index]; if (numberUsed > 0) { return (total/numberUsed); } else { cout << "ERROR: number of elements is 0 in computeAverage.\n" << "computeAverage returns 0.\n"; return 0; } } void showDifference(const int a[], int numberUsed) { double average = computeAverage(a, numberUsed); cout << "Average of the " << numberUsed << " scores = " << average << endl << "The scores are:\n"; for (int index = 0; index < numberUsed; index++) cout << a[index] << " differs from average by " << (a[index] - average) << endl; } ```
Output
 ```This program reads golf scores and showshow much each differs from the average.Enter golf scores:Enter up to 10 nonnegative whole numbers.Mark the end of the list with a negative number.70 73 69 68 -1 Average of the 4 scores = 70 The scores are: 70 differs from average by 0 73 differs from average by 3 69 differs from average by -1 68 differs from average by -2 ```

## Multidimensional arrays

• Arrays with more than one index
• An array of arrays

### Example

There are 3 pic10a lectures. Each lecture has at most 100 students. Write a 2 dimensional array to represent all the scores.
`double score;`
It can be visualize as
 Lecture 1 score, score, ......, score Lecture 2 score, score, ......, score Lecture 3 score, score, ......, score

Example
 ```#includeusing namespace std;int main() { const int STUDENT_NUM = 4; double score[STUDENT_NUM] = {{32.5, 67, 93.2}, {99.25, 98.2, 99}, {50.7, 60.3, 70.2}, {80.2, 70.8, 65}}; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); int i; cout << "Homework Midterm Exam Final Score\n"; for(i = 0; i < STUDENT_NUM; i++) { cout << score[i] << " " << score[i] << " " << score[i] << " " << 0.3*score[i] + 0.25*score[i] + 0.45*score[i] << endl; } return 0; } ```
Output
 `Homework Midterm Exam Final Score32.50 67.00 93.20 68.4499.25 98.20 99.00 98.8850.70 60.30 70.20 61.8880.20 70.80 65.00 71.01`
Another way to write the program is
 ```#includeusing namespace std;// find the final scoredouble final(double score[]); int main() { const int STUDENT_NUM = 4; double score[STUDENT_NUM] = {{32.5, 67, 93.2}, {99.25, 98.2, 99}, {50.7, 60.3, 70.2}, {80.2, 70.8, 65}}; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); int i; cout << "Homework Midterm Exam Final Score\n"; for(i = 0; i < STUDENT_NUM; i++) { cout << score[i] << " " << score[i] << " " << score[i] << " " << final(score[i]) << endl; } return 0; } double final(double score[]) { return 0.3*score + 0.25*score + 0.45*score; } ```
Explanation
• `score[i]` is an array, its elements are `score[i],score[i],score[i]`
Multidimensional Array Parameters
We won't discuss it. Please refer to the book.