×
>
<

Aptitude

Arrays in C++

Definition: An array is defined as a collection of items stored at contiguous memory locations under the same name

Ex: int (a,b,c,d,e )can be grouped in a single variable as int a[5]: now five continuous memory location are assigned with the same name ‘a’.

Instead of creating separate variables to store data, it is an efficient organization to store data in a single variable called array

Basic properties of Array
  • Array index always starts with zero and ends with n-1 (n is the size) ex:int a[5] has the index range a[0],a[1],….a[4]
  • An array is homogenous: It stores elements of the same type only ex: int a[5], integer array stores only integer elements

Ways to define an array

Array definition by specifying the size

  
int a[5];  
  

As per C99 versions, we can also declare an array of user-specified size

  
int n = 5; 
int a[n]; 
  

Array definition by initializing elements

  
int a[] = { 10, 20, 30, 40,50 } ;
  

Here size corresponds to the number of elements initialized, hence size=4.

Array definition by specifying size and initializing

  
int a[5] = { 10, 20, 30, 40 ,50};
  
C++ Array Example:-
  
#include "iostream"
using namespace std;
int main()
{
   int a[5]={6,9,1,5,3};//direct declartion and initialisation
   cout << "elements are:\n";
   for(int i=0;i<5;i++)// iterate all the elements
   cout << "a[" << i << "]=" << a[i] << "\n";//access though index
}
  
output
  
elements are:
a[0]=6
a[1]=9
a[2]=1
a[3]=5
a[4]=3 
  

Advantages

  • Reduces program size
  • Reduces access time
  • Efficient data organization

Disadvantages

Array size is static, due to which there may be chances of wastage and shortage of memory

Array elements are addressed sequentially

  • The address of each block will be sequentially located to access the memory blocks by using pointer
  • Generally, normal user access the array data with subscripted index i.e a[n] internally compiler will access by address and pointers

From the below example it is clear that a[0] is stored at location 0x6ff20 ,as it is an integer array element takes 4 bytes i.e a[1] at address 0x6ff24 and a[2] at address 0x6ff28 and so on

Example:-

  
#include "iostream"
using namespace std;
int main()
{
  int a[5]={6,9,1,5,3};
  cout<<"elements are:\n";
  for(int i=0;i<5;i++)
    cout<<"a["<<&a[i]<<"]="<< a[i] <<"\n";
}
  
output
  
elements are:
a[0x6ff20]=6
a[0x6ff24]=9
a[0x6ff28]=1
a[0x6ff32]=5
a[0x6ff36]=3
  
C++ program to read array of size n and find the frequency of the given element
  
#include
using namespace std;
int main()
{
  int n;
  cout<<"how many elements?:";
  cin>>n;
  
  int a[n],count=0,key;
  for(int i=0;i>a[i];
  }
  cout<<"enter an element to find its frequency:";
  cin>>key;
  
  for(int i=0;i < n; i++){
    if(a[i]==key)
    count++;
  }
  cout << " frequency of " << key <<" is " << count;
}
  
output
  
how many elements?:6
2 3 4 4 5 6
enter an element to find its frequency:4
frequency of 4 is 2
enter an element to find its frequency:3
frequency of 3 is 1
enter an element to find its frequency:9
frequency of 4 is 0
  
Additions Facts about Arrays

An array index cannot be negative or zero

  
a[-2];//invalid
a[0];//invalid
  

Unused space in the array is always filled with zeros

  
int a[5]={1,2,3};//a[3],a[4] are filled with zeros
  

All array elements are Garbage values before initialization of values

  
int a[5];//a[0]...a[5] are garbage values
  

Declaring an array without size is invalid

  
int a[];/invalid
  

The below array declaration sets all elements in the array to 0

  
int a[10]={0};
  
Multi Dimensional Array

A multi-dimensional array or a 2-D array is a similar to 1-D array but the element stored in a 2-D array are stored in a matrix format rather than a linear manner.The elements are stored in rows and columns like a matrix.

The simplest form of multi-dimensional array is 2-D array.

 

Initializing of 2-D Array:-

Example :

  
 a[2][1];
  

  • a[2][1] element is located at index 2nd row and 1st column
  • A two – dimensional array can be seen as a table with ‘m’ rows and ‘n’ columns where the row number ranges from 0 to (m-1) example: array a[3][3] ranges from i.e a[0][0] to a[2][2]1

  
#include <iostream>
using namespace std;
int main ()
{
  // an array with 5 rows and 2 columns.
  int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; 
  for ( int i = 0; i < 5; i++ )//iterate rows
   for ( int j = 0; j < 2; j++ ) //iterate columns
   {
     cout << "a[" << i << "][" << j << "]: ";
     cout << a[i][j]<< endl;
   }
}
  

Here’s the array will be like:-

  
a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8