c++ Arrays
Arrays in c++
In this article, we will learn about the concept of the array in cpp.you will learn, how to declare, initialize,and access the elements of the array in c++ programming.
An array is a series of the element with the same data type placed continuously in the memory.
The concept of the array understands in this way that we need five variables with the same data type.
we declare five variable and use in the code, but CPP language gives to a programmer to use an array instead of using five different variables.
An array of size five works the same as five different variables but, it is easy to use and understand. There is the only difference in the array of size five and five variables is that variables place in different locations in the memory but the array placed five variables in a continuous manner in the memory.
How to declare an array?
The declaration is the same as the declaration of variables but with the given size of the array. Following is an array with name arr with size 5.
Int arr[size of array]
Int arr[5];
How to initialize the array in cpp?
By default, array elements are uninitialized. This means none of the elements of the array set to any particular value .we can initialize at the time of declaration of the array.
For example:
Int Ar[5]={10,20,30,40,50};
initialized values must be in the brackets{}. The above statement declares the array of the name Ar with the size of 5 and int data type.
Graphic representation:
In the initialization take care of values in brackets{} are not exceed from the size of the array. In the above example Ar, write 5 values in the brackets. These values are less than the size of the array but not greater than the size of the array. If values less than the size of the array, left elements of array to initialize with the default values, For example:
Int Ar[5]={10,20,30};
If initialize with no value then each element initializes with zero. for example
int Ar[5]={};
In the case, when the size of array is not written in the bracket[], then c++ automatically creates an array with a number of initialization values write in the brackets. for example:
Int Ar[]={10,30,40};
In the c++, there is a size of Ar is three because of three initialize values write in the curly brackets{}.
How to access the values of the array in c++?
Accessing the values :
Every value of array access like accessing the value of the variable. In the previous example use Ar array with 5 sizes.Actually,there are 5 variables named as Ar[0],Ar[1],Ar[2],Ar[3],and Ar[4].If we want to access the first index of the array then we shall use Ar[0]. The value of the index also changes like a regular variable.
Ar[0]=5;
The value of the index can also store in the variable.
int x=Ar[0];
After assigning the value to x, the value of x becomes 5.
Take care of the data type of array and variable during assigning the value to a variable . The data type of variable and array must be the same.
In the c++, there is no error in accessing the value exceed the limit of the array like Ar[6]. This error not come during the compilation but cause an error during the run time. The reason behind this error we shall learn on the topic of pointers.
For more understanding, following are the solved examples by using an array
Example1
I thought it would be tough
ReplyDeleteBut you made it simple for me so a big "Thank You" from depth of my heart.
you welcome
Deleteits heaped for me. thank you.
ReplyDeletealways welcome
Delete