Table of Contents

    One-dimensional Array

    Programming Mastery

    One-dimensional Array

    Learn how a one-dimensional array stores multiple values in a single row and allows each value to be accessed using an index.

    What is a One-dimensional Array?

    A one-dimensional array, also called a 1D array, is an array that stores elements in a single line or single row.

    In simple words, a one-dimensional array is like a list of values arranged one after another. Each value has a position number called an index.

    A one-dimensional array is a linear collection of elements where each element can be accessed using one index.

    For example, if we want to store marks of five students, we can use one array:

    marks = [85, 90, 78, 88, 92]

    Here, marks is a one-dimensional array because all values are stored in a single sequence.

    Easy Real-Life Example

    One-dimensional Array as Train Coaches

    Imagine a train with coaches arranged in one straight line. Each coach has a number, and each coach contains passengers or items.

    A one-dimensional array works in the same way. It stores values one after another, and each value can be found using its position.

    Index:   0    1    2    3    4
    Value:  85   90   78   88   92

    In most programming languages, indexing starts from 0. That means the first element is at index 0, not index 1.

    Why is it Called One-dimensional?

    It is called one-dimensional because data is stored in only one direction: as a single row or list.

    A one-dimensional array does not have rows and columns like a table. It only has one line of elements.

    One-dimensional Array:
    
    [10, 20, 30, 40, 50]

    It needs only one index to access an element.

    numbers[2]

    The above expression accesses the value stored at index 2.

    Main Parts of a One-dimensional Array

    Part Meaning Example
    Array Name The name used to identify the array. marks
    Element Each value stored inside the array. 85, 90, 78
    Index The position number of each element. 0, 1, 2
    Length Total number of elements in the array. 5
    Data Type The type of data stored in the array. Integer, Text, Decimal

    General Syntax of One-dimensional Array

    The exact syntax changes from language to language, but the general language-neutral format is:

    arrayName = [value1, value2, value3, value4]

    Example:

    scores = [45, 78, 92, 66, 81]

    Here, scores is a one-dimensional array containing five numbers.

    Indexing in One-dimensional Array

    Each element in a one-dimensional array has an index. The index is used to access, update, or process the value.

    scores = [45, 78, 92, 66, 81]
    
    Index 0 → 45
    Index 1 → 78
    Index 2 → 92
    Index 3 → 66
    Index 4 → 81
    Important: If an array has n elements, the first index is usually 0 and the last index is usually n - 1.

    For example, if an array has 5 elements, the last index is 5 - 1 = 4.

    Accessing Elements from a One-dimensional Array

    To access an element, use the array name and index.

    scores = [45, 78, 92, 66, 81]
    
    DISPLAY scores[0]
    DISPLAY scores[2]
    DISPLAY scores[4]

    Expected Output

    45
    92
    81

    scores[0] gives the first element, scores[2] gives the third element, and scores[4] gives the fifth element.

    Updating Elements in a One-dimensional Array

    We can update an existing element by assigning a new value to a specific index.

    scores = [45, 78, 92, 66, 81]
    
    SET scores[1] = 80
    
    DISPLAY scores

    Updated Array

    [45, 80, 92, 66, 81]

    The value at index 1 changed from 78 to 80.

    Traversing a One-dimensional Array

    Traversal means visiting each element of the array one by one.

    Loops are commonly used to traverse one-dimensional arrays.

    scores = [45, 78, 92, 66, 81]
    
    FOR index FROM 0 TO length(scores) - 1
        DISPLAY scores[index]
    END FOR

    Expected Output

    45
    78
    92
    66
    81

    The loop starts at index 0 and continues until the last valid index.

    Example 1: Store and Display Student Marks

    /*
    This program stores and displays student marks using a one-dimensional array.
    */
    
    ENTRY POINT
        DECLARE marks AS ARRAY = [85, 90, 78, 88, 92]
    
        FOR index FROM 0 TO length(marks) - 1
            DISPLAY "Student " + (index + 1) + " Mark: " + marks[index]
        END FOR
    END ENTRY POINT

    Expected Output

    Student 1 Mark: 85
    Student 2 Mark: 90
    Student 3 Mark: 78
    Student 4 Mark: 88
    Student 5 Mark: 92

    Example 2: Calculate Total of Array Elements

    /*
    This program calculates the total of all elements in a one-dimensional array.
    */
    
    ENTRY POINT
        DECLARE numbers AS ARRAY = [10, 20, 30, 40, 50]
        DECLARE total AS INTEGER = 0
    
        FOR index FROM 0 TO length(numbers) - 1
            SET total = total + numbers[index]
        END FOR
    
        DISPLAY "Total: " + total
    END ENTRY POINT

    Expected Output

    Total: 150

    Example 3: Find Average of Array Elements

    /*
    This program calculates average marks using a one-dimensional array.
    */
    
    ENTRY POINT
        DECLARE marks AS ARRAY = [85, 90, 78, 88, 92]
        DECLARE total AS INTEGER = 0
        DECLARE average AS DECIMAL = 0.0
    
        FOR index FROM 0 TO length(marks) - 1
            SET total = total + marks[index]
        END FOR
    
        SET average = total / length(marks)
    
        DISPLAY "Average Marks: " + average
    END ENTRY POINT

    Expected Output

    Average Marks: 86.6

    Example 4: Find Highest Value

    /*
    This program finds the highest value in a one-dimensional array.
    */
    
    ENTRY POINT
        DECLARE scores AS ARRAY = [45, 78, 92, 66, 81]
        DECLARE highest AS INTEGER = scores[0]
    
        FOR index FROM 1 TO length(scores) - 1
            IF scores[index] > highest THEN
                SET highest = scores[index]
            END IF
        END FOR
    
        DISPLAY "Highest Score: " + highest
    END ENTRY POINT

    Expected Output

    Highest Score: 92

    Example 5: Search an Element

    /*
    This program searches for a value in a one-dimensional array.
    */
    
    ENTRY POINT
        DECLARE numbers AS ARRAY = [10, 20, 30, 40, 50]
        DECLARE target AS INTEGER = 30
        DECLARE isFound AS BOOLEAN = false
    
        FOR index FROM 0 TO length(numbers) - 1
            IF numbers[index] == target THEN
                SET isFound = true
                BREAK
            END IF
        END FOR
    
        IF isFound == true THEN
            DISPLAY target + " found in the array"
        ELSE
            DISPLAY target + " not found in the array"
        END IF
    END ENTRY POINT

    Expected Output

    30 found in the array

    Common Operations on One-dimensional Arrays

    Operation Meaning Example Idea
    Access Read a value using index. marks[0]
    Update Change a value at an index. marks[2] = 80
    Traversal Visit every element one by one. Use a loop.
    Search Find whether a value exists. Search for a name or score.
    Sum Add all numeric values. Total marks.
    Average Find mean value. Average score.
    Minimum / Maximum Find smallest or largest value. Lowest and highest marks.

    One-dimensional Array vs Two-dimensional Array

    Feature One-dimensional Array Two-dimensional Array
    Structure Single row or list. Rows and columns.
    Index Required One index. Two indexes.
    Example marks[index] matrix[row][column]
    Best For List of values. Table or grid data.

    Advantages of One-dimensional Arrays

    Benefits

    • They store multiple related values under one name.
    • They are simple and beginner-friendly.
    • They allow direct access using indexes.
    • They work well with loops.
    • They reduce the need for many separate variables.
    • They are useful for storing lists such as marks, names, scores, and prices.
    • They help build logic for searching, sorting, and calculations.
    • They prepare students for advanced data structures.

    Limitations of One-dimensional Arrays

    Limitations

    • Traditional arrays may have fixed size in many programming languages.
    • Accessing an invalid index may cause errors.
    • Insertion and deletion may require shifting elements.
    • They are not ideal for table-like data that needs rows and columns.
    • They may not be suitable when data size changes frequently.

    Common Beginner Mistakes

    Mistakes

    • Thinking the first index is 1 instead of 0.
    • Trying to access an index outside the array range.
    • Confusing array length with last index.
    • Forgetting that last index is usually length - 1.
    • Using many variables instead of one array.
    • Not using loops for array traversal.
    • Changing the wrong index value.
    • Not checking whether the array is empty before accessing elements.

    Better Habits

    • Remember that indexing usually starts from 0.
    • Use length - 1 for the last index.
    • Use meaningful array names such as marks, prices, and scores.
    • Use loops to process arrays.
    • Validate index before accessing an element.
    • Test arrays with zero, one, and multiple elements.
    • Use trace tables for difficult array logic.
    • Keep array operations simple and readable.

    Best Practices for One-dimensional Arrays

    Recommended Practices

    • Use one-dimensional arrays for simple lists of related data.
    • Use plural names for arrays, such as names, marks, and scores.
    • Use loops for traversal instead of repeated statements.
    • Start traversal from index 0.
    • Stop traversal at length - 1.
    • Check array length before accessing elements.
    • Keep array logic clean and well-indented.
    • Use meaningful variable names such as index, total, and highest.
    • Use comments only when the logic is not obvious.
    • Test with different array sizes.

    Prerequisites Before Learning One-dimensional Array

    Students should already understand:

    Required Knowledge

    • Variables and constants.
    • Data types.
    • Input and output.
    • Operators.
    • Conditions.
    • Loops and iteration.
    • Functions and methods basics.
    • What is an array?
    • Why arrays are used.

    Practice Activity: Identify Index and Value

    Study the following one-dimensional array:

    numbers = [10, 20, 30, 40, 50]

    Questions

    1. What is the value at index 0?
    2. What is the value at index 2?
    3. What is the length of the array?
    4. What is the last index?
    5. What is the value at the last index?

    Sample Answers

    1. Value at index 0 = 10
    2. Value at index 2 = 30
    3. Length = 5
    4. Last index = 4
    5. Value at last index = 50

    Mini Quiz

    1

    What is a one-dimensional array?

    A one-dimensional array is a linear collection of elements stored in a single row or list.

    2

    How many indexes are needed to access an element in a one-dimensional array?

    Only one index is needed.

    3

    What is usually the first index of a one-dimensional array?

    In most programming languages, the first index is 0.

    4

    What is array traversal?

    Array traversal means visiting each element of an array one by one.

    5

    Give one example use of a one-dimensional array.

    A one-dimensional array can be used to store student marks, product prices, names, scores, or temperatures.

    Interview Questions on One-dimensional Array

    1

    Define one-dimensional array.

    A one-dimensional array is a data structure that stores elements in a single linear sequence and allows access using one index.

    2

    Why is a one-dimensional array useful?

    It is useful because it stores multiple related values under one name and allows easy processing using loops.

    3

    What is the difference between length and last index?

    Length is the total number of elements, while the last index is usually length - 1.

    4

    How can you find the total of elements in a one-dimensional array?

    Initialize a total variable to 0, traverse the array using a loop, and add each element to the total.

    5

    How is a one-dimensional array different from a two-dimensional array?

    A one-dimensional array stores data in a single row, while a two-dimensional array stores data in rows and columns.

    Quick Summary

    Concept Meaning
    One-dimensional Array An array that stores values in a single row or list.
    Element A value stored inside the array.
    Index The position used to access an element.
    First Index Usually 0.
    Last Index Usually length - 1.
    Traversal Visiting each element one by one.
    Best Use Storing simple lists such as marks, names, scores, prices, and temperatures.

    Final Takeaway

    A one-dimensional array is the simplest form of array. It stores elements in a single row and allows each element to be accessed using one index. In the Programming Mastery Course, students should understand one-dimensional arrays as the foundation for working with lists of values, loops, searching, sorting, totals, averages, and many future data structure concepts.