121: Computer Science 1 --- Lecture February 15-20

Topics:

Introduction to arrays
Implementing Lists using Arrays
Input and output of Lists
Passing Arrays as Functions

One-dimensional Arrays

Suppose we have a vector of 15 numbers. The following declaration is very awkward:

int Number0, Number1, Number2, Number3, Number4, Number5, Number6, Number7, Number8, Number9, Number10, Number11, Number12, Number13, Number14;

Such a declaration would clearly be infeasible if had say 1000 numbers. In practice however we may have lists of tens of thousands or even millions of elements. Clearly, we need some structure to handle this. In C++ and other high-level programming language such a structure is called an array. An array consists of multiple data elements of the SAME type, i.e., all of type int, all of type float, etc. It is stored in memory as a contiguous block and elements of the array can be directly accessed. In our example above we would declare an array called Numbers as follows:

int Numbers[15];

The general syntax for the one-dimensional array declaration is as follows:

<element data class><array name>[<number of elements>]

Examples:

float Average[35];
char Grades[200];
long double Vector[90];

In C++ the first index of the array is 0 and the last index is one less than the size of the array. For example, Numbers has position 0, 1, ..., 14 and Grades has positions 0, 1, ..., 199.

When using arrays the size of the array needs to be declared large enough to handle the maximum number of positions that will be used. Often only a fraction of the array is utilized. For example, the array Grades above is declared to handle the largest possible class size, i.e., a class of 200 students. However, most classes are considerable smaller than this. It is a good idea to use a constant when declaring the size of an array. That way if you need to change the size of the array you only need to change the constant. For example,

const int MAX = 200;

char Grades[MAX];

Arrays allow for direct access. To directly access position i of array Numbers use Numbers[i].

For example, the following statement assigns position 7 the value 89:

Numbers[7] = 89;

An important operation that is often performed on arrays is to input values into the first Size positions of the array. This can be accomplished as follows:

for(int Position = 0; Position < Size; ++Position)
{
   count << "Enter number: ";
   cin >> Numbers[Position]
}//END for
 

Implementing Lists using Arrays

An important application of an array is to implement a list, e.g., a list of students in a class. It is natural to store the list in the first N positions of the array where N is the size of the list, i.e., positions 0 through N-1. In addition to the array we must maintain a variable which keeps track of the size of the list.

CAUTION: Do not confuse the size of the array which is fixed with the size of the list which may vary.

For example, suppose that we declared an array GradeList[] as follows:

int GradeList[200];

Depending on the number of students in the class the size of the list will vary from 1 to 200, and the list will be stored in positions 0 through Size - 1 of GradeList[], where Size is an integer variable that maintains the size of the class. The list elements can be input into the array GradeList[] as follows:

for(int Position = 0; Position < Size; ++Position)
{
   cout << "Enter list element: ";
   cin >> GradeList[Position]
}//END for

The list can be displayed as follows:

for(int Position = 0; Position < Size; ++Position)
{
   cout << GradeList[Position] << '\t';
}//END for
 

Passing Arrays as Functions

To pass an array to a function you must pass the address of the array. In C++ the array name is the address of the first element of the array. Since the array name references the address of the array the array is automatically passed by reference.

Consider the function GetGrades below, which inputs a list of grades. Grades are input into an array until a negative number (sentinel) is encountered. Note that, unlike with the parameter Size, there is no ampersand (&) preceding the parameter GradeList[]. This is because GradeList[] is an array and as such is automatically passed by reference.

void GetGrades(int GradeList[],int &Size)
{
      int Grade;
      //READ IN FIRST GRADE 
      cout << "Enter grade or negative value to stop: ";
      cin >> Grade;
      Size = 0;
      while(Grade >= 0)
      {
         GradeList[Size] = Grade;  //ADD GRADE TO ARRAY
         ++Size;  //UPDATE SIZE
 
         //READ IN NEXT GRADE
         cout << "Enter grade or negative value to stop: ";
         cin >> Grade;
      }//END while
  }//END GetGrades()

The following function computes the average of elements in a list. Note that since the parameter Size is to remain unchanged by Average(), it is passed by value. Since the parameter List[] is an array it is automatically passed by reference.

float Average(float List[], int Size)
{
   float Sum = 0.0;
   for(int Count=0; Count < Size; ++Count)  //COMPUTE SUM OF LIST ELEMENTS
      Sum += List[Count]; 
   return Sum/Size;
}//END Average()