Structured Data Type

Introduction to Arrays

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.

arrays

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++.

  1. 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

  1. 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

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

  1. 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.
  2. Multi-Dimensional Array – Multi-dimensional arrays contain values in any number of dimensions. The most common are 2D Arrays and 3D Arrays.
  3. 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

  1. Accessing Elements – Elements from an Array can be randomly and easily accessed using the indexing method.
  2. Code Optimization – Few lines of code are required to store multiple elements of the same data type in one variable.
  3. Traversal is easy – Traversal along an array is very easy.
  4. Sorting – Values can be sorted easily in an array.
  5. Memory Allocation – All the elements in an array are stored in continuous memory location. This saves memory as there is no extra memory allocation.
  6. 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

  1. Fixed no. of elements – The size of an array cannot be changed once it has been defined at the start.
  2. Insertion and Deletion of new elements is a tough task to perform.
  3. Allocation of more memory will create a memory wastage problem and Allocation of less memory will also create storage problems.
  4. 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?

  1. int arr {} ;
  2. int arr [5] ;
  3. array {5} ;
  4. 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] ;
}

  1. 5
  2. 0
  3. 15
  4. 20

Answer. Option C

Q3. What will be the output of the following program?

                    

int main()
    {
        char str[5] = "ABC";
        cout << str
        return 0;
    }

  1. ABC
  2. ABCD
  3. Compile-time error
  4. None of the Above

Answer. Option A

Q4. float a [3] = {10.2, 33.4, 44.4} is an example of?

  1. Initializing a Variable
  2. Declaring an Array
  3. Initializing a Function
  4. Initializing an Array

Answer. Option D

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.