The assignment was to accept an array 'a', pick up alternate values from 'a', store them in reverse order in another array 'b' and print the values of 'b'. I wrote the following code, but the values of 'b' getting printed are all 0.
import java.io.*;
public class Assignment
{
public int[] array(int[] a)
{
int l=a.length;
int x=l-1;
int[] b=new int[l];
for(int i=x;i>=0;i=-2)
{
b[x-i]=a[i];
}
return b;
}
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Assignment asg=new Assignment();
System.out.println("How many numbers do you want in the array?");
int l=Integer.parseInt(br.readLine());
System.out.println("Enter the numbers");
int[] a =new int[l];
for(int i=0;i<l;i++)
a[i]=Integer.parseInt(br.readLine());
int[] b=asg.array(a);
for(int j=0;j<l;j++)
System.out.println(b[j]);
}
}