Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
One dimensional Array in Data Structures with Example

One dimensional Array in Data Structures with Example

06 Jun 2024
Beginner
13.5K Views
16 min read
Learn with an interactive course and practical hands-on labs

Free Data Structures & Algorithms Online Course

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,...

Read More - DSA Interview Questions and Answers

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.
>>> Read More:- Differences Between Array and Linked List
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

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.

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

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.

Arrays are classified as homogeneous data structures because they store elements of the same data type in contiguous memory locations.
Share Article
About Author
Amit Kumar Ghosh (SDE and Mentor at Scholarhat)

As a software developer with a wealth of experience, he brings a unique combination of technical acumen and a passion for mentorship to my role. With 6 years of experience, he has honed the skills in C/C++, Java, Python, SQL, C#, JavaScript, React, Java Spring etc. and has a proven track record of delivering high-quality, scalable software solutions and core Computer fundamental knowledge DSA, OOPs, CN, OS etc.

As a teacher, his approach revolves around interactive techniques, prioritizing hands-on learning and real-world projects. He explains concepts clearly, offer practical examples, and encourage questions to foster a supportive environment.
Accept cookies & close this