Introduction to Arrays in C++(Part-1)

What is Array?

Array is a data structure which is used to store multiple values of similar data type in contigous memory locations.

Array is a linear data structure as elements are stored in contigous memory locations.

Linear data structure are type of data structure where elements are stored sequentially.


Why Array is needed?

Let's suppose we want to find maximum of 2 numbers.In this case,we can create 2 variables and find maximum between these 2 numbers.

int num1,num2,maxi;
cin>>num1;
cin>>num2;
maxi = max(num1,num2);
cout<<maxi;

Above code describes maximum of 2 numbers.Here we are creating 2 variables num1,num2 for storing the numbers and storing maximum number under maxi variable.

Let's say if we need to find maximum of 1000 numbers.Then we need to create 1000 variables for storing the numbers and finding maximum among them.It is a very time consuming process or we can say that it is a challenging task to perform using variables.

We can just create an array of required size and can just loop through the array to find the largest element of an array.


Array declaration

data_type name_of_array[size_of_array];

Example: 1) int arr[4];

In above example,

  • data type is integer

  • name of array is arr

  • array contains 4 elements

2) char a[5];

In above example,

  • data type is character

  • name of array is a

  • array contains 5 elements

3)float c[7];

In above example,

  • data type is float

  • name of array is c

  • array contains 7 elements.


Array initialisation

We can initialise the array with or without size in C++ but when we declaring an array,it is mandatory to pass the size.Let's understand with the help of example:

int arr[] = {1,2,3,4,5,6}

Above code is valid as when we are initialising the array,passing the size is not mandatory.

int arr[];

Above code is invalid as when we are declaring an array,passing the size is mandatory.

In above image,you can see the error is shown as storage size of arr is not known.


Accessing and modifying elements in an array

We can access the elements of an array using syntax given below:

data_type name_of_array[index];

In arrays in C++,first element is present at index 0,second element is present at index 1,third element is present at index 2 and so on.

If there are 'n' elements in an array,then range of an index of an array

is (0 to n-1)

Now let's understand accessing elements of an array with the help of an example:

int arr[4] = {10,20,30,40};
cout<<"First element of an array is "<<arr[0]<<endl;
cout<<"Second element of an array is "<<arr[1]<<endl;
cout<<"Third element of an array is "<<arr[2]<<endl;
cout<<"Fourth element of an array is "<<arr[3]<<endl;

Above image represents output of a code written above.Here you can see we can access elements of an array using indexes.

We can update elements of an array in C++ by modifying the value of their indexes.

arr[0] = 15; //Modifying first element of an array
arr[1] = 30; //Modifying second element of an array
arr[2] = 40; //Modifying third element of an array
arr[3] = 70;//Modifying last element of an array
cout<<arr[0]<<endl;
cout<<arr[1]<<endl;
cout<<arr[2]<<endl;
cout<<arr[3]<<endl;

Above image represents output of above code.Here we can see elements of an array are modified.

Did you find this article valuable?

Support Divyanshu Jain by becoming a sponsor. Any amount is appreciated!