sábado, 30 de noviembre de 2013

Introduction to Arrays

An array is used to process a collection of data of the same type like a list of names, temperatures, etc.

To declare an array, name score, containing five variable of type int we can use: int score [5];
The value in brackets [] is called a subscript and an index.

The number of indexed variable in an array is the declared size, or size of the array.  An array can have indexed variables of any types and they can be used anywhere an ordinary variable of the base type is used.

To assign a value to an indexed variable we use the assignment operator. For example:

int n = 2;
score[n+1] = 99;

In this example, variable score[3] is assigned 99.

A for-loops are commonly used to step through arrays.  For example:

for (i=0; i<5; i++)                              (The first index is 0, the last index is (size-1))
{
.
.
.
}

We use constants to declare the size of an array.  For example:

const int NUMBER_OF_STUDENTS = 50;
int score[NUMBER_OF_STUDENTS];
...
for (i=0; i<NUMBER_OF_STUDENTS; i++)

cout << score[i] << "off by"
        << (max - score[i] << endl;

Only the value of the constant must be changed to make this code work for any number of students.

Most compilers do not allow the use of a variable to declare the size of an array.  For example:

cout << "Enter numbers of students:";
cin >> number;
int score[number];

This code is illegal on many compilers.

To declare an array, use the syntax:

Type_Name Array_Name [Declared_Size];

Once declared, the array consists of the indexed variables:

Array_Name[0] to Array_Name{Declared_Size - 1]


Arrays in Memory

  • Declaring the array int a[6]
    • Reserves memory for six variables of type int
    • The variables are stored one after another
    • The address of a[0] is remembered
      •  The addresses of the other indexed variables is not remembered
  •  To determine the address of a[3]
    •  Start at a[0]
    •  Count past enough memory for three integers to find a[3]


Array Index Out of Range

A common error is using a nonexistent index
  • If an array is declared as int a[6]; and an integer is declared as:  int i = 7;  executing the statement a[i] = 238 causes:
    • The computer to calculate the address of the illegal a[7]
    • This address could be where some other variable is stored
    • The value 238 is stored at the address calculated for a[7]
    • No warning is given!
Initializing Arrays

To initialize an array when it is declared, the values for the indexed variables are enclosed by braces and separated by commas.  For example:

int children[3] = {2, 12, 1};    

Is equivalent to:

int children[3];
children [0] = 2; 
children [1] = 12;
children [2] = 1;

If too few values are listed in an initialization statement, the listed values are used to initialize the first of the indexed variables and the remaining indexed variables are initialized to a zero of the base type.


If no values are listed in the array declaration, some compilers will initialize each variable to zero of the base type.

Remember that index values for int a[6] are the values 0 through 5.

No hay comentarios.:

Publicar un comentario