A simple way to reverse the order of elements in an array with c plus plus
This is a simple way to reverse the order of elements in an array, using c++
#include <iostream>
int main ()
{
// Initialize random number generator
srand(time(NULL));
// Define the length of the array
const int LENGHT = 10;
// Create an array of LENGTH elements
int v[LENGHT];
// Fill the array with random integers
for (int i=0; i<LENGHT; i++) v[i] = (rand()%100)+1;
// Showing the contents of the array
printf("The content of array\n");
for (int i=0; i<LENGHT; i++) printf("%d " , v[i]);
// Reverse the contents of the array
for (int i=0; i < LENGHT/2; i++)
{
int temp;
temp = v[i];
v[i] = v[LENGHT-1-i];
v[LENGHT-1-i] = temp;
}
printf("\n\n");
// Showing the contents of the array reversed
printf("The contents of the array reversed\n");
for (int i=0; i<LENGHT; i++) printf("%d " , v[i]);
return 0;
}
This is the result:
The content of array
83 65 55 21 52 67 78 78 27 34
The contents of the array reversed
34 27 78 78 67 52 21 55 65 83