One-Dimensional Array

A form of data structure known as an array is used in computer science to store components of the same data type in contiguous memory regions. Multiple values of the same data type can be stored in a single variable using arrays. The values can be stored in a variety of forms thanks to multiple array types. The one-dimensional array structure is among the most used array types for storing values. In the article that follows, we will learn more about the idea of a 1D Array.

Definition of one dimensional array

  • The most basic type of array is a one dimensional array, in which each element is stored linearly and may be retrieved individually by providing its index value.
  • A collection of elements with the same data type that are kept in a linear arrangement under a single variable name is referred to as a one dimensional array.

One Dimensional Array

One of the most basic types of an Array is this. They are relatively simple to utilize in programs and define. A One Dimensional Array is a particularly practical Data structure type since the values it stores are easily initialized and changed.

Declaration Syntax

data_type array_name [array_size];


array_name = name of the 1D array
array_size = defines the number of elements in the array

Initialization Syntax

Simply adding a list to the right side of the 1D Array’s declaration syntax initializes it. Simply said, we assign values to the defined 1D Array according to the supplied array size.
data_type array_name [array_size] = {comma_separated_element_list};

Note: The defined Array Size shall not be exceeded by the number of entries supplied in the list.


int arr [10] ;      // Declaring a 1D array of size 10
int roll_no [5] =  {1, 2, 3, 4, 5} ;      // Initializing a 1D array of size 5
char names[30] = {"Raj, John, Krish"} ;   // Initializing a 1D array of type char

Input Elements in a 1D Array

There are just a few techniques available for assigning and storing values in an array. Let’s look at the two most popular approaches to better comprehend.

1.Direct Initialization

The elements are allocated in this procedure at the time the 1D Array is declared.


int num [10] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};

2.User Input Method

With this approach, the user is prompted to enter an array of items during program execution.

#include <iostream>
using namespace std;
int main()
  int num [5];
  cout<<"Enter array elements: \n";
  for(int i = 0; i < 5 ; i++)
    cin >> num[i] ;


Enter array elements:

Accessing 1D Array Elements

To find out what the elements of a 1D Array are, we may either display the full array or use the indexing technique to display only the values of the array that we need.
Note: Indexing an array always begins at 0.


# To print a single element of an array
int arr [5] = {1, 3, 5, 7, 9};
cout << arr[3];   // arr[3] i.e. index 3 of the array will print the value 7

Manipulation of 1D Array Elements

We will use the next way to alter a specific member included in an array:


#include <iostream>
using namespace std;
int main()
   int arr [7] = {10, 20, 30, 40, 55, 60, 70};
   cout << "5th value of Array Before updation: \n" << arr[4];
   arr [4] = 50;
   cout << "\n 5th value of Array After updation: \n" << arr[4];


5th value of Array Before Updation:
5th value of Array After Updation:

By using the subsequent technique, we can utilize 1D arrays to calculate the average and sum of the items in the array:

#include <iostream>
using namespace std;
int main()
  int sum = 0, avg = 0;
  int num [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ;
  for (int i = 0; i < 10; i++ )
      sum = sum + num [i] ;
  cout << "Sum of all elements is: " << sum ;
  avg = sum / 10 ;
  cout << "\n Average is: " << avg ;


Sum of all elements is: 55
Average is: 5

Declaration of Strings in 1D Arrays

One-dimensional arrays can incorporate alphabetic values in addition to numeric data. The only thing that strings are is a collection of characters; alternatively, we might say that strings are an array of characters. Using the char data type, strings may also be stored in arrays. Initializing a string array requires the following syntax:


char string_name [string_size] = {comma_seperated_character_list} ;


char str [20] = {"Hello World"};
cout << str;


Hello World

The length of a string may be determined, two strings can be compared, a string can be copied, or reversed, certain words or alphabets can be removed from the strings, the number of words or letters can be counted, etc. using strings in a 1D array.

Applications of 1D Arrays

  1. Other data structures like stacks, queues, heaps, graphs, etc. are implemented using one-dimensional arrays.
  2. We can execute operations on 1D arrays using them, including identifying the location of any element within the array, determining the largest and smallest element inside the array, adding and deleting elements, merging two arrays, and more.
  3. Additionally, sorting algorithms like Insertion Sort, Bubble Sort, Selection Sort, Merge Sort, etc. are implemented using 1D arrays.

Leave a Reply

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