Michael Queue
Michael Queue

Reputation: 1400

Printing text with spacing recursively

This is an assignment for school. I am having trouble understanding how I can print the following recursively:

This was written by call number 2.
 This was written by call number 3.
  This was written by call number 4.
   This ALSO written by call number 4.
  This ALSO written by call number 3.
 This ALSO written by call number 2.
This ALSO written by call number 1.

I'm not sure if I am supposed to be illustrating a loop vs. recursion or if there is a way to print all of this recursively. Additionally, how would I go about reversing the recursion call so it starts from 4 as per the example output?

This is my current output.

This was written by call number 2.
This was written by call number 3.
This was written by call number 4.
This ALSO written by call number 1.
  This ALSO written by call number 2.
   This ALSO written by call number 3.
    This ALSO written by call number 4.

There is no spacing implemented in the for loop yet b/c I'm not sure if that part is also supposed to be recursive.

My code:

public class Recursion {

  public static void main(String[] args) {
    for (int i = 2; i < 5; i++) {
        System.out.println("This was written by call number " + i + ".");
    }
    recurse(4);
  }

  public static void recurse(int n) {
    String temp = "";

    for (int i = 0; i < n; i++) {
        temp += " ";
    }

    if (n < 2) {
        System.out.println("This ALSO written by call number " + n + ".");
    } 
    else {
        recurse(n - 1);
        System.out.println(temp + "This ALSO written by call number " + n + ".");
    }
}

Upvotes: 0

Views: 1823

Answers (5)

Michael Queue
Michael Queue

Reputation: 1400

Thanks to everyone for the help. I ended up modifying the solution from @JoseLuis a little bit.

public class Recursion {

  public static void main(String[] args) {
    recurse(1, 5);
  }

  public static void recurse(int n, int max) {
    String temp = "";
    for (int i = 0; i < n; i++) {
        temp += " ";
    }
    if (n == max) {
        return;
    }
    if (n != 1) {
        System.out.println(temp + "This was written by call number " + n + ".");
    }
    recurse(n + 1, max);
    System.out.println(temp + "This ALSO was written by call number " + n + ".");
  }
}

Upvotes: 0

bestsss
bestsss

Reputation: 12066

Here is quite a straightforward solution. Also pay attention how you can easily get the indent string (via substring). The recursion is as simple as it gets: print the number, enter the function with a larger number if below the max, then follow back.

   class R{
      static final String spaces="                                 ";
      public static void main(String[] args) {
        rec3(1,4);
      }
      private static void rec3(int i, int max) {
        if (i>1) System.out.printf("%sThis was written by call number: %d%n", spaces.substring(0, i-1), i);  
        if (i<max) rec3(i+1, max);
        System.out.printf("%sThis was ALSO written by call number: %d%n", spaces.substring(0, i-1), i);     
      }
  }

Upvotes: 1

SkyMaster
SkyMaster

Reputation: 1323

A simpler solution.

public static void main(String[] args) {
    recurse(1);
}

public static void recurse (int n) {
    if (n==5) return;
    String temp="";
    for (int i=0;i<n;i++) temp += " ";
    if (n!=1) {
     System.out.println(temp + "This was written by call number " + n + ".");
    }
    recurse(n+1);
    temp=" ";
    for (int i=0;i<n;i++) temp += " ";
    System.out.println(temp + "This ALSO was written by call number " + n + ".");
}

Upvotes: 1

Kursad Gulseven
Kursad Gulseven

Reputation: 2008

Try this:

public static void main(String[] args) {
    recurse(1, true, 1);
}

public static void recurse(int n, boolean loop, int add) {
    String temp = "";
    String out = "";

    for (int i = 0; i < n; i++) {
        temp += " ";
    }

    if (add > 0) {
        out = temp + "This was written by call number ";
    } else {
        out = temp + "This ALSO written by call number ";
    }

    if (n == 1 && !loop) {
        System.out.println(out + n + ".");
        return;
    } else if (n == 1) {
        recurse(n+add, false, add);
    } else if (n == 5) {
        add = add - 2 * add;
        recurse(n+add, false, add);
    } else {
        System.out.println(out + n + ".");
        recurse(n+add, false, add);
    }
}

Upvotes: 1

ajb
ajb

Reputation: 31699

The key to writing most recursive programs (especially the ones you're given as assignments) is to look for a larger problem that contains a similar but smaller occurrence of the same problem.

In your case, the "larger problem" would be to print the 6 lines that start and end with "call number 2". That is, print lines for call numbers 2 through 4. The way to do this is: print the first line that says "call number 2", solve the problem to print the 4 lines for call numbers 3 through 4, and print the last line that says "call number 2". The part in the middle is the smaller occurrence of the same problem. That's going to be the recursive call.

Since your larger problem is going to start with "call number 2", and your smaller problem is going to start with the call number that's one higher, I'd recommend arranging things so that you call recurse(n+1) instead of recurse(n-1). If you do that, you'll need a second parameter so that you know when to stop recursing--something like recurse(n+1, last).

Hopefully this will be enough to get you thinking on the right track.

Upvotes: 1

Related Questions