2018-10-12 04:30:38 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: C++ Arrays
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## What are Arrays?  
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								An array is a series of elements of the same data type which are stored in contiguous memory locations and can be referenced individually.  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-30 00:58:55 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Declaration:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								dataType arrayName[arraySize];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 13:11:01 +05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For example, an array containing 5 integer values called numbers is declared like so:  
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 04:30:38 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```C++
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								int numbers [5];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Initializiation:  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```C++
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//Initialization with entries:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								int numbers [5] = {1, 2, 3, 4, 5};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//Initialization with no values:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								int numbers [5] = {};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//Initialization with declaration:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								int numbers [] = {1, 2, 3, 4, 5};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//Note that here the number of values defines the size of the array.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//In the examples above, the size was fixed beforehand
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-16 00:17:06 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Types Of Arrays
  
						 
					
						
							
								
									
										
										
										
											2018-10-30 00:58:55 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								There are two types of arrays based on the way we declare it.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 04:30:38 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-16 00:17:06 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								**1**. Static array:
							 
						 
					
						
							
								
									
										
										
										
											2018-10-30 00:58:55 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Those arrays whose size is defined before compile time like in the examples above, are called static arrays. In these arrays we can't change their size once they are declared.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-16 00:17:06 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**2**. Dynamic array:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Dynamic arrays are those arrays, whose size is not known at compile time and we can define their size at run time. These arrays are created by using **new**  keyword and when done with that array we can delete that array by using the **delete**  keyword.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 04:30:38 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Access:  
  
						 
					
						
							
								
									
										
										
										
											2018-10-12 13:11:01 +05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Elements from an array can be accessed via reference of their position in the array. (Start counting from 0).  
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 04:30:38 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Example:  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```C++
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								x = numbers[0]; // = 1. [0] == first position
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								numbers[2] = 55; // Sets the third position (3) to the new number 55
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//numbers[] is now: {1, 2, 55, 4, 5}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-30 00:58:55 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								How to insert and print array elements:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```C++
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								int vnum[5] = {1, 2, 3, 4, 5}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// change 4th element to 9
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								vnum[3] = 9;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// take input from the user and insert in third element
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								cin >> vnum[2];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// take input from the user and insert in (i+1)th element
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								cin >> vnum[i];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// print first element of the array
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								cout < <  vnum [ 0 ] ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// print (i)th element of the array
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								cout >> vnum[i-1];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```