write a function that reverses the elements of an array so that last element becomes the first, the second from the last becomes the second, and so forth. The function is to reverse the elements in place-that is. without using another array.(it is permissible to use a variable to hold an element temporarily).
1
Expert's answer
2013-12-25T09:19:59-0500
#include <iostream> using namespace std;
const int SIZE = 9;
int main() { int arr[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; cout << "Initial array:" << endl; for (int i = 0; i < SIZE; i ++) cout << arr[i] << " "; cout << endl;
for (int i = 0; i < SIZE/2; i ++) { int temp = arr[i]; arr[i] = arr[SIZE - i - 1]; arr[SIZE - i - 1] = temp; }
cout << "Final array:" << endl; for (int i = 0; i < SIZE; i ++) cout << arr[i] << " "; cout << endl;
Comments
Leave a comment