Answer:
I am writing a JAVA program:
import java.util.Scanner; //Scanner class is used to take input from user
public class ReverseArray{ //class to reverse the input array
static 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; }
System.out.println("Reversed array: \\"); //prints the resultant reversed array after swapping process
for (j = 0; j < size; j++) { //loop to print the reversed array elements
System.out.println(array[j]); } }
public static void main(String[] args) { //start of main() function body
Scanner input=new Scanner(System.in); // create Scanner class object
System.out.println("Enter the length of array: "); //prompts user to enter the length of the array
int n =input.nextInt(); //scans and reads the input size of array
int A[]=new int[n]; // creates an array of size n
System.out.println("Enter the elements: "); //prompts user to enter array elements
for(int i=0;i<n;i++){ //loop for reading the elements of array
A[i]=input.nextInt(); }
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
If you do not want to take input from user then you can declare an array and assign elements to it as:
int [] A = {1, 2, 3, 4};
Then call reverse function by passing A[] and A.length which computes the length of the array
reverse(A, A.length);
The program and output according to the example 2,5,9,7 given in the question is attached as a screenshot.