Write a segment of Java code that will copy the array elemen

 Write a segment of Java code that will copy the array elements     a[0], a[1], ..., a[n - 1] into the array b[] in reverse order,     with a[0] being copied into b[n - 1], a[1] copied into b[n - 2],     etc. and a[n - 1] copied into b[0].  Both arrays are type double.          Example:       Suppose n=5 and a[0]=8.1, a[1]=3.1, a[2]=0.5, a[3]=-5.5, and a[4]=6.9 .       Then after your code executes, the elements of a[] should be     unchanged and                 b[0]=6.9, b[1]=-5.5, b[2]=0.5, b[3]=3.1, and b[4]=8.1  

Solution

StoreArrayInReverseOrder.java

import java.util.Scanner;

public class StoreArrayInReverseOrder {

   public static void main(String[] args) {
       //Declaring variables
       int size,n;
      
       //Scanner class Object is used to read the inputs entered by the user
               Scanner sc=new Scanner(System.in);
      
               //Getting the size of the array entered by the user
       System.out.print(\"Enter the Size of the array :\");
       size=sc.nextInt();
      
       //Assigning the size to an integer type variable
       n=size;
      
       //Creating two double type arrays based on user entered size.
       double a[]=new double[size];
       double b[]=new double[size];
      
       //this for loop will get the values entered by the user
       for(int i=0;i<size;i++)
       {
           //Getting the numbers entered by the user
           System.out.print(\"Enter the element#\"+(i+1)+\":\");
           a[i]=sc.nextDouble();
          
           b[--n]=a[i];
       }
      
       //Displaying the elements in the array1[]
       System.out.println(\"The Elements in the First array are :\");
       for(int i=0;i<size;i++)
       {
System.out.println(a[i]+\" \");
       }
      
       //Displaying the elements in the array2[]
       System.out.println(\"The Elements in the Second array are :\");
       for(int i=0;i<size;i++)
       {
System.out.println(b[i]+\" \");
       }

   }

}

_________________________________

Output:

Enter the Size of the array :5
Enter the element#1:8.1
Enter the element#2:3.1
Enter the element#3:0.5
Enter the element#4:5.5
Enter the element#5:6.9
The Elements in the First array are :
8.1
3.1
0.5
5.5
6.9
The Elements in the Second array are :
6.9
5.5
0.5
3.1
8.1

_______Thank You

 Write a segment of Java code that will copy the array elements a[0], a[1], ..., a[n - 1] into the array b[] in reverse order, with a[0] being copied into b[n -
 Write a segment of Java code that will copy the array elements a[0], a[1], ..., a[n - 1] into the array b[] in reverse order, with a[0] being copied into b[n -

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site