Table of contents
1.
Introduction
2.
What is an Array?
3.
Why do we need an array?
4.
Indexing in the Array
5.
Syntax of an array declaration
6.
Initialization Methods in an Array:
6.1.
Static Initialization
6.2.
Dynamic Initialization
6.3.
Zero Initialization
6.4.
Initialization with Default Values
7.
Types of array declaration in C++
7.1.
Declaration of an array by specifying the size
7.1.1.
Implementation
7.2.
Declaration of an array by initializing elements
7.2.1.
Implementation
7.3.
Declaration of an array by specifying the size and initializing elements
7.3.1.
Implementation
8.
Advantages of an array
9.
Disadvantages of an array
10.
Frequently asked questions
10.1.
How to Write an Array in C++?
10.2.
How Many Data Types Are in an Array?
10.3.
What is the Length of an Array?
11.
Conclusion
Last Updated: Aug 11, 2024
Easy

Introduction to Array

Author Shivam Verma
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

An array is one of the most fundamental data structures in programming, providing a way to store multiple values of the same type in a single, contiguous block of memory. Arrays are widely used across various programming languages due to their simplicity and efficiency in managing and accessing collections of data. 

Introduction to Array

Whether you’re dealing with a list of numbers, a series of characters, or any other set of elements, arrays offer a straightforward method to organize and manipulate this data. In this blog, we will explore the concept of arrays, their syntax, and how they can be utilized to solve common programming problems. Understanding arrays is a key step in mastering more complex data structures and algorithms, making them an essential topic for any aspiring programmer.

example of array

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

What is an Array?

An array is a data structure that allows you to store multiple elements of the same data type in a single, contiguous block of memory. Each element in an array is identified by an index, which indicates its position within the array. The index typically starts at 0, meaning the first element is at index 0, the second at index 1, and so on.

Arrays provide a convenient way to manage and manipulate large sets of related data. Instead of creating individual variables for each value, you can use an array to group them together, making your code more organized and easier to manage. For example, if you need to store the grades of 100 students, instead of creating 100 separate variables, you can create an array with 100 elements.

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.

Indexing in the Array

Indexing in an array refers to the method of accessing elements based on their position within the array. Each element in an array is assigned a unique index, which allows you to retrieve or modify the element efficiently. The index is a numerical value that represents the position of an element within the array, with indexing typically starting from 0.

There are several key points about Indexing:

1. Zero-Based Indexing:

  • In most programming languages, arrays use zero-based indexing. This means that the first element of the array is accessed with index 0, the second element with index 1, and so on. For example, in an array named arr, arr[0] refers to the first element, arr[1] refers to the second element, and so forth.

2. Positive and Negative Indexing:

  • In some languages, you can use negative indexing to access elements from the end of the array. For instance, arr[-1] typically refers to the last element, arr[-2] refers to the second-to-last element, and so on. This feature is common in languages like Python.

3. Out-of-Bounds Access:

  • Accessing an element using an index that is outside the valid range (i.e., less than 0 or greater than or equal to the array size) results in an error or undefined behavior. This is known as an out-of-bounds access and can lead to runtime errors or program crashes.

4. Indexing Examples:

  • Consider an array arr of size 5: int arr[5] = {10, 20, 30, 40, 50};
    • arr[0] will access the value 10
    • arr[1] will access the value 20
    • arr[4] will access the value 50
  • If you attempt to access arr[5] or arr[-1] in a zero-based indexing language, it would typically result in an error.

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.

Initialization Methods in an Array:

Initializing an array involves assigning values to its elements when it is created. Different programming languages offer various methods for initializing arrays, allowing you to set up your data structure in a way that suits your needs. Here are some common initialization methods:

Static Initialization

1. Direct Initialization: You can initialize an array with a set of values at the time of declaration. This method is straightforward and is used when you know the values you want to assign in advance.

int arr[5] = {10, 20, 30, 40, 50};

In this example, arr is an integer array with 5 elements, initialized with the values 10, 20, 30, 40, and 50.

2. Partial Initialization: If you provide fewer values than the size of the array, the remaining elements are automatically initialized to zero (for numeric types) or null (for pointers). 

int arr[5] = {10, 20};

Dynamic Initialization

1. Using a Loop: You can initialize an array dynamically by using loops, which is useful when the values are determined during runtime or are generated algorithmically.

int arr[5]; 
for (int i = 0; i < 5; i++) 
{ 
arr[i] = i * 10; 
}

Here, each element of the array arr is initialized to a value based on its index.

2. Function-Based Initialization: Some programming languages provide functions or methods to initialize arrays. For example, in Python, you can use list comprehensions or functions to create and initialize arrays. 

arr = [i * 10 for i in range(5)]

Zero Initialization

In many languages, you can explicitly initialize an array with zero values. This is particularly useful for numeric arrays where you want to start with a known base state. 

int arr[5] = {0};

Initialization with Default Values

Some languages allow you to initialize arrays with default values. For example, in C++, you can use the std::fill function from the Standard Library to set all elements to a specific value.

#include <algorithm>
int arr[5];
std::fill(std::begin(arr), std::end(arr), 42); // All elements are set to 42

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

Implementation

// 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

Implementation

// 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

Implementation

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

Advantages of an array

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

Disadvantages of an 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.  

Frequently asked questions

How to Write an Array in C++?

Declare an array with its type and size, then optionally initialize it. Example:

int arr[5] = {10, 20, 30, 40, 50};

How Many Data Types Are in an Array?

An array in C++ can only contain elements of a single data type. Mixing types in one array is not allowed.

What is the Length of an Array?

The length of a statically allocated array can be determined using:

int length = sizeof(arr) / sizeof(arr[0]);

Conclusion

In this blog, we have discussed about arrays. Arrays are a fundamental data structure that simplify the management and organization of multiple elements of the same type. Understanding arrays is crucial for efficient data handling in programming, enabling straightforward access, manipulation, and storage of data. 

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!

Live masterclass