Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# One-Dimensional Array

Last Updated on November 1, 2023 by Ankit Kochar

In computer science, arrays serve as a data structure for the contiguous storage of elements sharing the same data type. Arrays allow for the consolidation of multiple values of a uniform data type within a single variable. Various types of arrays facilitate the storage of values in different configurations, with one-dimensional arrays being one of the most frequently employed structures for this purpose. In the article that follows, we will learn more about the idea of One Dimensional Array in C.

## Definition of One Dimensional Array

One Dimensional Array can be defined as follows.

• 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

The one-dimensional array is considered one of the simplest forms of arrays, known for its ease of use and definition in programming. This type of array is particularly practical as a data structure because it allows for straightforward initialization and modification of the stored values.

### Declaration Syntax of One Dimensional Array

``data_type array_name [array_size];``

where,

``````array_name = name of the one dimensional array
array_size = defines the number of elements in the array``````

### Initialization Syntax of One Dimensional Array

Simply adding a list to the right side of the One Dimensional 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.

Example of Declaration of One Dimensional Array

``````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 One Dimensional Array

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

### 1. Direct Initialization of One Dimensional Array

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

Example

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

### 2.User Input Method of One Dimensional Array

With this approach, the user is prompted to enter an array of items during program execution.
Example of Input Elements in One Dimensional Array:

```#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 Elements of One Dimensional Array

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.

Example of Accessing Elements of One Dimensional Array

``````// 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 Elements of One Dimensional Array

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

Example 1 of Manipulating Elements of One Dimensional 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];
}
```

Output

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

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

Example 2 of Manipulating Elements in One Dimensional 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 ;
}
```

Output

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

## Declaration of Strings in One Dimensional Array

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:

Syntax of Declaration of String Using One Dimensional Array:

``char string_name [string_size] = {comma_seperated_character_list} ;``

Example of Declaration of String Using One Dimensional Array:

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

Output

``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 One Dimensional Array

The Applications of One Dimensional Array are listed below.

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.

Conclusion
One-dimensional arrays are a fundamental and versatile data structure in programming, allowing for the storage and manipulation of elements of the same data type in a contiguous memory space. In this conclusion, we’ll summarize key points and address frequently asked questions related to one-dimensional arrays.

## Frequently Asked Questions (FAQs) related to One-Dimensional Array

Here are some Frequently Asked Questions related to “One Dimensional Array”.

1. What is the main characteristic of a one-dimensional array?
A one-dimensional array is characterized by its linear structure, where elements are stored in a single row or column. Elements in a one-dimensional array are accessed using a single index.

2. How are one-dimensional arrays different from other types of arrays, like multi-dimensional arrays?
One-dimensional arrays store elements in a linear sequence, while multi-dimensional arrays store elements in a grid-like structure with rows and columns. Multi-dimensional arrays are used when data requires more complex organization.

3. What are common use cases for one-dimensional arrays?
One-dimensional arrays are frequently used for storing lists, implementing data structures like stacks and queues, performing mathematical operations on sets of numbers, and representing sequences, among other applications.

4. Are one-dimensional arrays limited to a single data type?
No, one-dimensional arrays can store elements of various data types, but all elements within a single array must be of the same data type.

5. What is the time complexity of accessing an element in a one dimensional array?
Ans. The time complexity of accessing an element in a one dimensional array is O(1).

For better understanding of Arrays you can check out these Articles.