One dimensional Array in Data Structures with Example

06 Jun 2024
Beginner
4.28K Views
Learn via Video Course & by Doing Hands-on Labs

One dimensional Array: An Overview

Data Structures and Algorithms (DSA) form the backbone of computer science, enabling efficient problem-solving and algorithmic design. In this DSA tutorial, we'll explore the concept of one-dimensional arrays, their characteristics, and their applications in DSA. You might have already come across arrays while learning arrays in C and arrays in C++.

To further enhance your understanding and application of array concepts, consider enrolling in the Best Data Structures and Algorithms Course, where you can gain comprehensive insights into effective data structure utilization for improved problem-solving and time management.

What is One dimensional Array in Data Structure?

A one-dimensional array is a linear data structure that stores elements of the same data type in contiguous memory locations. It provides a systematic way of organizing and accessing a collection of elements, where each element is identified by its index or position within the array. The index starts from 0 and goes up to the size of the array minus one.

One-dimensional arrays are useful and robust data formats that can be used for a variety of data storage and manipulation purposes. They are used in a variety of applications and are a necessary component of many C programs.

Syntax

The syntax for declaring and initializing a one-dimensional array in C is as follows:

``data_type array_name[array_size] = value1, value2, value3,...;``
• The arguments used in the preceding syntax are listed below.
• The data_type variable represents the data type of the array's items.
• array_name is the name of the array.
• array_size specifies how many elements the array can hold.
• The values of each array element are value1, value2, value3,...

Declaration and Initialization

In most programming languages, one-dimensional arrays can be declared and initialized as follows:

``````int myArray[5];
// Initialization
myArray[0] = 10;
myArray[1] = 20;
myArray[2] = 30;
myArray[3] = 40;
myArray[4] = 50;
``````

Common Operations on One-Dimensional Arrays

1. Accessing Elements

Elements in a one-dimensional array are accessed using their respective indices. For example, myArray[2] would return the value 30.

Syntax

``````element = arrayName[index];
``````

Example

For example, if we have an array named myArray and we want to access the element at index 2:

``````int value = myArray[2];
``````

This line of code retrieves the value stored at index 2 in the array myArray. In this case, if myArray is {10, 20, 30, 40, 50}, the variable value would be assigned the value 30.

2. Insertion and Deletion

Inserting or deleting an element in a one-dimensional array may require shifting the subsequent elements accordingly to maintain the sequential order. The syntax for inserting and deleting elements is as follows:

Syntax

``arrayName[index] = newElement;``

Example

For example, if we have an array named myArray and we want to access the element at index 2:

`` myArray[2] = 25;``

Deletion

Deletion involves shifting elements after the deleted element to fill the gap. In this example, let's delete the element at index 3:

``````// Deleting the element at position index
// Shift elements after the deleted index to fill the gap
for (int i = index; i < arraySize - 1; i++) {
arrayName[i] = arrayName[i + 1];
}
arraySize--; // Decrease the size of the array``````

3. Updating Elements

Elements in an array can be updated by assigning a new value to the desired index.

Syntax

``arrayName[index] = newValue;``

Example

For example, if we have an array named myArray and we want to access the element at index 2:

``myArray[2] = 35;``

4. Traversing

Traversing involves visiting each element of the array sequentially, allowing for various operations on each element. The syntax for traversing through an array using a loop is as follows:

Syntax

``````for (int i = 0; i < arraySize; i++) {
// Perform operations on array elements, e.g., print, modify, etc.
// Access array elements using arrayName[i]
}``````

Example

For example, to print all elements of the array myArray:

``````printf("Elements of the array:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", myArray[i]);
}
printf("\n");``````

Example of One-Dimensional Array

Let's take a simple example in C to demonstrate the use of a one-dimensional array in DSA. In this example, we'll create an array of integers, initialize it, and perform basic operations like accessing elements and traversing the array.

``````# Declaration and Initialization of a One-Dimensional Array
myArray = [10, 20, 30, 40, 50]

# Accessing and Printing Elements
print("Elements of the array:")
for i in range(5):
print(myArray[i], end=" ")
print()

# Updating an Element
myArray[2] = 35
print("Updated array after changing the third element:")
for i in range(5):
print(myArray[i], end=" ")
print()

# Summing the Elements
sum_value = sum(myArray)
print("Sum of the elements:", sum_value)
``````
``````
public class OneDimensionalArrayExample {
public static void main(String[] args) {
// Declaration and Initialization of a One-Dimensional Array
int[] myArray = {10, 20, 30, 40, 50};

// Accessing and Printing Elements
System.out.println("Elements of the array:");
for (int i = 0; i < 5; i++) {
System.out.print(myArray[i] + " ");
}
System.out.println();

// Updating an Element
myArray[2] = 35;
System.out.println("Updated array after changing the third element:");
for (int i = 0; i < 5; i++) {
System.out.print(myArray[i] + " ");
}
System.out.println();

// Summing the Elements
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += myArray[i];
}
System.out.println("Sum of the elements: " + sum);
}
}
``````
``````
#include

int main() {
// Declaration and Initialization of a One-Dimensional Array
int myArray[5] = {10, 20, 30, 40, 50};

// Accessing and Printing Elements
std::cout << "Elements of the array:" << std::endl;
for (int i = 0; i < 5; i++) {
std::cout << myArray[i] << " ";
}
std::cout << std::endl;

// Updating an Element
myArray[2] = 35;
std::cout << "Updated array after changing the third element:" << std::endl;
for (int i = 0; i < 5; i++) {
std::cout << myArray[i] << " ";
}
std::cout << std::endl;

// Summing the Elements
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += myArray[i];
}
std::cout << "Sum of the elements: " << sum << std::endl;

return 0;
}
``````

In this example, we declare and initialize an array myArray of size 5 with integer elements. We use a loop to print the elements of the array. Update the third element of the array and print the array after the update. Then calculate and print the sum of all elements in the array.

Output

``````Elements of the array:
10 20 30 40 50
Updated array after changing the third element:
10 20 35 40 50
Sum of the elements: 155``````

Applications in DSA

1. Linear Search

One-dimensional arrays are commonly used in linear search algorithms to find the position of a specific element in the array.

2. Sorting Algorithms

Sorting algorithms like bubble sort, insertion sort, and selection sort often rely on manipulating one-dimensional arrays to rearrange elements in ascending or descending order.

3. Dynamic Memory Allocation

One-dimensional arrays are crucial in dynamically allocating memory for structures like linked lists, stacks, and queues.

4. Mathematical and Scientific Applications

Arrays are extensively used to store and process data in mathematical and scientific computations, making them invaluable in fields like engineering and physics.

5. String Manipulation

Strings can be represented as one-dimensional character arrays, enabling various string manipulation operations.
Summary
This article delves into the intricacies of one-dimensional arrays in Data Structures and Algorithms (DSA). It provides a concise exploration of their definition, syntax, declaration, and initialization. We hope this post has been instrumental in enhancing your understanding of the one-dimensional array concept in DSA.
For those seeking a more structured and comprehensive learning experience, consider exploring a Best DSA Course Online. Such courses offer a systematic approach to mastering data structures and algorithms, providing learners with a solid foundation for tackling programming challenges. By incorporating these online courses into your learning journey, you can further solidify your grasp on topics like one-dimensional arrays in DSA and pave the way for more advanced algorithmic problem-solving.

FAQs

Q1. What is array data structure?

An array is a linear data structure that stores elements of the same data type in contiguous memory locations. It provides a systematic way of organizing and accessing a collection of elements, where each element is identified by its index or position within the array.

Q2. How do I access elements in a one-dimensional array?

Elements in a one-dimensional array are accessed using their respective indices.

Q3. What is the difference between a 1D array and a 2D array?

A 1D array and a 2D array are both fundamental data structures in programming languages, but they differ in terms of their dimensions and how data is organized within them.

Q4. Why are arrays classified as homogeneous data structures?

Arrays are classified as homogeneous data structures because they store elements of the same data type in contiguous memory locations.
Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
 ASP.NET Core Certification Training Jul 15 MON, WED, FRI Filling Fast 07:00AM to 08:30AM (IST) Advanced Full-Stack .NET Developer Certification Training Jul 15 MON, WED, FRI Filling Fast 07:00AM to 08:30AM (IST) Azure Master Class Jul 20 SAT, SUN Filling Fast 03:00PM to 05:00PM (IST) ASP.NET Core Certification Training Jul 28 SAT, SUN Filling Fast 07:00AM to 09:00AM (IST) Software Architecture and Design Training Jul 28 SAT, SUN Filling Fast 05:30PM to 07:30PM (IST) .NET Solution Architect Certification Training Jul 28 SAT, SUN Filling Fast 05:30PM to 07:30PM (IST) Azure Developer Certification Training Jul 28 SAT, SUN Filling Fast 10:00AM to 12:00PM (IST) Advanced Full-Stack .NET Developer Certification Training Jul 28 SAT, SUN Filling Fast 07:00AM to 09:00AM (IST) Data Structures and Algorithms Training with C# Jul 28 SAT, SUN Filling Fast 08:30PM to 10:30PM (IST) Angular Certification Course Aug 11 SAT, SUN Filling Fast 09:30AM to 11:30AM (IST) ASP.NET Core Certification Training Aug 24 SAT, SUN Filling Fast 07:00AM to 09:00AM (IST)

Can't find convenient schedule? Let us know

Similar Articles