Virtual Classes Logo          Virtual Classes
    Read Anytime,Anywhere


Array In C++

An array is a group of similar types of items (items are of the same nature).
Array elements are placed in contiguous memory locations that can be individually referenced by an index. Index is an uniquie identifier used to get data stored in array. Hence, if we want to store five int/string variables, we can create an array of int/string without having to declare five different variables.We can acccess different array variable by array index.
Note:Array can store same type of values only.It means , an integer array can store int values only.
Lets understand this with a simple example,
A sociery has no of houses and each house has a no which we call house no. We can cosider sociery as array of house which store house and house no is an index to find a house in a society.

Syntex-
1-DataType ArrayName [dimension];
Here type is a valid type (such as int, float...), name is the name of array which we are creating and the elements field specifies the length of the array in terms of the number of elements the array will store.
Array varibale can also be initialized,as we initialize other variable.
2-DataType ArrayName[dimension] = { element1, element2, …, element n};
As the syntex shows,first we are declaring the array and then initilizing this with elements(right side of equal to operator)
The elements, also called items, that compose the array are included between an opening curly bracket '{' and a closing curly bracket '}'. Each item is separate from the next by a comma operator. It is not necessary to give dimension to an array if we are initilizing it.
In this scenario the size of an array is equal to no of elements in the initilization list.
In C++, the first element in an array is always numbered with a zero (not a one), no matter its length. Like a regular variable, an array must be declared before it is used.


Example-
int societyHouse[10];
In this example, we have declared an array values named societyHouse which is integer type and and will store 10 integer variable.

Here are examples of declaring an initializing arrays:
1- string studentNames[5] = {"A","B","C","D","E"};
2-string studentNames[] = {"A","B","C","D","E"};
string array of dimension 5.
3-int marks[5] = {50,45,40,35,30};
4-double weight[] = {44.1, 72.52, 96.8, 68.7, 60.2};
To get the value of the 5rd element of the array, you would type weight[4].
fifth elements index is 4 because index starts from 0.
position of an item is also called its index. The first member of the array, the most left, has an index of 0. The second member of the array has an index of 1. Since each array has a number of items which can be specified as n, the last member of the array has an index of n-1
Types of Array-
1-Single Dimentional Array-All previous example and syntex,we have read above.
2-Multidimentional Array-Multidimensional arrays can be described as "arrays of arrays".
Syntex DataType ArrayName[dimension][dimension];