Results 1 to 1 of 1
  1. #1
    dkofek's Avatar
    Join Date
    Feb 2009
    Gender
    male
    Location
    The toilet in the club next to your house
    Posts
    59
    Reputation
    13
    Thanks
    114
    My Mood
    Bored

    Recursive way to "shift"

    so title says it all, I've been trying to figure out a recursive way to "shift" an array - rotate it.
    I have found a way but I've been told its not good since its using a temp... so here I am asking you for your advice for a recursive procedure that doesnt use a temp(temporary parameter).

    here's my code so far:

    Code:
    public static void moving(int[]a,int length,int temp)
    {
            if(length==0)
                System.out.print("");
            else
            {
                temp=a[a.length-length-1];
                a[a.length-length-1]=a[a.length-1]
                a[a.length-1]=temp;
                moving(a,length-1,temp);        
            }
        }
    so I put a bit more thought into this and found out the way I was aiming to:
    Code:
    public static void moveLikeJagger(int[] a,int length,int k)
    {
    if(length==0)
        a[length]=k;
    else
    {
        a[length]=a[length-1];
        moveLikeJagger(a,length-1,k);
    }
    }
    the main should look like this:
    Code:
    public static void main(String[] args)
    {
        System.out.println("enter array's length");
        int n=reader.nextInt();//reader is the Scanner name
        int[] a=new int[n];
        moveLikeJagger(a,a.length-1,a[a.length-1]);
    }
    Have fun using that code, all rights reserved to me (at least give me credits!)
    Last edited by dkofek; 09-26-2011 at 08:48 AM.

Similar Threads

  1. Favorite Quote
    By EleMentX in forum Spammers Corner
    Replies: 13
    Last Post: 01-04-2020, 04:36 PM
  2. MPGH PM Chat Quote Thread
    By arunforce in forum Entertainment
    Replies: 39
    Last Post: 05-12-2013, 08:24 PM
  3. MPGH AIM Chat Quote Thread
    By ace76543 in forum Entertainment
    Replies: 30
    Last Post: 09-25-2011, 12:14 PM
  4. Recursive way to "shift"
    By dkofek in forum Programming Tutorials
    Replies: 0
    Last Post: 09-25-2011, 11:12 AM
  5. MPGH IRC Chat Quote Thread
    By i eat trees in forum Entertainment
    Replies: 746
    Last Post: 07-02-2011, 10:07 PM