One-Dimensional Array in C++.

One-Dimensional Array in C++.

Array Data Structure is a Linear Data Structure that can store elements of similar data types in a contiguous memory location. When you want to use an Array data structure to store your data values, you need to declare the size of the array in advance. 

There are two types of Array data structures that is One-Dimensional and Multi-Dimensional Data Structures. In this article, I will discuss One-Dimensional data structure in detail.

What is a One-Dimensional Array?

In a One-Dimensional Array, elements are stored under a single variable name in a linear manner one after another in a continuous memory location. It allows random access to any element by using the index value of that particular element.

A one-Dimensional Array is the simplest form of Array Data Structure in which data manipulation and access are much easier compared to other data structures. It is also used for creating other data structures like stacks, queues, trees, and graphs. 

How To Declare a One-Dimensional Array?

To define a 1D array in C++ programming, you first have to write the data type of the array followed by the array name and inside subscript (square bracket) you have to define the size of the array. 

Key points to note about array declaration
  • If you have created integer-type data then you can store only integer-type values.
  • The size of an array must be known in advance and you cannot change this at run time. 
  • You can only use positive integer values to define the size of the array and floating or negative values are not allowed.

How To Initialize One-Dimensional Array?

There are multiple ways to initialize a 1D array in C++ and here I will discuss a few of them one by one.

1. Declared the data type and size of the array and you can initialize the array element inside curly brackets. The number of Elements in the brackets must be less or equal to the size of the array.

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


2. No need to declare the array size in advance you can directly initialize the array elements inside curly brackets. The size of the array is going to depend upon the number of elements present in the brackets.

int arr[] = {23, 5, 6, 12, 1};


3. Declare the array with the number of elements you want to store in that array and then use the index value to initialize the element one by one.

int arr[5];

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


4. First declare the array with the number of elements you want to store, and then you run a for-loop and initialize the array by taking input value from the user.

int arr[6];

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


(getButton) #text=(How To Pass Array to Function in C++) #icon=(link) #color=(#2339bd)

How To Traverse One-Dimensional Array?

Traversing is a process of visiting each element of the array at least once. To perform this operation we simply use a for loop that is going to run n times where n is the number of elements present in the array.


C++ Program to Traverse One-Dimensional Array

#include<iostream>
using namespace std;

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

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

Output:

31 5 9 11 2 20

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])


Next:

⚡ 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