Reverse an Array in C++

In this exercise, you will implement a program that reverses an array. This task is useful in many scenarios such as reversing a list of elements, manipulating data structures, and understanding array indexing.

The program should use a loop to iterate over the array and swap elements, effectively reversing the order. This will help you practice array manipulation, swapping elements, and using loops in C++.

Group

Arrays and Vectors in C++

Objective

1. Define an array with several elements.
2. Implement a function or logic to swap elements in the array.
3. Start from both ends of the array and swap the elements until you reach the middle.
4. Output the reversed array.
5. Test the program with different arrays to ensure it works correctly.

Write a program that reverses an array.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include iostream for input and output
using namespace std; // Use the standard namespace

// Function to reverse an array
void reverseArray(int arr[], int size) {
    int temp; // Temporary variable for swapping

    // Loop through the array to swap elements from both ends
    for (int i = 0; i < size / 2; i++) {
        temp = arr[i]; // Store the current element in temp
        arr[i] = arr[size - 1 - i]; // Swap the current element with the element from the other end
        arr[size - 1 - i] = temp; // Put the value from temp into the other end
    }
}

int main() {
    // Define an array of numbers
    int numbers[] = {1, 2, 3, 4, 5};
    int size = sizeof(numbers) / sizeof(numbers[0]); // Calculate the size of the array

    // Output the original array
    cout << "Original array: ";
    for (int i = 0; i < size; i++) {
        cout << numbers[i] << " "; // Print each element in the array
    }
    cout << endl;

    // Reverse the array
    reverseArray(numbers, size);

    // Output the reversed array
    cout << "Reversed array: ";
    for (int i = 0; i < size; i++) {
        cout << numbers[i] << " "; // Print each element of the reversed array
    }
    cout << endl;

    return 0; // End of program
}

 Output

Original array: 1 2 3 4 5 
Reversed array: 5 4 3 2 1

Share this C++ Exercise


More C++ Programming Exercises of Arrays and Vectors in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Sum Two Arrays Element by Element in C++

    In this exercise, you will implement a function that sums two arrays element by element. This means that for two given arrays of the same size, the function will return a new array...

  • Remove Duplicates from an Array in C++

    In this exercise, you will create a program that removes duplicates from an array of numbers. The function will iterate over the array and remove any duplicate values, leaving only...

  • Check if an Array is Symmetric in C++

    In this exercise, you will create a function to determine if an array is symmetric. An array is considered symmetric if it reads the same forward and backward, meaning the first el...

  • Sort an Array of Numbers in Ascending Order in C++

    In this exercise, you will implement a program that sorts an array of numbers in ascending order. Sorting is one of the most fundamental operations in programming, and there are se...

  • Sum of Array Elements in C++

    In this exercise, you will create a program that calculates the sum of all the elements in an array. Summing the elements of an array is a common task in programming, which is ofte...

  • Search for a Number in an Array in C++

    In this exercise, you will create a program that searches for a specific number in an array. Searching through an array is a common task in programming, which is used in various ap...