One Dimensional Array

In computer science, Array is a type of Data structure that is used to store elements of the same data type in continuous memory locations. Arrays let us store multiple values of the same data type in a single variable. Different types of arrays make it possible to store the values in various structures. One of the most common structures of an Array in which the values are stored is the One Dimensional Array Structure. We will learn more about the concept of 1D Array in the article below.

Definition

• A One-Dimensional Array is the simplest form of an Array in which the elements are stored linearly and can be accessed individually by specifying the index value of each element stored in the array.
• A One-Dimensional Array is a group of elements having the same data type which are stored in a linear arrangement under a single variable name.

One Dimensional Array

This is one of the simplest forms of Array. They are very easy to define and use in the programs. The values stored in a One Dimensional Array can be easily initialized and manipulated, making it a very feasible Data structure type.

Declaration Syntax

                    
data_type array_name [array_size] ;



where,

array_name = name of the 1D array

array_size = defines the number of elements in the array

Initialization Syntax

To Initialize the 1D Array, we simply add a list to the right side of the declaration syntax of the 1D Array. In simple terms, we assign values to the declared 1D Array as per the array size specified.

                    
data_type array_name [array_size] = {comma_separated_element_list};



Note:Â The number of elements specified in the list should not exceed the defined Array Size.

Example

                    
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 few methods by which we can assign and store values in an Array. Let us understand by looking at the 2 most common methods.

1. Direct Initialization

In this method, the elements are assigned during the declaration of the 1D Array.

Example

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



1. User Input Method

In this method, The user is asked to enter the array elements of his/her choice during the run-time of the program.

Example â€“

                    
#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] ;
}
}



Output â€“

                    
Enter array elements :
1
3
5
7
9



Accessing 1D Array Elements

We can either display the entire 1D Array to know what the elements are or we can use the method of indexing in order to display only those Array values which we require.

Note:Â Array Indexing always starts from 0.

Example 1

                    
# 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 array will print the value 7



Example 2

                    
# To print all the elements of an Array

int arr [5] = {1, 3, 5, 7, 9} ;
cout<<"The array elements are: \n";
for (int i = 0; i < 5; i++)
{
cout << arr[i] << " ";
}



Output

                    
The array elements are:
1 3 5 7 9



Manipulation of 1D Array Elements

If we want to change a specific element stored in an array, we will use the following method:

Example

                    
#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] ;
}



Output

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



We can use 1D Arrays to find out the sum of all elements and the average of the array by using the following method:

Example

                    
#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 ;
}



Output

                    
SumÂ ofÂ allÂ elementsÂ is:Â 55
AverageÂ is:Â 5



Declaration of Strings in 1D Arrays

One Dimensional Array not only includes numeric data but can also contain alphabetic values. Strings are nothing but a collection of characters, or we might also say Strings are an array of characters. Strings can also be stored in Arrays by using the char data type. The syntax for initializing a string array is as follows:

Syntax

                    
char string_name [string_size] = {comma_seperated_character_list} ;



Example

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

cout << str ;



Output

                    
Hello World



Using Strings in 1D Array, we can perform various operations such as finding the length of a string, comparing 2 strings, copy a string, reverse the string, delete specific words/alphabets from the strings, count the number of words/letters, etc.

Applications of 1D Arrays

1. One Dimensional Arrays are used to implement other data structures such as stacks, queues, heaps, graphs, etc.
2. Using 1D Arrays, we can perform operations such as finding the position of any element in the array, find out the largest and the smallest element in the array, insert and delete an element, merge two arrays, etc.
3. 1D arrays are also used to implement Sorting Algorithms such as Insertion Sort, Bubble Sort, Selection Sort, Merge Sort, etc.

FAQs onÂ One Dimensional Array

Q1. One Dimensional Array is always considered as?

1. Linear
2. Complex
3. Sequential
4. Both A & C

Q2. How can we access the 5th element stored in an array?

1. arr(5);
2. arr[5];
3. arr[4];
4. arr{4};

Answer. Option C. Indexing starts from 0, Hence, the 5th element will be stored at the 4th location in the array.

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

                    
#include <iostream>
using namespace std;
int main()
{
int num [4] = {1,4,6,3} ;
int result = 0 ;
for (int i = 0; i < 5; i++)
{Â Â Â Â Â Â Â Â Â Â
result = result + num [i] ;
}
cout << result ;
}


1. 12
2. 13
3. 14
4. 15

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.
Ashhar Firdausi
IIT Roorkee
Biology
Dr. Nazma Shaik
VTU
Chemistry
Gaurav Tiwari
APJAKTU
Physics
Get Started