C++ Arrays


In C++, an array is a collection of elements of the same data type stored in consecutive memory locations. Arrays make it easier to store and manage large amounts of related data without using multiple separate variables.

For example, instead of declaring 10 different variables to store marks of 10 students, you can declare one array to store them all.

What Is an Array in C++?

An array is a fixed-size sequence of elements that share the same data type.
Each element in the array can be accessed using an index number, which starts from 0.

Example

int marks[5] = {90, 85, 88, 92, 75};

Here:

  • int → data type of the array

  • marks → name of the array

  • [5] → number of elements the array can hold

Declaring an Array in C++

Syntax:

dataType arrayName[arraySize];

Example:

int numbers[10];
float prices[5];
char letters[26];

You can declare arrays of any valid data type — integers, floats, characters, or even user-defined structures.

Initializing an Array in C++

There are several ways to initialize an array in C++.

1. Static Initialization

int numbers[5] = {1, 2, 3, 4, 5};

2. Partial Initialization

If you provide fewer elements than the array size, the remaining elements automatically become 0.

int numbers[5] = {10, 20};

Output: 10, 20, 0, 0, 0

3. Implicit Size Determination

If you don’t specify the size, C++ will calculate it automatically.

int numbers[] = {2, 4, 6, 8};

Here, the size of the array is automatically set to 4.

Accessing Array Elements

Each array element can be accessed using its index number.

Example:

#include <iostream>
using namespace std;

int main() {
    int marks[5] = {90, 85, 88, 92, 75};
    cout << "First mark: " << marks[0] << endl;
    cout << "Last mark: " << marks[4];
    return 0;
}

Output:

First mark: 90
Last mark: 75

Modifying Array Elements

You can update any element in an array by directly referring to its index.

Example:

marks[2] = 95;

This changes the third element (index 2) to 95.

Looping Through an Array

Most commonly, arrays are processed using loops.

Example: Using a For Loop

#include <iostream>
using namespace std;

int main() {
    int numbers[5] = {10, 20, 30, 40, 50};

    for (int i = 0; i < 5; i++) {
        cout << "Element " << i << ": " << numbers[i] << endl;
    }
    return 0;
}

Output:

Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50

Example: Summing All Array Elements

#include <iostream>
using namespace std;

int main() {
    int numbers[5] = {2, 4, 6, 8, 10};
    int sum = 0;

    for (int i = 0; i < 5; i++) {
        sum += numbers[i];
    }

    cout << "Sum = " << sum;
    return 0;
}

Output:

Sum = 30

Array with User Input

You can also take array values directly from the user.

#include <iostream>
using namespace std;

int main() {
    int scores[5];

    cout << "Enter 5 scores: ";
    for (int i = 0; i < 5; i++) {
        cin >> scores[i];
    }

    cout << "You entered: ";
    for (int i = 0; i < 5; i++) {
        cout << scores[i] << " ";
    }

    return 0;
}

Sample Output:

Enter 5 scores: 10 20 30 40 50
You entered: 10 20 30 40 50

Array Memory Representation

Arrays occupy continuous memory locations.
For example:

int arr[3] = {10, 20, 30};

If the base address (first element) is at memory location 1000 and each integer takes 4 bytes:

  • arr[0] → 1000

  • arr[1] → 1004

  • arr[2] → 1008

This continuous layout helps C++ access elements quickly using the formula:

Address = Base Address + (Index × Size of each element)

Multidimensional Arrays

A multidimensional array stores data in a table or matrix form.
The most common is the two-dimensional array.

Example:

int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

You can visualize it like this:

1 2 3
4 5 6

Accessing Elements:

cout << matrix[1][2];

Output: 6

Example: Displaying a 2D Array

#include <iostream>
using namespace std;

int main() {
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Output:

1 2 3
4 5 6

Limitations of Arrays

  1. Fixed size: Once defined, the array size cannot change.

  2. Same data type: All elements must be of the same type.

  3. No automatic bounds checking: Accessing elements outside valid indexes can lead to errors.

Advantages of Using Arrays

  • Easy to store and manage multiple data elements.

  • Allows fast access using index numbers.

  • Supports looping and efficient computation.

  • Provides a simple structure for working with related data.

Example: Finding the Maximum Element

#include <iostream>
using namespace std;

int main() {
    int arr[5] = {23, 45, 12, 67, 34};
    int max = arr[0];

    for (int i = 1; i < 5; i++) {
        if (arr[i] > max)
            max = arr[i];
    }

    cout << "Maximum element: " << max;
    return 0;
}

Output:

Maximum element: 67

Summary of C++ Arrays

Arrays in C++ allow you to store and manipulate multiple values of the same type efficiently.
They are foundational data structures used for storing sequences, managing tables, and performing repetitive operations easily.

Key takeaways:

  • Arrays use zero-based indexing.

  • You can use loops for processing large data.

  • Two-dimensional arrays are useful for matrices and tabular data.

  • Arrays provide speed and simplicity but have fixed sizes.


Practice Questions

  1. Write a C++ program to input 10 integers into an array and display them in reverse order.

  2. Create a program to find the maximum and minimum elements in an array of 8 integers.

  3. Write a C++ program that takes 5 numbers as input and calculates their average using an array.

  4. Develop a program to count how many even and odd numbers are present in an array.

  5. Write a C++ program to find the sum of all elements in an array of size 10.

  6. Create a program to search for a specific number in an array and display its position if found.

  7. Write a C++ program to copy all elements from one array to another.

  8. Write a program that takes 10 numbers and sorts them in ascending order using an array.

  9. Create a C++ program that stores marks of 5 subjects in an array and calculates the total and percentage.

  10. Write a C++ program that merges two arrays into a single array and displays the combined result.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top