Array Data Structure in C/C++

An Array is a linear Data Structure that contains a number of data values of the same type stored at contiguous memory locations. 


Before you start learning more about Array you must know about Data Structure. Data Structure is a way of organizing and storing data and each data structure is designed to organize data to suit a specific purpose. 


Below is the example of Array Data Structure which we can visualize as a large chunk of memory divided into a smaller block of memory of equal size and each block is capable of storing a data value of same type.

Array Data Structure

In the above example, you can observe that we can have different kinds of Array Data Structure like Integer Array or Character Array based on the type of value we are storing in it. All the elements of an array are arranged in a sequential manner you can access any element of an array using the index value like a[4] = 21. This is an example of a one-dimensional Array.


Now as you know what a one-dimensional array looks like, let's understand,


How to Declare and Define One-Dimensional Array?

The syntax of declaring a 1D array is like first of all you have to define the data type of the array then the name of the array and the number of elements you want to store in this array.

Syntax: data_type name of the array[no. of elements];

Example: int arr [5];

Here you are not only declaring the array you are also defining the array by providing the detail of the number of elements you want to store because based on this compiler will allocate a contiguous block of memory of size  = 5*sizeof(int) where the size of an integer depends on machines.

Note: The length of an array can be specified by any positive integer constant expression and specifying the length of an array using a macro is considered to be an excellent practice so that if there is any change in array size you don't need to update it everywhere. 

#define N 15

int arr[N];(alert-success)

How to Access the One-Dimensional Array elements?

To access any array element, you just need to specify the array name with the index value of that particular element in the square bracket. Usually, most of the time we use 0-based indexing so if the size of the array is declared as 5 then the last element is going to be present at index 4 (length -1) and the first element is going to be present at index 0.

Syntax: array_name[index];

Example: 
Accessing the last element of the array: arr[4];
Accessing the first element of the array: arr[0];


How to initialize One Dimensional Array?

There are multiple methods of initializing a 1D array here we are going to see four different methods. 


Method 1: In this, you have declared the data type and size of the array and you can initialize the array element inside curly brackets and the number of elements present inside the bracket should be equal to the size of the array.

int arr[5] = {3, 5, 6, 9, 12};


Method 2: In this, you don't need to declare the array size in advance you can directly initialize the array elements inside curly brackets, and based on the number of elements present you can calculate the size of the array.

int arr[] = {3, 5, 6, 9, 12};


Method 3: In this, you first declare the array with the number of elements you want to store in that array, and then using the index value you can initialize the element one by one.

int arr[5];

arr[0] = 3;
arr[1] = 5;
arr[2] = 6;
arr[3] = 9;
arr[4] = 12;


Method 4: In this, you first declare the array with the number of elements you want to store, and then you run a for-loop and take the element as input from the user and which will then initialize inside the array one by one.

int arr[5];

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


Note: If the number of elements you initialize is lesser than the length of the array then in that case the remaining locations of the array are filled by value 0. But make sure that must have to specify at least 1 element for this. It cannot be completely empty and it is also not allowed to add more elements than the length of an array.

int arr[10] = {5, 7, 9, 11, 5, 21};

All the remaining elements of array will automatically fill by 0
int arr[10] = {5, 7, 9, 11, 5, 21, 0, 0, 0, 0};


You can use the sizeof() operator to find the number of elements present in an Array. Below is the syntax for calculating this. 

int num = sizeof(name_of_array)/sizeof(name_of_array[0])


C++ Program to Print One-Dimensional Array

#include<iostream>
using namespace std;

int main(){
    //Initialising 1D Array
    int arr[] = {3, 5, 9, 11, 2, -1, 20};
    //No. of elements present in the array
    int n = sizeof(arr)/sizeof(arr[0]);

    //Printing all array elememts
    for(int i = 0; i < n; i++){
        cout<<arr[i]<<" ";
    }
}

Output:

3 5 9 11 2 -1 20


Multidimensional Arrays in C/C++.

A Multidimensional Array can be defined as an Array of Arrays that store data values of the same type in a tabular form. The size (number of elements it can store) of a multidimensional array can be calculated by multiplying the size of all the dimensions. 

The general form of declaring an N-dimensional Array is as  follows:

syntax: data_type name_of_array[size1][size2]...[sizeN];

For example:
int arr[4][5]     //Two Dimensional Array
size of arr[4][5] = 4x5 = 20 elements we can store in this array

int arr[4][5][6]  //Three Dimensional Array
size of arr[4][5][6] = 4x5x6 = 120 elements we can store in this array


Two Dimensional Array


Two-Dimensional Array.

A two-Dimensional Array can be visualized as a matrix (or table) which can be represented with n number of rows and m number of columns. The elements which are going to be present in a 2D Array must be of the same data type. Below is the syntax for the declaration of a 2D Array.
syntax: data_type name_of_array[no. of rows][no. of columns];
Example: int arr[4][3];

How To Initialize a Two-Dimensional Array?

There are multiple ways of initializing a 2D Array and we are going to see two different methods out of all:
Method 1: int arr[4][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Method 2: int arr[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};

We can access elements stored in a 2D Array by using the row index and column index of that particular element in the matrix.

C++ Program to Print Two-Dimensional Array.

#include<iostream>
using namespace std;

int main(){
    //Initialising 1D Array
    int arr[4][3] = {{3, 15, 9}, {2, 11, -1}, {7, 10, 4},{0, 1, 21}};    

    //Printing all array elements
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 3; j++){
            cout<< arr[i][j]<<" ";
        }
        cout<<"\n";
    }
}

Output:

3 15 9 
2 11 -1
7 10 4
0 1 21


I hope you have understood the basic and fundamental building blocks of Array Data Structure. If you have any questions regarding this topic please write them in the comment section below. 


👉Support Us by Sharing our post with others if you really found this useful and also share your valuable feedback in the comment section below so we can work on them and provide you best ðŸ˜Š.(alert-success) 

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS