How to use Array in C++
Arrays are the collection of fixed size , same type of elements in a sequential order. These elements are basically variables of same data type . They can be acessed via indexing i.e they are alloted some number through which each element in an array can be acessed .
Each element takes a memory block to store its data in it .Similary array also takes many memory blocks to store its data depending upon its arraysize i.e
number of memory blocks allocated=array size
Consider the following figure :
SYNTAX
datatype name_of_the_array[arraysize];
here datatype can be build in type like int, char, float, etc or user defined type about which we’ll discuss later . Now for writtin this name of the array we need to follow certain rules and regulations :
- Hahaha got scared dont worry the rules of writting name of an array is same as writting the name of a variable , if you are still forgetting about it then don’t worry you can view them on this link —–> link
so , lets continue now comes the array size , this is a numeric value and tells about the size of the array i.e how many elements can be inserted in an array .
Now the question that maximum of you might have ……
Can the size of an array be negative????
You can even make the user decide what should be the size of the array .Remember the size of the array tells the compiler how many blocks of the memory should be reserved for that particular array .
consider the following example , this will help you to undersatnd better …
Can we use negative index to access an element ?
Yes we can use a negative index to access an element .
consider the following example , this will help you to undersatnd better …
EXAMPLE
int arr[100];
Here we decleared an array of name arr and of size 100 .
INITIALIZING AN ARRAY
There are 3 ways to initialize an array
- By directly initialing the array , i.e programmer initialize the array with arraysize:
SYNTAX
datatype name_of_the_array[arraysize]={dataelement1, dataelement2,....} ;
EXAMPLE
int arr[4]={10,28,28,39};
here the size is mentioned and dataelemts are directly added by the programmer to the array.
- By directly initialing the array , i.e programmer initialize the array without arraysize:
SYNTAX
datatype name_of_the_array[]={dataelement1, dataelement2,....};
EXAMPLE
int arr[]={10,28,28,39};
here the size is not mentioned and dataelemts are directly added by the programmer to the array.
- By indirectly initializing the array , i.e user initialize the array, this can be done by using loops :
SYNTAX
datatype name_of_the_array[arraysize];
By using for loop
for(int variable=0 ; variable<arraysize ; variable++) { cin>>name_of_the_array[variable]; }
By using while loop
int variable=0; while (i<10) { cin>>name_of_the_array[variable]; variable++; }
EXAMPLE
consider the following example
TO VIEW ARRAY ELEMENTS
consider the following program:
MULTIDIMENTIONAL ARRAY
Uptil now we were talking about single dimentional array ,i.e here only one dimension was taken into account . A multidimentional array is also known as array of array.consider the following figure , you’ll understand better.
DISADVANTAGES OF AN ARRAY
- Since the size is fixed hence if the array is full , one can not add any more elements to it .(Solution create a new array of bigger size and copy the elements of the old array in the new array and further you can add more elements to it .
- consider the following program )
- Again since the size is fixed hence if the number of data elements added is less than the size of the array then lot of memory blocks will be wasted . Hence pointers should be used (In this the allocation of memory blocks is done dynamically i.e during runtime only the memory blocks are allocated . )