COSC 1315

Programming Fundamentals

One-Dimensional Arrays

Revised:  February 5, 2007
By Richard G. Baldwin

File:  Pf 00190.htm
Practice Text

 


Preface

This lesson was written specifically for the benefit of my students in COSC 1315, Fundamentals of Programming.  The lesson was written under the assumption that those students have no prior programming knowledge when they enroll in the course.

Another browser window

I recommend that you open another copy of this document in a separate browser window so that you can view the code and the discussion of that code at the same time.

Introduction

What is an array?

An array is a list of variables, all of the same type, all of which have the same name.

What is an element?

Each variable in the array is called an element.

What is an index?

Each element in the array has a subscript or index that differentiates it from the other elements in the array.  (Some people call it a subscript while others call it an index.)

The index is positive integer that indicates the position of a particular element relative to the first element in the array.

The first element in a C++ array always has an index value of 0.

Declaring an array

You must declare an array before you can use it.

When you declare an array in C++, you create a data structure that contains the list of variables.

(This is not true in all programming languages.  For example, in Java, it is necessary to create an object that encapsulates the array and to declare a variable that points to the object as separate operations.  However, it is possible to combine both operations into a single statement in Java.)

The size of an array

You specify the number of elements (the size) of a C++ array when you declare it.  Once created, the size of a C++ array cannot be changed.  (This is also true for other programming languages such as Java.)

The range of index values

If the size of the array is N, the index values for the elements in the array always extend from 0 to N-1.

Accessing an array element

You access an individual element in an array by:

(See the lesson entitled Expressions and Operators for a list of C++ operators including the subscript operator.) 

Why use an array?

As you will see in the sample program in this lesson, probably the most important value of an array lies in the ability to use another variable as the index.

This makes it possible to:

No automatic element initialization

Declaring an array in C++ does not automatically initialize the values of the elements to zero.

(Other languages such as Java do automatically initialize all array elements to a default value that depends on the type of the elements.)

Be very careful about initialization

Therefore, when programming in C++, you should always make certain that you purposely store a value into each array element before you access and use the value stored in that element.

Otherwise, you will simply be processing garbage left over in memory from the execution of some previous program.

Staying within the array bounds

When using arrays in C++, you must be particularly careful to stay within the bounds of the array.

For example, if you create and populate an array containing five elements, there is nothing in C++ to prevent you from later fetching and using six element values supposedly from the array.

A serious logic error

Accessing an array element that is outside the bounds of the array is a serious logic error.

(Other languages such as Java won't allow you to store or fetch data outside the bounds of an array.)

Processing garbage

If you do fetch and use six element values from consecutive indices in a five-element array, one of those element values will be garbage because it really isn't part of the array.

Sample Program

In this lesson, I will present and explain a C++ program named Array01.cpp.  I will explain the program in fragments.  You can view a complete listing of the program in Listing 8 near the end of the lesson.

A one-dimensional array of type long

This program illustrates the use of a simple one-dimensional array in C++.

The program begins by declaring a five-element array where each element in the array is designed to store an integer value of type long.

Populating the array

The program asks the user to enter five integer values in succession.  It uses those five values to populate the array.

(To populate an array is techno-jargon for storing values in the elements of the array.)

Values are stored in reverse order

The program purposely stores the values in reverse order in the array.

By this, I mean that:

Display the array contents

After populating the array, the program displays the contents of the array in forward order.

Forward order means beginning with the element at index 0 and progressing to the element at index 4.

Display the average of the element values

Following this, the program computes and displays the average of the values stored in the array.

Display minimum and maximum values

Finally, the program finds and displays the minimum and maximum values stored in the array.

Sample output

Figure 1 shows the program output for a typical set of input values:

Enter an integer value: -100
Enter an integer value: -50
Enter an integer value: 0
Enter an integer value: 49
Enter an integer value: 99
Array contents:
99 49 0 -50 -100
Average: -0.4
Minimum: -100
Maximum: 99

Figure 1

Will explain in fragments

As mentioned earlier, I will explain this program in fragments.

Two of the functions in the program, main and classMain, are identical to functions having the same names in sample programs in previous lessons.  Therefore, I won't explain those functions in this lesson.

As indicated earlier, you can view the entire program in Listing 8 near the end of the lesson.

The function named doSomething

All of the new and interesting code in this program is in an instance function named doSomething.  The beginning of the function named doSomething is shown in Listing 1.

  void doSomething(){
    const int size = 5;
    long values[size];
  
Listing 1

Declaring the array

The code in the doSomething function begins by declaring a constant named size having a value of 5.

Then it uses that constant to declare an array named values having a size of five elements of type long .

(It is important to note that the value for size could have been derived from some other source before the array was declared.  For example, it could have been input by the user, or could have been the day of the month.  This makes it possible for the size of the array to be established when the program is run, as opposed to being established when the program is compiled.)

The subscript operator

Note the use of the subscript operator [] in Listing 1 to declare the array.  The declaration syntax consists of:

That's all there is to declaring an array in C++.

(Declaring an array in Java is a little more complicated because all arrays in Java are encapsulated in separate objects.)

Processing the array

Once the array has been declared, the doSomething function executes the code in Listing 2 to process the array.

    //Populate the array in reverse order.
    getData(values,size);
    //Display contents of the array in forward
    // order.
    cout << "Array contents:" << endl;
    displayData(values,size);
    //Compute and display average, minimum, and
    // maximum values.
    cout << "Average: " << getAvg(values,size)
                                         << endl;
    cout << "Minimum: " << getMin(values,size)
                                         << endl;
    cout << "Maximum: " << getMax(values,size)
                                         << endl;
  }//end doSomething function
  
Listing 2

Call five functions in succession

The code in Listing 2 calls the following five functions in succession to perform the indicated operations on the array:

Parameter passing

Note the actual parameters that are passed to each of these functions.

The first parameter is the name of the array.

The second parameter is the size of the array.

The same actual parameter list is used for each of the five functions.

How are the parameters passed?

As you will see later, the formal parameter list for each function makes it appear that both parameters are passed by value instead of being passed by reference.

Actually passed by reference

However, as you will also see later, even though the first parameter appears to be passed by value on the basis of syntax, the first parameter is actually passed as a reference to the array and is not simply a copy of the array.

How do I know this?

That this is true is demonstrated by the function named getData, which changes the values stored in the elements in the original array.

If a copy of the array were to be passed to the function named getData, it would not be possible for the function to deposit user input values into the original array.  Rather, those values would be deposited in the copy of the array, which would cease to exist when the function terminates.

(See the lesson entitled Functions for a discussion of the difference between passing parameters by value and passing parameters by reference.)

The getData function

The getData function is shown in its entirety in Listing 3.

  void getData(long array[],int size){
    int count = size - 1;
    while(count >= 0){
      cout << "Enter an integer value: ";
      cin >> array[count];
      count = count - 1;
    }//end while loop
  }//end getData
  
Listing 3

Accessing an array element

The most important aspect of Listing 3 in the context of this lesson is the array-access expression that is highlighted in boldface in Listing 3.

An array access expression consists of:

The formal parameter list

Note the syntax of the formal parameter list for the function.

An empty subscript operator is used in the declaration of the first parameter to specify that the first parameter is an array.

(As mentioned earlier, this is really a reference to the array rather than a copy of the array although that isn't shown explicitly by the declaration syntax.)

In this case, the second parameter is the size of the array.  This value will be used in the conditional clause of a while loop that gets integer input values from the user and stores those values in the elements of the array.

The element access expression

Note the syntax of the expression that is highlighted in boldface in Listing 3

This expression uses the subscript operator, [], along with the value of the loop counter to store an input value provided by the user into the array element whose index matches the value of the loop counter.

The most important value ...

This is an example of what was meant by the earlier statement regarding the most important value of an array and the ability to use another variable as the index to iterate through the array elements in a controlled fashion.

In this case, the variable is the loop counter named count.  As the value of count is decremented once during each iteration of the loop, incoming values from the user are stored successively in array elements 4, 3, 2, 1, and 0.

Changing the values in the original array elements

Once again, as we will see when we later invoke the displayData function to display the contents of the array, the code in Listing 3 is actually changing the values in the elements of the original array, and is not changing the values in the elements in a copy of the array.

Therefore, even though the formal parameter list does not qualify the first parameter using the reference operator &, that incoming parameter has to be a reference to the array and is not a copy of the array.

Remaining code should be very familiar

The remaining code in Listing 3 should be very familiar to you by now, so I will let that be the end of the discussion of the getData function.

The displayData function

The displayData function is shown in its entirety in Listing 4.

  void displayData(long array[],int size){
    int count = 0;
    while(count < size){
      cout << array[count] << " ";
      count = count + 1;
    }//end while loop
    cout << endl;
  }//end displayData
  
Listing 4

The formal parameter list

Note that the formal parameter list for this function is identical to the formal parameter list for the previously-discussed function named getData.  In fact, that will be the case for all of the remaining functions to be discussed in this lesson, so I won't mention it again.

The purpose of the function

The purpose of this function is to access and display the values currently stored in the array.  These are the values that were stored in the array by the earlier execution of the getData function.

The element access expression

The most important thing in Listing 4 in the context of this lesson is the element access expression shown in boldface.

Once again, this expression uses the subscript operator along with the value of the loop counter named count to access a different array element during each iteration of the loop.

A fetch expression

However, in this case, the expression fetches the value currently stored in the element instead of storing a new value in the element as was the case in the getData function.

How do I know that?

The element access expression in Listing 4 is identical to the element access expression in Listing 3.

How do I know that it is a fetch expression in Listing 4 and a storage expression in Listing 3.

Must consider the context

You can't determine that simply from the syntax of the access expression alone.  You must consider the access expression in the context of its surroundings.

Right operand of an extraction operator

The element access expression in Listing 3 is the right operand of a C++ extraction operator.  Such an operator is designed to extract something from its left operand and to store it into its right operand.

Right operand of an insertion operator

The element access expression in Listing 4 is the right operand of a C++ insertion operator.  Such an operator is designed to get something from its right operand and to insert it into its left operand.

Remaining code is straightforward

The remaining code in Listing 4 is straightforward and shouldn't require further explanation.

The getAvg function

The getAvg function is shown in its entirety in Listing 5.

  double getAvg(long array[],int size){
    int count = 0;
    double sum = 0;
    while(count < size){
      sum = sum + array[count];
      count = count + 1;
    }//end while loop
    return sum/size;
  }//end getAvg
  
Listing 5

Purpose of the function

The purpose of this function is to compute and return the average of the values stored in all of the elements in the array.

Note that even though the elements in the array are integer values of type long, the average value isn't necessarily an integer.  Therefore, the average is computed and returned as type double.

Add all the element values

The approach is straightforward.  An accumulator variable of type double named sum is declared and initialized to a value of zero.

Then a while loop is used to iterate on the array, fetching the value of one element during each iteration of the loop, and adding that value to the cumulative sum.

After all of the element  values have been fetched and added to the accumulator, the loop terminates.

Divide by the number of elements

At this point, the function divides the sum by the number of elements and returns the resulting value as type double.

For the specific set of user input values shown in Figure 1, the average value was -0.4.

Obviously, the average would be different for a different set of user input values.

The getMin function

The getMin function is shown in its entirety in Listing 6.

  long getMin(long array[],int size){
    long min = 2147483647;
    int count = 0;
    while(count < size){
      if(array[count] < min){
        min = array[count];
      }//end if
      count = count + 1;
    }//end while loop
    return min;
  }//end getMin
  
Listing 6

Purpose of the function

The purpose of this function is to find the algebraic minimum value stored in the array and to return that value as type long.

Set up a trial minimum value

The function begins by declaring a variable named min and initializing that variable with a trial minimum value of 2147483647.  This is the largest possible value that can be stored in an array element of type long.  Therefore, every value in the array must be less than or equal to that value.

Fetch and compare

Then the function uses a while loop to:

Possibly replace

If the value fetched from an element is algebraically less than the current value stored in min:

When all elements have been processed ...

When all of the array elements have been examined and processed in this manner, the variable named min will contain the minimum value of all the values stored in the array.

This value is returned by the function.

For the specific set of user input values shown in Figure 1, the minimum value was -100.

The getMax function

The getMax function is shown in its entirety in Listing 7.

  long getMax(long array[],int size){
    long max = -2147483648;
    int count = 0;
    while(count < size){
      if(array[count] > max){
        max = array[count];
      }//end if
      count = count + 1;
    }//end while loop
    return max;
  }//end getMax
  
Listing 7

Purpose of the function

The purpose of this function is to find the algebraic maximum value in the array and to return that value as type long.

Very similar to getMin function

This function is very similar to the previously-discussed function named getMin.

The main differences between the two functions are:

When all of the elements have been processed ...

When all of the array elements have been examined and processed in this manner, the variable named max will contain the maximum value of all the values stored in the array.

This value is returned by the function.

For the specific set of user input values shown in Figure 1, the maximum value was 99.

Complete Program Listing

A complete listing of the program discussed in this lesson is shown below.
 
/*File:  Array01.cpp
This C++ program illustrates the use of a simple
one-dimensional array in C++. 

The program asks the user to enter five integer
values in succession.  It stores the values in
reverse order in an array.  Then it displays the
contents of the array in forward order.

Then it computes and displays the average of the
values stored in the array, the minimum value
stored in the array, and the maximum value 
stored in the array.

The program displays the following output on the
screen for a typical set of input data:

Enter an integer value: -100
Enter an integer value: -50
Enter an integer value: 0
Enter an integer value: 49
Enter an integer value: 99
Array contents:
99 49 0 -50 -100
Average: -0.4
Minimum: -100
Maximum: 99

************************************************/

#include <iostream>
using namespace std;

class Array01{ 
  public:
  static void classMain(){
    Array01* ptrToObject = new Array01();
    ptrToObject -> doSomething();
  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Array01 class
  void doSomething(){
    const int size = 5;
    long values[size];
    //Populate the array in reverse order.
    getData(values,size);
    //Display contents of the array in forward
    // order.
    cout << "Array contents:" << endl;
    displayData(values,size);
    //Compute and display average, minimum, and
    // maximum values.
    cout << "Average: " << getAvg(values,size)
                                         << endl;
    cout << "Minimum: " << getMin(values,size)
                                         << endl;
    cout << "Maximum: " << getMax(values,size)
                                         << endl;
  }//end doSomething function
  //-------------------------------------------//

  void getData(long array[],int size){
    int count = size - 1;
    while(count >= 0){
      cout << "Enter an integer value: ";
      cin >> array[count];
      count = count - 1;
    }//end while loop
  }//end getData
  //-------------------------------------------//

  void displayData(long array[],int size){
    int count = 0;
    while(count < size){
      cout << array[count] << " ";
      count = count + 1;
    }//end while loop
    cout << endl;
  }//end displayData
  //-------------------------------------------//

  double getAvg(long array[],int size){
    int count = 0;
    double sum = 0;
    while(count < size){
      sum = sum + array[count];
      count = count + 1;
    }//end while loop
    return sum/size;
  }//end getAvg
  //-------------------------------------------//

  long getMin(long array[],int size){
    long min = 2147483647;
    int count = 0;
    while(count < size){
      if(array[count] < min){
        min = array[count];
      }//end if
      count = count + 1;
    }//end while loop
    return min;
  }//end getMin
  //-------------------------------------------//

  long getMax(long array[],int size){
    long max = -2147483648;
    int count = 0;
    while(count < size){
      if(array[count] > max){
        max = array[count];
      }//end if
      count = count + 1;
    }//end while loop
    return max;
  }//end getMax
  //-------------------------------------------//

};//End Array01 class
//---------------------------------------------//

int main(){
  Array01::classMain();
  return 0;
}//end main

Listing 8


Copyright 2005, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java, C#, and XML. In addition to the many platform and/or language independent benefits of Java and C# applications, he believes that a combination of Java, C#, and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Programming Tutorials, which have gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.

In addition to his programming expertise, Richard has many years of practical experience in Digital Signal Processing (DSP).  His first job after he earned his Bachelor's degree was doing DSP in the Seismic Research Department of Texas Instruments.  (TI is still a world leader in DSP.)  In the following years, he applied his programming and DSP expertise to other interesting areas including sonar and underwater acoustics.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com

-end-