Introduction to Arrays
We generally use primitive data types to initialize one value per variable in a simple program. But, in order to save multiple values of data that are of the same type, programming languages have a special concept which is known as Array Data Structure. Arrays are used to store a collection of variables or values which are of similar data type in a single variable. Arrays make the work of searching for a specific type of data very easy. Let us learn more about the Array Data Structure in the below article.
Definition
- An Array in any programming language is a type of Data Structure where a collection of elements that are of similar data types are stored in continuous memory locations.
- Arrays contain a series of elements that are similar in type that are stored in continuous memory locations, which can be accessed by referring to the indices of an array.
Arrays
Arrays are mostly used in cases where a programmer needs to define and store similar types of data in one single variable. Thus instead of declaring multiple variables and store a value in it individually, it is easier to declare one single variable and store various values of the same category in it. This feature is provided by the Array Data Structure. The values are stored in a continuous manner, which makes it easier to fetch them whenever needed. In simple words, Arrays are blocks of static memory whose size must be known to the compiler at the time of execution.
SyntaxÂ
Just like a common variable, Arrays also need to be defined before assigning its values. Arrays can either be one-dimensional or multi-dimensional. To define a one-dimensional Array in C/C++, we use the following syntax :
data_type identifier [array_size] ;
where,
data_type = variable type
identifier = name of an array
array_size = size of array i.e. number of elements in an array
Note 1:Â The array_size must be a valid integer value greater than 0.
Note 2:Â Array indexing always starts from 0.
Example
int arr [5];Â Â Â // Array size of 5 blocks has been created
double amount [10];Â // Array size of 10 blocks has been created
Browse more Topics under Structured Data Type
Types of Array Declarations
There are different methods by which we can Declare and Initialize Arrays in C/C++.
-
Array Declaration by Size
In this method, only the size of an array is defined in the program statement. The initialization can have no values defined.
Example
int arr [10] ; // Array of size 10 is created
int num = 5 ;
int arr2 [num] = {} ; // Array of size 5 is created
-
Array Declaration by initializing elementsÂ
In this method, values are assigned to the variable without specifying the size of the array.
Example
int arr [] = {10, 23, 5, 64, 2} ;  // 5 values are added to the Array ‘arr’
Array Declaration by specifying the size and then initializing elements
Here the size of an array is defined and then the values are assigned to the array. Note, the number of values cannot exceed the array size.
Example
int arr [4] = {5, 10, 15, 20} ;Â //Array of size 4 is created and values are stored
Accessing Array Elements
It is very easy to access an element stored in an Array. The index number written in Square brackets will return only that value that is stored at that memory location in the array.
Example
int main()
{
int val[5] = {2, 24, 64, 4, 75} ;
cout << val[3] ;Â Â // Array Index 3 which contains value 4 will be returned
}
Output
4
For displaying all the elements stored in an array, refer to the following code.
Example
int main()
{
int arr[5] = {5, 10, 15, 20, 25} ;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << '\t';
}
}
Output
5Â Â Â Â 10 Â Â Â Â 15 Â Â Â Â 20Â Â Â Â 25
Types of Arrays
There are 3 Types of Arrays
- One-Dimensional Array – This type of array stores value in a linear fashion. I.e. it stores the value in one dimension only. They are commonly called 1D Arrays.
- Multi-Dimensional Array – Multi-dimensional arrays contain values in any number of dimensions. The most common are 2D Arrays and 3D Arrays.
- Pointer to an Array – A pointer is a variable that holds the address of a specific variable. These pointers can also be used to store the address of an array cell.
Advantages of Arrays
- Accessing Elements – Elements from an Array can be randomly and easily accessed using the indexing method.
- Code Optimization – Few lines of code are required to store multiple elements of the same data type in one variable.
- Traversal is easy – Traversal along an array is very easy.
- Sorting – Values can be sorted easily in an array.
- Memory Allocation – All the elements in an array are stored in continuous memory location. This saves memory as there is no extra memory allocation.
- Arrays can be used as a base Data Structure in order to implement other Complex Data Structures such as Linked List, Stacks, Queues, Graphs, etc.
Disadvantages of Arrays
- Fixed no. of elements – The size of an array cannot be changed once it has been defined at the start.
- Insertion and Deletion of new elements is a tough task to perform.
- Allocation of more memory will create a memory wastage problem and Allocation of less memory will also create storage problems.
- The number of elements to be stored in an array should be known in advance.
FAQs on Introduction to Arrays
Q1. Which of the following array declaration is correct?
- int arr {} ;
- int arr [5] ;
- array {5} ;
- arr [-3] ;
Answer. Option B
Q2. What will be the output of the following program?
int main()
{
  int arr[4]={5, 10, 15, 20} ;
 cout << arr[2] ;
}
- 5
- 0
- 15
- 20
Answer. Option C
Q3. What will be the output of the following program?
int main()
   {
       char str[5] = "ABC";
       cout << str
       return 0;
   }
- ABC
- ABCD
- Compile-time error
- None of the Above
Answer. Option A
Q4. float a [3] = {10.2, 33.4, 44.4} is an example of?
- Initializing a Variable
- Declaring an Array
- Initializing a Function
- Initializing an Array
Answer. Option D
Leave a Reply