81.1k views
1 vote
g 18.6 [Contest 6 - 07/10] Reverse an array Reversing an array is a common task. One approach copies to a second array in reverse, then copies the second array back to the first. However, to save space, reversing an array without using a second array is sometimes preferable. Write a function that reads in an integer representing the length of the array. Then read in and reverse the given array, without using a second array. If the original array's values are 2 5 9 7, the array after reversing is 7 9 5 2. Write a method to reverse the input array and call the method in the main. Note: The first item in the input is the length of the input array. The rest of the items in the input are the elements stored inside of the input array. Hints: Use this approach: Swap the first and last elements, then swap the second and second-to-last elements, etc. Stop when you reach the middle; else, you'll reverse the vector twice, ending with the original order. Think about the case when the number of elements is even, and when odd. Make sure your code handles both cases.

User Ibtsam
by
4.9k points

1 Answer

6 votes

Answer:

I am writing C++ program. Let me know if you want the program in some other programming language.

#include <iostream> //to use input output functions

using namespace std; //to identify objects like cin cout

void reverse(int array[], int size){ //method to reverse an array

int i, j, temp; //declare variables

for (i = 0; i < size / 2; i++) { /* the loop swaps the first and last elements, then swap the second and second-to-last elements and so on until the middle of the array is reached */

temp = array[i];

array[i] = array[size - i - 1];

array[size - i - 1] = temp; }

cout<<"Reversed array: "; //prints the resultant reversed array after swapping process

for (j = 0; j < size; j++) { //loop to print the reversed array elements

cout<<array[j]<<" "; } }

int main() { //start of main() function body

cout<<"Enter the length of array: "; //prompts user to enter the length of the array

int n; //stores the value of size of array

cin>>n; //reads the input size of array

int A[n]; //declare array A of size n

cout<<"Enter the elements: "; //prompts user to enter array elements

for(int i=0;i<n;i++){ //loop for reading the elements of array

cin >> A[i]; }

reverse(A,n); } //calls reverse function to reverse the elements of the input array A by passing array A and its length n to function reverse

Explanation:

The reverse function takes an array and its length as parameter. In the body of reverse function three integer type variables i,j and temp are declared.

for (i = 0; i < size / 2; i++) The for loop has a variable to iterate through the array. The for loop checks if the value of i is less than the array length. Lets say we have an array of elements 1, 2, 3, 4. As i=0 and size/2 = 4/2= 2. So the condition checked in for loop is true as 0<2 which means the body of for loop will be executed.

In the body of the loop there are three swap statements.

temp = array[i] This statement stores the element of array at i-th index to temp variable.

array[i] = array[size - i - 1] statement stores the element of array at size-i-1 to array index i.

array[size - i - 1] = temp statement stores the the value in temp variable to the array index size-i-1

In simple words first, the first and last elements of array are swapped, then the second and second last elements are swapped. This process keeps repeating and for loop keeps executing these swap statements until the middle of the array is reached. In this program no extra array is used for reversing procedure but just a variable temp is used to hold the array elements temporarily.

array[4] = { 1 , 2 , 3 , 4}

At first iteration

i < size/2 is true as 0<2

So the program control moves in the body of the loop where three swap statement works as following:

temp = array[i]

temp = array[0]

As element at 0-th index which is the first element of array is 1 so

temp= 1

array[i] = array[size - i - 1];

array[0] = array[4-0-1]

= array[3]

As the element at 3rd index of array is 4 which is the last element of the array so

array[0] = 4

This means that 4 becomes the first element of array now.

array[size - i - 1] = temp

As the value in temp was set to 1 so

array[4-0-1] = 1

array[3] = 1

This means 1 is assigned to the 3rd index of array. Which means 1 becomes the last element of the array now.

This is how the first and last elements of array are swapped.

2nd iteration: value of i is incremented to 1 and now i = 1

Again for loop condition is checked which evaluates to true as 1<2

temp = array[i]

temp = array[1]

As element at 1st index which is the second element of array is 2 so

temp= 2

array[i] = array[size - i - 1];

array[1] = array[4-1-1]

= array[2]

As the element at second index of array is 3 which is the last element of the array so

array[1] = 3

This means that 3 becomes the second element of array now.

array[size - i - 1] = temp

As the value in temp was set to 2 so

array[4-1-1] = 2

This means 2 is assigned to the 2nd index of array. Which means 2 becomes the second last element of the array now.

This is how the second and second to last elements of array are swapped.

3rd iteration: value of i=2 The for loop body will not execute now as the for loop condition i<size-2 evaluates to false because 2=2. So the loop stops and next for (j = 0; j < size; j++) loop iterates through array[] which has now been reversed and print the elements of this reversed array.

Next the main() method passes length of the array and the array elements to the reverse function and prints these elements in reverse.

The output is:

4 3 2 1

The program and output according to the example 2,5,9,7 given in the question is attached as a screenshot.

g 18.6 [Contest 6 - 07/10] Reverse an array Reversing an array is a common task. One-example-1
User Vitalii Ponomar
by
4.6k points