Reverse an Array in O

Beginner

Introduction

In this lab, you will learn how to reverse an array in the C++ programming language using different techniques. Array reversal means changing the order of an array where the last element of the array becomes the first, and the first element becomes the last. This lab will teach you how to accomplish array reversal using different methods, including for loops, built-in functions, pointers, recursion, and user-defined functions.

Create a new C++ file

Create a new C++ file named reverse_array.cpp in the ~/project directory by running the following command:

touch ~/project/reverse_array.cpp

Reverse an array using a for loop

The following code demonstrates how to reverse an array using a for loop:

#include <iostream>
using namespace std;
void reverseArray(int arr[], int size){
    for(int i=0;i<size/2;i++){
        int temp = arr[i];
        arr[i] = arr[size-i-1];
        arr[size-i-1] = temp;
    }
}
int main(){
    int arr[]={1,2,3,4,5};
    int size = sizeof(arr)/sizeof(arr[0]);
    cout<<"Original Array: ";
    for(int i=0;i<size;i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    reverseArray(arr,size);
    cout<<"Reversed Array: ";
    for(int i=0;i<size;i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    return 0;
}

To compile and run the code, use the following commands:

g++ reverse_array.cpp -o reverse_array
./reverse_array

Reverse an array using a built-in function

The following code demonstrates how to reverse an array using a built-in C++ function:

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int arr[]={1,2,3,4,5};
    int size = sizeof(arr)/sizeof(arr[0]);
    cout<<"Original Array: ";
    for(int i=0;i<size;i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    reverse(arr,arr+size);
    cout<<"Reversed Array: ";
    for(int i=0;i<size;i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    return 0;
}

To compile and run the code, use the following commands:

g++ reverse_array.cpp -o reverse_array
./reverse_array

Reverse an array using a user-defined function

The following code demonstrates how to define a user-defined function to reverse an array:

#include <iostream>
using namespace std;
void reverseArray(int arr[], int size){
    for(int i=0;i<size/2;i++){
        int temp = arr[i];
        arr[i] = arr[size-i-1];
        arr[size-i-1] = temp;
    }
}
int main(){
    int arr[]={1,2,3,4,5};
    int size = sizeof(arr)/sizeof(arr[0]);
    cout<<"Original Array: ";
    for(int i=0;i<size;i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    reverseArray(arr,size);
    cout<<"Reversed Array: ";
    for(int i=0;i<size;i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    return 0;
}

To compile and run the code, use the following commands:

g++ reverse_array.cpp -o reverse_array
./reverse_array

Reverse an array using pointers

The following code demonstrates how to reverse an array using pointers:

#include <iostream>
using namespace std;
void reverseArray(int *arr, int size){
    int *first = arr, *last = arr + size - 1;
    while(first < last){
        int temp = *first;
        *first = *last;
        *last = temp;
        first++;
        last--;
    }
}
int main(){
   int arr[] = {1, 2, 3, 4, 5};
   int size = sizeof(arr)/sizeof(arr[0]);
   cout<<"Original Array: ";
    for(int i=0;i<size;i++)
        cout<<arr[i]<<" ";
    cout<<endl;
   reverseArray(arr, size);
   cout<<"Reversed Array: ";
    for(int i=0;i<size;i++)
        cout<<arr[i]<<" ";
    cout<<endl;
   return 0;
}

To compile and run the code, use the following commands:

g++ reverse_array.cpp -o reverse_array
./reverse_array

Reverse an array using recursion

The following code demonstrates how to reverse an array using recursion:

#include <iostream>
using namespace std;
void reverseArray(int arr[], int start, int end){
    if(start >= end)
        return;
    int temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;
    reverseArray(arr,start+1,end-1);
}
int main(){
   int arr[] = {1, 2, 3, 4, 5};
   int size = sizeof(arr)/sizeof(arr[0]);
   cout<<"Original Array: ";
    for(int i=0;i<size;i++)
        cout<<arr[i]<<" ";
    cout<<endl;
   reverseArray(arr,0,size-1);
   cout<<"Reversed Array: ";
    for(int i=0;i<size;i++)
        cout<<arr[i]<<" ";
    cout<<endl;
   return 0;
}

To compile and run the code, use the following commands:

g++ reverse_array.cpp -o reverse_array
./reverse_array

Summary

Congratulations! You have completed the Reverse an Array in O(n) lab. You can practice more labs in LabEx to improve your skills.

Other Tutorials you may like