Sunday, 13 March 2016

One dimensional array in c++

The declaration of a one dimensional array is analogous to a variable declaration. The number of elements must be an integer. We must use an array to associate two or more memory cells with a unique name. In array declaration, we use the name to be used (any valid identifier) and the number of elements to be associated with this name and the data type, which will be associated for all the declared elements.

For Example:

int item[5];
This declaration provides not only a name for the array and type of data but also specify how many elements will be allocated for that array. The above declaration tells the compiler to associate 5 memory cells with the name item of a single integer data type. Item is the single name of array refers to a collection of objects and each object is of type integer. In the declaration, int is the c++ language keyword followed by the name of the array and then the range of the index in the square brackets, and then followed by a semicolon. At least one space is a must in between the type and array name. We must be careful of referencing variable item and must avoid operations such as
item = 5;
item = item + item;
if (item > 10)
Arrays provide a real facility in many cases, but there are some restrictions, that is, c++ will not do everything automatically like the assignment of one array to another. It is common to loop over all elements of an array but not the direct assignment. In case we have two array item1 and item2 of the same type, it is not permissible to say item1 = item2. There is a way to handle array with the help of the subscript as show in the figure.
Five elements of Array
item [0] = 10; assign 10 into location 1 in the group of 5
item [1] = 20; assign 20 into location 2 in the group of 5
item [4] = 50; assign 50 into location 5 in the group of 5
To access a particular location, we use a subscripted variable. The subscripted variable must contain an integer value between 0 and 4 (in this case) for that variable to exist. Ut the index value is not in the discrete range that is from 0 to 4 then the compiler error will be raised in some languages not in c++ language.
A segment below in c++ language sets each component of array item to zero’s using loop preferably the for loop. The picture of array item initialization to zero’s is shown in the figure.
int I;
for (i = 0; i
item[i] = 0;
Five elements of Array, Item sets to Zero's
This is the example of accessing array elements sequentially. In situation like this, we usually start with the value of initial subscript and end with the last value of subscript. We could initialize all the array elements using while loop but for loop enables us to initialize array elements sequentially very easily and efficiently. Random access to array elements is also allowed in c++ language.
item [3] = 10;
Normally we do not assign and write through hard coding. It is determined by input data that varies from execution to another.

For Example:

Before using the cout, we should first check the input value to see it is in the specified range of the subscript. It must match in the declared subscript range. Otherwise the responsibility will lie on the programmer shoulders. Let us look at another declaration of array, which can contain a character.
char ch[26];
This declaration sets up 26 memory cells, each of which is of type char. The same is true for other data types too.

No comments:

Post a Comment