C++ Arrays

Introduction

Arrays in C++ are used to store multiple values of the same type in a single variable. They are a fundamental data structure that provides a convenient way to manage collections of data. Understanding how to use arrays effectively is crucial for handling large amounts of data in your programs.

Declaring and Initializing Arrays

Syntax for Declaring an Array

dataType arrayName[arraySize];

Example: Declaring and Initializing an Array

#include <iostream>
using namespace std;

int main() {
    int numbers[5]; // Declares an array of 5 integers

    // Initializing the array
    numbers[0] = 10;
    numbers[1] = 20;
    numbers[2] = 30;
    numbers[3] = 40;
    numbers[4] = 50;

    // Accessing and printing the array elements
    for (int i = 0; i < 5; i++) {
        cout << "numbers[" << i << "] = " << numbers[i] << endl;
    }

    return 0;
}

Output

numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50

Explanation

  • int numbers[5]; declares an array named numbers of size 5.
  • Each element of the array is accessed and initialized using the index.
  • The for loop is used to iterate through the array and print each element.

Initializing Arrays at Declaration

You can also initialize an array at the time of declaration.

Example: Initializing an Array at Declaration

#include <iostream>
using namespace std;

int main() {
    int numbers[5] = {10, 20, 30, 40, 50}; // Declares and initializes an array

    // Accessing and printing the array elements
    for (int i = 0; i < 5; i++) {
        cout << "numbers[" << i << "] = " << numbers[i] << endl;
    }

    return 0;
}

Output

numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50

Explanation

  • int numbers[5] = {10, 20, 30, 40, 50}; declares and initializes the array in a single line.

Array Size

The size of an array must be a constant expression and cannot be changed once declared. Using the sizeof operator, you can determine the size of the array.

Example: Determining the Size of an Array

#include <iostream>
using namespace std;

int main() {
    int numbers[] = {10, 20, 30, 40, 50}; // Array without explicitly specifying the size
    int size = sizeof(numbers) / sizeof(numbers[0]); // Calculate the size of the array

    cout << "The size of the array is: " << size << endl;

    return 0;
}

Output

The size of the array is: 5

Explanation

  • int size = sizeof(numbers) / sizeof(numbers[0]); calculates the number of elements in the array by dividing the total size of the array by the size of one element.

Multidimensional Arrays

C++ allows you to create arrays of more than one dimension. The most commonly used multidimensional array is the two-dimensional array.

Example: Declaring and Initializing a Two-Dimensional Array

#include <iostream>
using namespace std;

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

    // Accessing and printing the elements of the matrix
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << "matrix[" << i << "][" << j << "] = " << matrix[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Output

matrix[0][0] = 1 matrix[0][1] = 2 matrix[0][2] = 3
matrix[1][0] = 4 matrix[1][1] = 5 matrix[1][2] = 6
matrix[2][0] = 7 matrix[2][1] = 8 matrix[2][2] = 9

Explanation

  • int matrix[3][3] declares a two-dimensional array with 3 rows and 3 columns.
  • Nested for loops are used to iterate through the matrix and print each element.

Example Programs

Example 1: Finding the Largest Element in an Array

#include <iostream>
using namespace std;

int main() {
    int numbers[] = {10, 25, 5, 30, 15};
    int size = sizeof(numbers) / sizeof(numbers[0]);
    int max = numbers[0]; // Assume the first element is the largest

    for (int i = 1; i < size; i++) {
        if (numbers[i] > max) {
            max = numbers[i]; // Update max if the current element is larger
        }
    }

    cout << "The largest element in the array is: " << max << endl;

    return 0;
}

Output

The largest element in the array is: 30

Explanation

  • The program initializes an array numbers and calculates its size.
  • It iterates through the array to find the largest element and prints it.

Example 2: Summing Elements of an Array

#include <iostream>
using namespace std;

int main() {
    int numbers[] = {10, 20, 30, 40, 50};
    int size = sizeof(numbers) / sizeof(numbers[0]);
    int sum = 0;

    for (int i = 0; i < size; i++) {
        sum += numbers[i]; // Add each element to sum
    }

    cout << "The sum of the elements in the array is: " << sum << endl;

    return 0;
}

Output

The sum of the elements in the array is: 150

Explanation

  • The program initializes an array numbers and calculates its size.
  • It iterates through the array to sum the elements and prints the total.

Example 3: Transposing a Matrix

#include <iostream>
using namespace std;

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

    // Transpose the matrix
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }

    // Print the transposed matrix
    cout << "Transposed matrix:" << endl;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            cout << transpose[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Output

Transposed matrix:
1 4
2 5
3 6

Explanation

  • The program initializes a 2×3 matrix and declares a 3×2 matrix for the transpose.
  • It transposes the matrix and prints the transposed matrix.

Conclusion

Arrays in C++ are a powerful way to handle collections of data. This chapter covered how to declare, initialize, and access arrays, including multidimensional arrays. It also included example programs to demonstrate how to find the largest element in an array, sum the elements of an array, and transpose a matrix. Understanding how to use arrays effectively will help you manage and process data efficiently in your programs. In the next chapter, we will explore pointers in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top