0
0
C++programming~5 mins

Common array operations in C++

Choose your learning style9 modes available
Introduction

Arrays help us store many values in one place. Common operations let us add, find, or change these values easily.

You want to keep a list of scores in a game.
You need to find if a number exists in a list.
You want to change a value at a certain position.
You want to add a new value to the end of a list.
You want to print all values to see what is stored.
Syntax
C++
#include <iostream>
using namespace std;

class Array {
public:
    int* data;
    int size;
    int capacity;

    Array(int capacity) {
        this->capacity = capacity;
        size = 0;
        data = new int[capacity];
    }

    ~Array() {
        delete[] data;
    }

    void add(int value) {
        if (size < capacity) {
            data[size] = value;
            size++;
        } else {
            cout << "Array is full!" << endl;
        }
    }

    int find(int value) {
        for (int i = 0; i < size; i++) {
            if (data[i] == value) {
                return i;
            }
        }
        return -1; // not found
    }

    void update(int index, int newValue) {
        if (index >= 0 && index < size) {
            data[index] = newValue;
        } else {
            cout << "Index out of range!" << endl;
        }
    }

    void print() {
        for (int i = 0; i < size; i++) {
            cout << data[i] << " ";
        }
        cout << endl;
    }
};

This class creates a simple array with fixed capacity.

Operations include adding, finding, updating, and printing values.

Examples
Add two numbers and print: output will be '10 20 '
C++
Array numbers(5);
numbers.add(10);
numbers.add(20);
numbers.print();
Print an empty array: output will be empty line
C++
Array emptyArray(3);
emptyArray.print();
Update the only element from 5 to 15 and print
C++
Array singleElement(2);
singleElement.add(5);
singleElement.update(0, 15);
singleElement.print();
Try to add 4 elements to capacity 3 array; last add prints 'Array is full!'
C++
Array numbers(3);
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
Sample Program

This program creates an array with capacity 5. It adds three numbers, finds one, updates one, tries to add more than capacity, and prints the array at each step.

C++
#include <iostream>
using namespace std;

class Array {
public:
    int* data;
    int size;
    int capacity;

    Array(int capacity) {
        this->capacity = capacity;
        size = 0;
        data = new int[capacity];
    }

    ~Array() {
        delete[] data;
    }

    void add(int value) {
        if (size < capacity) {
            data[size] = value;
            size++;
        } else {
            cout << "Array is full!" << endl;
        }
    }

    int find(int value) {
        for (int i = 0; i < size; i++) {
            if (data[i] == value) {
                return i;
            }
        }
        return -1;
    }

    void update(int index, int newValue) {
        if (index >= 0 && index < size) {
            data[index] = newValue;
        } else {
            cout << "Index out of range!" << endl;
        }
    }

    void print() {
        for (int i = 0; i < size; i++) {
            cout << data[i] << " ";
        }
        cout << endl;
    }
};

int main() {
    Array numbers(5);
    cout << "Initial array: ";
    numbers.print();

    numbers.add(10);
    numbers.add(20);
    numbers.add(30);
    cout << "After adding 3 elements: ";
    numbers.print();

    int index = numbers.find(20);
    cout << "Index of 20: " << index << endl;

    numbers.update(1, 25);
    cout << "After updating index 1 to 25: ";
    numbers.print();

    numbers.add(40);
    numbers.add(50);
    numbers.add(60); // should print full message

    cout << "Final array: ";
    numbers.print();

    return 0;
}
OutputSuccess
Important Notes

Adding an element is O(1) if space is available.

Finding an element is O(n) because it checks each item.

Updating an element is O(1) if index is valid.

Common mistake: adding beyond capacity without resizing causes errors.

Use dynamic arrays or vectors if you need flexible size.

Summary

Arrays store multiple values in order.

Common operations: add, find, update, print.

Always check array size before adding new elements.