Answer to Question #38007 in C++ for tehreem e amna

Question #38007
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;

cin.get();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS