Results 1 to 13 of 13
  1. #1
    severus1 is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default Reversing an array

    im getting incorrect output in my code of reversing an array,plz help
    Java Code:
    import java.util.*;
    class a
    {
    public static void main(String args[])
    {
    int i,temp;
    int []a=new int[5];
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the elements of the first array");
    for(i=0;i<5;i++)
    {
    a[i]=sc.nextInt();
    }
    for(i=0;i<2;i++)
    {
    temp=a[i];
    a[i]=a[4-i];
    a[4-i]=temp;
    }
    System.out.println("The reversed array is:");
    System.out.println(a[i]);
    }
    }
    Last edited by severus1; 06-30-2011 at 10:29 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    im getting incorrect output in my code
    Can you show us what you are getting?

    Please wrap you code in code tags to preserver its formatting. Use the # icon above to right of input area.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Your code is hardcoded to only work for 5 elements.
    What would you have to do to make it work for 8 elements or 50 elements?

  4. #4
    severus1 is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    if im entering the input as
    1
    2
    3
    4
    5
    im getting 3 as the output ie im getting a[2] as the output

  5. #5
    severus1 is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Your code is hardcoded to only work for 5 elements.
    What would you have to do to make it work for 8 elements or 50 elements?
    then how to make it generic,will int []a=new int[]; work?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    to make it generic
    To generate an array to sort you'll have to specify its size:
    int[] anArray = new int[333];
    The code to reverse the array should get anArray as an argument and use the anArray.length property to get its size.

    See the Arrays class toString() method as an easy way to print the contents of an array.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Cross posted at Reversing an array

  8. #8
    severus1 is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    so whats the bug in my code?i think the logic is rite

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Re-posting your code in code tags will encourage me to read it. Without it I don't really feel like deciphering your scribbles.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  10. #10
    severus1 is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Dark View Post
    Re-posting your code in code tags will encourage me to read it. Without it I don't really feel like deciphering your scribbles.
    sorry,done

  11. #11
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Well when I said putting it in code tags, you have to put the formatted code in there otherwise it will still look exactly like it did before. Just in a blue box.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  12. #12
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    However, I took a look at it anyways. Honestly I have no idea what you are doing.
    Java Code:
    for(i=0;i<2;i++)
    		{
    			temp=a[i];
    			a[i]=a[4-i];
    			a[4-i]=temp;
    		}
    This seems way to complicated for what you are trying to accomplish. And you're only getting one input because this line of code is outside of a loop, so it will only run once.

    Java Code:
    System.out.println("The reversed array is:");
    System.out.println(a[i]);
    My advice is that once you created your original array, you just loop through it backwards. Instead of using more variables to try and find which slot you should be fetching information from.

    For loops don't have to start at 0 and go up, you can set them to the length of your array and work down.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  13. #13
    severus1 is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Array reverse algorithim - reversing elements
    By Adomini in forum New To Java
    Replies: 9
    Last Post: 08-30-2010, 04:13 AM
  2. Reversing
    By whosadork in forum New To Java
    Replies: 14
    Last Post: 11-06-2008, 04:29 AM
  3. Reversing String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-02-2007, 09:42 PM
  4. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:24 PM
  5. reversing a string
    By toad in forum New To Java
    Replies: 5
    Last Post: 11-07-2007, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •