﻿ C++ Exercises: Reverse a given array of integers and length 5 - w3resource # C++ Exercises: Reverse a given array of integers and length 5

## C++ Basic Algorithm: Exercise-86 with Solution

Write a C++ program to reverse a given array of integers and length 5.

Sample Solution:

C++ Code :

``````#include <iostream>
using namespace std;

int *test(int nums[]) {
static int  r_array[] = {nums, nums, nums, nums, nums };
return r_array;
}

int main () {
// a pointer to an int.
int *p;
int nums[] = {0, 10, 20, 30, 40};

int arr_length = sizeof(nums) / sizeof(nums);
cout << "Original array: " << endl;
for ( int i = 0; i < arr_length; i++ ) {
cout << nums[i] << " ";
}

p = test(nums);
cout << "\nReverse array: " << endl;
for ( int i = 0; i < arr_length; i++ ) {
cout << *(p + i) << " ";
}

return 0;
}
``````

Sample Output:

```Original array:
0 10 20 30 40
Reverse array:
40 30 20 10 0
```

Pictorial Presentation: Flowchart: C++ Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

﻿