Arrays in C++:-
In C++ programming, Arrays are the collection of the consecutive memory locations with same name and similar address in a free store or heap. These collections of consecutive memory locations with similar name and address are called Arrays.
Types of Arrays:-
There are mainly two types which are as follows:-
1. Integer array.
2. Character array.
4. Multi-Dimensional Array.
Advantages of using Arrays:-
In C++ they are used too much due to the following advantages.
1. The large number of values could be stored in them with a single name.
2. By using them it becomes very easy to process and access a large number of different values.
3. The huge amount of values could be stored in them easily.
4. The values stored in arrays could be searched and sort easily.
Integer Arrays:-
They are the most commonly used arrays in C++. These are purely used for the purpose to store integers in the consecutive memory locations. They could be assigned during the initialization process or they could be stored by using a loop.
How to declare One-Dimensional integer Array:-
It consists on only one row and one column. They are simply declared by typing a keyword Int before their name and then the size of an array in brackets.
Syntax of One-Dimensional integer Arrays:-
(Data type) (Name) [size];
For example:-
Int boller[15];
When we type the above statement the compiler will reserved a 15 consecutive memory locations for the integer array boller.
How to store values in One-Dimensional integer Array:-
There are three methods to store values in them.
1. First method is by Initializing the values during the process of declaration.
2. Second method is by assigning values to each memory location by accessing each location of an array.
3. Third method is by using a loop. This method is the most commonly used method to store data in arrays. Because by this method the huge amount of values could be stored easily in a shorter time.
Initializing the values during the process of Declaration:-
The process of assigning values to an array during the process of declaration is called initialization. To initialize them the programmer has to write values in curly braces, separating each value by comma and assigned it to array.
For Example:-
Int Onum [5] = {43, 44, 56, 72, 49};
By typing above statement the compiler creates an integer array named Onum and initializes it with the above values.
Assigning values to each memory location by accessing each location:-
The values could be assigned individually to each memory location of arrays.
For Example:-
Int Onum [5]; Onum [0] = 43; Onum [1] = 44; Onum [2] = 56; Onum [3] = 72; Onum [4] = 49;
Initializing array by using a Loop:-
This is the most common method of initializing them. This method is very useful for storing a large amount of values.
For Example:-
Int Onum [5]; For(int q1=0 , q1<5, q1++) { Cout<<"Enter the numbers :"; Cin>>Onum [q1]; }
By typing the above code the program will take 5 inputs from the user and store them in an array Onum.
How to take Input in One-Dimensional Array:-
The process of taking input in them is same as we discussed in above in initializing array by loop. However the process of output is also same by the addition of the cout statement in the above program.
For Example:-
Int Onum [5]; // For taking Input For( int q1=0 , q1<5, q1++) { Cout<<"Enter the numbers :"; Cin>> Onum [q1]; } //For Output For( int q2=0 , q2<5, q1++) { Cout>> Onum [q2]; // The Addition statement }
Character Arrays:-
In C++ they are purely used to store the alphabets or special characters in the consecutive locations of a memory. The alphabets or special characters could be assigned during the initialization process or they could be stored by using a loop.
How to declare One-Dimensional character Arrays:-
They consist of only one row and column. They are simply declared by typing a keyword char before the name of an array and then the size in brackets.
Syntax:-
(Data type) (Name) [size];
For example:-
char vovel[6];
When we type the above statement the compiler will reserved a 6 consecutive memory locations for the array vovel.
How to store characters in character array:-
The method of storing vales in them is same as we discussed for the integer array with a slight change of putting apostrophe around every alphabet during initialization.
For example:-
Char Vovel [5] = {‘a’, ‘e’, ‘I’, ‘o’, ‘u’};
Two-Dimensional Arrays in C++:-
In C++ Two-Dimensional arrays are those which store data in a form of a matrix. It is declared by specify two indexes in a bracket, First index indicates towards a row and the second index indicates towards the column of a matrix.
Syntax of 2-D Array:-
(Data type) (Name) [No. of rows] [No. of columns];
For example:-
Int twoDim [4][4];
When the above statement will be typed the compiler will automatically generate a 2-Dimension array of 4 rows and columns.
[yop_poll id=”6″]
Multi-Dimensional Array:-
The arrays with more than two dimensions are called a Multi-Dimensional Arrays. There could be infinite dimensions. Disadvantage of these arrays are they occupy too much space. Number of nested Loops for input and output of Multi-Dimensional Array depends upon there dimensions.
Syntax of Multi-Dimensional Array:-
(Data type) (Name) [a] [b]…… [n];
The constant expressions a, b and n shows the number of dimensions.
If you like my work and wanted to read my other articles then visit my Homepage.
@ 2014 HellGeeks.com