0

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]);
    }
}
0

4 Answers 4

1

After fixing the main method signature change the code to as follows:

try
        {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        MainClass asg=new MainClass();
        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);
        int newSize = 0;
        if(l% 2 == 0)
            newSize = l/2;
        else
            newSize = (l/2) +1;
        for(int j=0;j<newSize;j++) System.out.println(b[j]);
        }
        catch(Exception ex)
    {
        ex.printStackTrace();
    }


public int[] array(int[] a)
        {
            int l=a.length;
            int x=l-1;
            int newSize = 0;
            if(l% 2 == 0)
                newSize = l/2;
            else
                newSize = (l/2) +1;

            int[] b=new int[newSize];
            int i = 0;
            while(x >= 0)
                {
                    b[i]=a[x];
                    i++;
                    x -= 2;
                }

            return b;
         }

The length of b should be half of a and not same as a.

Sign up to request clarification or add additional context in comments.

1 Comment

The code works perfectly, thank you. Although I removed the catch(Exception ex) block because I got an error that 'try' statement was missing and that we aren't allowed to use anything out of syllabus (high school education is sometimes restrictive). Anyway, I appreciate your efforts.
0

The main method signature is not right and check this i=-2 condition.

Comments

0

main method signature must look like this :

 public static void main(String s[]){
        ....
}

here in for() loop i should be decremented by 1;

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--)  // decrement i by 1;
            {
                b[x-i]=a[i];
            }
            return b;
         }

Comments

0

First, your prgram would not compile - main() method has wrong signature. Use

public static void main(String[] args) {
...
}

Then change loop storing values in new array to:

for(int i = x; i >= 0; i--) {
    b[x - i] = a[i];
}

1 Comment

I don't have enough 'reputation' to downvote. And i don't know who voted you down or why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.