Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Reversing an array

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Reversing an array

    im getting incorrect output in my code of reversing an array,plz help
    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]);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Reversing an array

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/45905-reversing-array.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Reversing an array

    i think my logic is rite,plz find the bug

  4. #4
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Reversing an array

    Quote Originally Posted by severus1 View Post
    im getting incorrect output in my code of reversing an array,plz help
    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:");
    for(i=0;i<5;i++)
        System.out.println(a[i]);
    }
    }

    notice that you just output only ONE element in your original code. after the last loop, the counter "i" had the value of the last iteration, so it output a[2] only.
    You have to loop through the entire array to display all its elements, hence for loop i added.
    My website : MediDev

  5. The Following User Says Thank You to hexwind For This Useful Post:

    severus1 (June 30th, 2011)

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Reversing an array

    i think my logic is rite
    The problem with your code is that it is hardcoded for an array with exactly 5 elements.
    Try changing your code to take an array of any length.

  7. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Reversing an array

    thx a lot hexwind

  8. #7
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Reversing an array

    anytime, glad it helped
    My website : MediDev

Similar Threads

  1. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  2. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  3. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM
  4. Reversing lines using LinkedList
    By velop in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 14th, 2011, 05:10 AM
  5. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 04:29 PM