C++ Arrays

    An Array is a collection of similar data types (homogenous data type) elements that store at a contiguous memory location and using the indices or index values we can access the random element from that collection. An array is only capable of storing similar data type which means if we have an array of integer, so only integer values would be able to store in that array nither float nor double the only integer. An array can store primitive data types such as int, float, double, char, etc. and if we have an array of character, we call it a string.

    Why use an array?

    Suppose if we want to store multiple values of the same data types in our program so instead of creating a different variable for the different values, we can create a single array that is capable of storing multiple values.

    Declaring an Arrays

    Like other variable declaration, we also need to define the array data types with its name. Along with array data type and its name we also need to define its size. The size of the array defines the amount of storage occupied by that array in the memory. Syntax

    data_type array_name[array_size];

    Example

    int arr[10];

    Behind the code Here in this example, we have defined an integer array arr[10], and its size is 10, which means it can store 10 different integer values, and using the name arr and corresponding index value we can access any value from the array.

    Array Initialization

    In array initialization we add values in the array, there are two methods by which we can add elements in an array.

    • Initializing values during the declaration
    • Initializing values after declaration.

    Array Initializing during Declaration

    We do not use this technique so often to initialize the elements in the array. In this technique when we declare an array along with it, we also initialize the elements of the array using curly brackets. Example

    #include <iostream>
    using namespace std;
    
    int main()
    
    {             
        int arr[6] ={ 100,200, 300, 400, 500,600}; //initializing an array
        return 0;
    }// its just an initialization so there is no output

    Behind the code In this above code, we have initialized an array by name arr and its size is 6, which means it can only hold 6 values, and here we have also provided it all the 6 values100 ,200, 300, 400, 500,600 .

    Array initialization after the declaration

    In this, we first declare the array with its data type, name and size, and after declaration using the index value, we stored all elements in the array. The index value of array starts with 0 and end with the one less than the total size of the array, for instance, if the array size is 100 then its index value starts from 0 and end at 99. Example

    #include <iostream>
    using namespace std;
    
    int main()
    {             
           int arr[6] ;
           arr[0]=100;
           arr[1]=200;
           arr[2]=300;
           arr[3]=400;
           arr[4]=500;
           arr[5]=600;
    
           return 0;              
    }//  its just an initialization so there is no output

    Behind the code This example is equivalent to the example where we have initialized the array during declaration.

    Accessing Array Elements

    To access the elements of the array we use the corresponding index value of the element. To access the element, we use the array name, and index value inside the square brackets. Example

    #include <iostream>
    #include<cmath>
    #include<stdlib.h>
    using namespace std;  
    int main()
    {             
       //declaration
          int arr[6] ;
     //initialization
          arr[0]=100;
          arr[1]=200;
          arr[2]=300;
          arr[3]=400;
          arr[4]=500;
          arr[5]=600;             
          cout<<"The value which is stored at 0 index is: "<< arr[0]; // to print the element which is at 0 index
          cout<<endl;       //to print new line
          cout<<"The value which is stored at 5 index is: "<< arr[5];
          return 0;
    }

    Output

    The value which is stored at 0 index is: 100
    The value which is stored at 5 index is: 600

    Behind the code In this example, we have to access the 0 and 5 index values of the array by using arr[0] and arr[5] statements.

    Advantages of Array

    • We can randomly access any element of the array
    • Easy to access all the elements
    • No need to create separate variables for similar data types.
    • Elements store at the contiguous memory location.

    Disadvantages of Array

    • Only a fixed number of elements can be stored.
    • It can only hold similar data type elements.
    • Insertion and deletion of an element between the array could be expensive, in terms of time complexity.
    • Fixed-size of an array can be a disadvantage.

    Summary:

    • An array is a collection of similar data types elements.
    • Array uses corresponding index values for each element.
    • The array of characters is known as string
    • The index can be used to access the element from the array.

    People are also reading: