PIC10A Lecture 14

Partially-filled Arrays

Example
//Shows the difference between each of a list of golf scores and their average.
#include <iostream>
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[0] through a[numberUsed-1] have been filled with //nonnegative integers read from the keyboard. double computeAverage(const int a[], int numberUsed);
//Precondition: a[0] through a[numberUsed-1] have values; numberUsed > 0.
//Returns the average of numbers a[0] 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 shows
how 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

Example

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

Example
#include<iostream>
using namespace std;

int main() {
const int STUDENT_NUM = 4;
double score[STUDENT_NUM][3] =
{{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][0] << " "
<< score[i][1] << " "
<< score[i][2] << " "
<< 0.3*score[i][0] + 0.25*score[i][1] + 0.45*score[i][2] << endl; } return 0; }
Output
Homework    Midterm     Exam        Final Score
32.50 67.00 93.20 68.44
99.25 98.20 99.00 98.88
50.70 60.30 70.20 61.88
80.20 70.80 65.00 71.01
Another way to write the program is
#include<iostream>
using namespace std;

// find the final score
double final(double score[]);

int main() {
const int STUDENT_NUM = 4;
double score[STUDENT_NUM][3] =
{{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][0] << " "
<< score[i][1] << " "
<< score[i][2] << " "
<< final(score[i]) << endl; } return 0; } double final(double score[]) { return 0.3*score[0] + 0.25*score[1] + 0.45*score[2]; }
Explanation Multidimensional Array Parameters
We won't discuss it. Please refer to the book.