Last Updated: Oct 13, 2023
Easy

Introduction to Array

Shivam Verma
1 upvote

Introduction

An array is a data structure that sequentially stores an element of the same data type. In C/C++ or any other programming language, an array is a collection of similar data items. The data items are always stored in an array at contiguous memory locations. The element of the array can be accessed randomly using the indices of an array.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element of the array and the highest address to the last element of the array.

Also see, Literals in C.Fibonacci Series in C++

Why do we need an array?

Arrays are a very useful data structure in any programming language. Using an array, we can store the values of more than one variable of the same data type together instead of storing these values separately. We can access every value of the array individually. For example, if you want to store more than one integer type of data, you can store them separately by taking integer type variables. But it is not an excellent method to store these values. We can store these values easily by using an array.

Syntax of an array declaration

``type array_name[array_size]; ``

In the above syntax, type means the data type and array_name is the name of the array you want to create, and array_size is the number of the element you want to store in the array.

For example

int marks[6];

Here int is a data type, and the 'marks' is the array's name, and 6 is the size of the array.

Types of array declaration in C++

• Declaration of an array by specifying the size
• Declaration of an array by initialising elements
• Declaration of an array by specifying the size and initialising elements

Declaration of an array by specifying the size

Code

``````// declaration of an array by specifying size
int arr1[5];
// another way of declaration of an array by specifying size
int n=5;
int arr2[n];
``````

In the above code, the compiler creates an array of size 5. The elements of the array are not initialized.

Declaration of an array by initializing elements

Code

``````// declaration of an array by initializing elements
int arr[] = {15, 25, 35, 45, 55}``````

In the above code, the compiler creates an array of size 5. The Elements of the array are {15, 25, 35, 45, 55}.

Declaration of an array by specifying the size and initializing elements

Code

``````// Declaration of an array by specifying size and initializing elements
// elements
int arr[7] = { 15, 26, 28, 60,45 }``````

In the above code, the compiler creates an array of size seven and then initializes the first five elements as specified by the user and the rest two elements as 0.

• An array can store various data items of the same data type using a single name.
• Traversal of the array becomes easy using a single loop.
• You can randomly access an element in an array by using the index number.
• Sorting of the element is very easy in an array.
• Array allocates memory in the contiguous memory location, so there is no wastage of memory in the array.

• The number of elements you want to store in an array should be known in advance.
• Insertion and deletion in the array are very difficult because the elements in the array are stored in consecutive order.
• An array is a static data structure so that you can not increase the size once the array is declared.
• Allocating more memory than the requirement leads to the wastage of memory, and less allocation of memory also leads to a problem.

1. What is an array?
Ans: An array is a data structure that sequentially stores an element of the same data type

2. What is the main advantage of using an array?
Ans: By using the array, accessing an element is easy using the index number.

3. What is the index of the first element of the array?
Ans: 0 is the index of the first element of the array.

4. What is the main disadvantage of using an array?
Ans: The main disadvantage of the array is that you cannot increase the size of the array after the creation of the array.

Key Takeaways

That's all about the array. This blog is over, but the thirst for learning is not over yet. If you wish to prepare for technical interviews, you can visit our library of curated articles and blogs by clicking here.

To learn more about Data Structures and Algorithms, you can enroll in our DSA in C++ Course.
This blog provides an overview of arrays; to learn more about arrays, watch the video below and subscribe to Data Structures and Algorithms in C++, a free and comprehensive video series by Coding Ninjas to learn more about each topic in depth.

Till then, have a nice day and Happy Coding!