One-Dimensional Arrays

The fifth element of this array is A[4] = 45 ... C++ vs. Pseudocode void main(). { const int size = 20; int Array[size]={2,3,4,5,6,-13,2,3, 14,22,9,3,0,-2,99,1,1,9,-2,30}; ... This program will initialize the elements in an array and then sum them.
95KB taille 295 téléchargements 334 vues
One-Dimensional Arrays •

Until now, all the data types we have been using have been atomic. That means that individual data items cannot be subdivided into smaller units. A variable of type int, for example, cannot be further broken down into individual digits.



We will now move on to explore compound data types, which are made up of multiple atomic data types. Our first structured data type is the array (also sometimes called a vector).

An array is a data structure that consists of multiple sub-units, each of which is of the same type. For example, an array of integers is a group of integers that are stored together in memory and are referred to by a single name, the identifier of the array. I.e. a collection of ages (each of them is an integer, can be stored in an array and referred to by the name age.)

Array Syntax An array is declared as follows: data-type array-name[ number-of-items ];

NOTE: •

The number of elements in the array must be specified at compile time. In other words, the number-of-items term in the declaration must be a specific integer, not a variable.



It can be either a literal or a named constant (const variable). The reason for this is that the elements of the array are stored contiguously in memory, and the compiler needs to know how much memory to allocate for the array. It must know both the number of elements and the data type (since different data types require different amounts of storage);

1

Example of array declaration: int Age [45]; // array Age consist of 45 integers float Grade[3]; //array Grade consists of 3 floating point values or const int array_size = 50; // do you remember what const qualifier does? int Num[array_size]; //array Num consists of 50 integers •

Each sub-unit of the array is referred to as an element.



Since the entire array is referred to by a single identifier (name), each individual element is accessed using its index or subscript. Each element in the array is numbered consecutively, starting from 0. So the third element in the array Age could be accessed using the code:

Age[2] = 21; Remember the third element is number 2 counting from 0!!! It is really easy to be one number “off” when dealing with arrays. •

Each individual element of an array can be used in any manner a scalar (atomic) variable of the same type could be used. For example, it could be used in an assignment statement as follows:

int num; int bunch_of_nums[10];

//just a number //an array of 10 numbers, indexed from 0 to 9

bunch_of_nums[0] = 25; num = bunch_of_nums[0];

//assign 25 to the first element of the array //set num equal to the first element of the array



It is also possible to access individual elements in an array using a subscript expression that evaluates to an integer, such as:

result = result + value[ i * 5]; cout