Name: _____________________ Class: Comp 217
SSN/ID:   _____________________ Section & Group: ____________
One Dimensional Arrays


Highlights of this lab:

In this lab you will see one dimensional arrays. The objective of this lab is as follows:

Lab Exercise:

NOTE: Exercises 1 and 2 will be marked in the lab.


Back to Highlights

Array Declaration

A one-dimensional array is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value.

Here is the syntax template of a one-dimensional array declaration:

	DataType ArrayName [ConstIntExpression];
In the syntax template, DataType describes what is stored in each component of the array. Array components may be of any type, but for now we limit our discussion to simple data types (e.g. integral and floating types). ConstIntExpression indicates the size of the array declared. That is, it specifies the number of array components in the array. It must have a value greater than 0. If the value is n, the range of the index values is 0 to n-1. For example, the declaration
	int number[50];
creates the number array, which has 50 components, each capable of holding one int value. In other words, the number array has a total of 50 components, all of type int.

Back to Highlights

Fundamental Operations on a One-dimensional Array

Now let's look at how to access individual components of an array. The syntax template for accessing an array component is
	ArrayName[ IndexExpression ]
The index expression must result in an integer value. It can be of type char, short, int, long, or bool value because these are all integral types. The simplest form of index expression is a constant. For example:
	number[0]  specifies the 1st compnent of the number array
	number[1]  specifies the 2nd compnent of the number array
	number[2]  specifies the 3rd compnent of the number array
	number[3]  specifies the 4th compnent of the number array
	number[4]  specifies the 5th compnent of the number array
		.
		.
		.
	number[48]  specifies the 2nd last component of the number array
	number[49]  specifies the last component of the number array
To store values in the number array, we can do the following:
	for (int i=0; i < 50; i++) {
	    number[i]=i;      // Store a number in each array element
	    cout << "number[" << i << "] = " << number[i] << endl;
	}
Each array element can be treated as a simple variable. Here, an integer value is assigned to each array element, which has been declared to hold data type int.

To use the values stored in the number array, we can treat each array element as a simple variable of data type int. For example:

	for (int i=0; i < 50; i++) {
	    number[i]=2*number[i];  // Double the value in each array element
				    // and store it in the array element
	    cout << "number[" << i << "] = " << number[i] << endl;
	}

For the number array, the valid index range is from 0 to 49. If the IndexExpression results in a value less than 0 or greater than the array_size - 1, then the index is considered to be out-of-bounds. For instance, number[50] is trying to access a memory location outside of the number array.

Back to Highlights

Array Initialization in its Declaration

A variable can be initialized in its declaration. For example:

	int value = 25;
The value 25 is called an initializer. Similarly, an array can be initialized in its declaration. A list of initial values for array elements can be specified. They are separated with commas and enclosed within braces. For example:
	int age[5] = {23, 56, 87, 92, 38};
In this declaration, age[0] is initialized to 23, age[1] is initialized to 56, and so on. There must be at least one initial value between braces. If too many initial values are specified, a syntax error will occur.

Please Note: If the number of initial values is less than the array size, the remaining array elements will be initialized to zero.

In C++, the array size can be omitted when it is initialized in the declaration. For example:

	int age[] = {23, 56, 87, 92, 38, 12, 15, 6, 3};
The compiler determines the size of the age array according to how many initial values are listed. Here, the size of the age array is 9.

Back to Highlights


Lab Exercise — One-dimensional Array

Exercise 1: (This exercise will be marked in the lab.)

Exercise 2: (This exercise will be marked in the lab.)

Back to Highlights

Hand In: This lab handout with the answers filled in attached to a listing of your final exercises
(use the enscript command from your Programming Style Sheet to print it out:
enscript -E -G -2rj -M Letter -PECT2_PS <filename> or a2ps <filename>).


Ricky J. Sethi <rickys at sethi.org>
Last modified: Wed Oct 5 14:25:56 PDT 2005