0
0
C++programming~5 mins

One-dimensional arrays in C++

Choose your learning style9 modes available
Introduction

One-dimensional arrays help you store many values of the same type in a single list. This makes it easy to organize and access data using a simple number called an index.

When you want to keep a list of student scores in a test.
When you need to store daily temperatures for a week.
When you want to hold a sequence of numbers to process them one by one.
When you want to remember the names of players in a game (using an array of strings).
When you want to quickly access data by position without creating many separate variables.
Syntax
C++
class OneDimensionalArray {
public:
    int size;
    int* elements;

    OneDimensionalArray(int array_size) {
        size = array_size;
        elements = new int[size];
    }

    ~OneDimensionalArray() {
        delete[] elements;
    }

    void setElement(int index, int value) {
        if (index >= 0 && index < size) {
            elements[index] = value;
        }
    }

    int getElement(int index) {
        if (index >= 0 && index < size) {
            return elements[index];
        }
        return -1; // Return -1 if index is invalid
    }

    void printArray() {
        for (int i = 0; i < size; i++) {
            std::cout << elements[i] << " ";
        }
        std::cout << std::endl;
    }
};

The array size must be known when creating the array.

Indexing starts at 0, so the first element is at index 0.

Examples
This creates an array of size 5, sets the first and last elements, and prints the array.
C++
OneDimensionalArray arr(5);
arr.setElement(0, 10);
arr.setElement(4, 50);
arr.printArray();
This shows an empty array with size 0. It prints nothing but does not crash.
C++
OneDimensionalArray emptyArray(0);
emptyArray.printArray();
This creates an array with only one element and sets it to 99.
C++
OneDimensionalArray singleElementArray(1);
singleElementArray.setElement(0, 99);
singleElementArray.printArray();
Trying to set elements outside valid indexes does nothing, preventing errors.
C++
OneDimensionalArray arr(3);
arr.setElement(-1, 10); // ignored
arr.setElement(3, 20);  // ignored
arr.printArray();
Sample Program

This program creates a one-dimensional array of size 5, initializes all elements to 0, then sets values at each index. It prints the array before and after setting values. It also shows how to get elements safely, returning -1 for invalid indexes.

C++
#include <iostream>

class OneDimensionalArray {
public:
    int size;
    int* elements;

    OneDimensionalArray(int array_size) {
        size = array_size;
        elements = new int[size];
        for (int i = 0; i < size; i++) {
            elements[i] = 0; // Initialize all elements to 0
        }
    }

    ~OneDimensionalArray() {
        delete[] elements;
    }

    void setElement(int index, int value) {
        if (index >= 0 && index < size) {
            elements[index] = value;
        }
    }

    int getElement(int index) {
        if (index >= 0 && index < size) {
            return elements[index];
        }
        return -1; // Return -1 if index is invalid
    }

    void printArray() {
        for (int i = 0; i < size; i++) {
            std::cout << elements[i] << " ";
        }
        std::cout << std::endl;
    }
};

int main() {
    std::cout << "Creating array of size 5..." << std::endl;
    OneDimensionalArray numbers(5);

    std::cout << "Initial array: ";
    numbers.printArray();

    std::cout << "Setting values..." << std::endl;
    numbers.setElement(0, 10);
    numbers.setElement(1, 20);
    numbers.setElement(2, 30);
    numbers.setElement(3, 40);
    numbers.setElement(4, 50);

    std::cout << "Array after setting values: ";
    numbers.printArray();

    std::cout << "Trying to get element at index 2: ";
    std::cout << numbers.getElement(2) << std::endl;

    std::cout << "Trying to get element at invalid index 10: ";
    std::cout << numbers.getElement(10) << std::endl;

    return 0;
}
OutputSuccess
Important Notes

Time complexity for accessing or setting an element by index is O(1) because you go directly to the position.

Space complexity is O(n), where n is the size of the array.

Common mistake: Accessing indexes outside the array size causes errors or undefined behavior, so always check index bounds.

Use one-dimensional arrays when you have a fixed number of elements and want fast access by position.

Summary

One-dimensional arrays store multiple values of the same type in a list accessed by index.

Indexes start at 0 and go up to size-1.

They provide fast access and are useful for simple collections of data.