Write a program reverse an array in C++/C
#include <iostream>
using namespace std;
void display(int *arr, int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
void reverseArray(int arr[], int n)
{
int start = 0;
int end = n - 1;
while (start < end)
{
swap(arr[start++], arr[end--]);
}
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(int);
cout << "The array is : ";
display(arr, n);
cout << "The Reverse Array is : ";
reverseArray(arr, n);
display(arr, n);
}
Output is :
The array is : 1 2 3 4 5
The Reverse Array is : 5 4 3 2 1
0 Comments