Welcome to Dream.In.Code
Become a Java Expert!

Join 149,740 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,359 people online right now. Registration is fast and FREE... Join Now!




recursion...need a solution

 
Reply to this topicStart new topic

recursion...need a solution, dealing with string or char i think

wille2511
9 Feb, 2007 - 08:42 AM
Post #1

New D.I.C Head
*

Joined: 24 Oct, 2006
Posts: 46


My Contributions
In this example of recursion, how would this help me to make a program thats outputs this:

*****
****
***
**
*
*
**
***
****
*****

yes i know its easy with println() and for method but my teacher told me this way. just give any suggested method to do this plz ....though im not sure if i still use arrays for it, if so do i declare string or char.

thanks
CODE
import java.io.*;
public class Recursion
{
    public static void main(String[] args)
  {
    int[] intArray = {23,43,35,38,67,12,76,10,34,8};
    System.out.println("The largest element in intArray: " +largest(intArray, 0, intArray.length - 1));

  }

  public static int largest(int[] list, int lowerIndex, int upperIndex)
  {
    int max;

    if(lowerIndex == upperIndex)
    {
    return list[lowerIndex];
    }

     else
     {
     max = largest(list, lowerIndex + 1, upperIndex);

        if(list[lowerIndex] >= max)
        {
        return list[lowerIndex];
        }
          else
          {
          return max;
          }
     }
  }
}

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Recursion...need A Solution
9 Feb, 2007 - 12:21 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
i'm a little confused, there is a question and there is code, but they do not go together. It appears to be the code for a largest int?

To output the design above you do not need an array at all.
the method will presumably print down to 1 star, and have a max size, so your method declaration would be something like:
public void stars(int max, int current, int n)
then what you want to do is use a print, instead of println and print 1 * then recursively call stars(max,current+1,n) with your (partial) base case being max == current.

You will also want to use n, this being a copy of max, thus your true final base case being that of max = max+1;

This isn't that complicated, you just want to count max down, then if it's 1, count it back up, once you have reached max again, your done. Perhaps writting the for loop method would give you some starter ideas.
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Recursion...need A Solution
15 Feb, 2007 - 11:37 AM
Post #3

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
Let's take a buz through some code, see what you think tongue.gif

CODE

public class RAST {
  private int r; // needed for incPrint

  private void printStars(int i) {
    if (i < 1) {
      System.out.print("\n");  
      return;
    } // Base case (i <=0)
    System.out.print("*"); i--;
    printStars(i); // Case (i > 0)
  }

  private int decPrint(int i) {
    if (i < 1) return 1; // Base case ( i <= 0) Note: use 0 to generate a space
    printStars(i);
    i--;
    return decPrint(i);
  }  

  private int incPrint(int i) {
    if (i > r) return r; // Base case ( i <= 0)
    printStars(i);
    i++;
    return incPrint(i);
  }  
  
  private void cycle(int n, int i, int j) {
    if (j > n) return; // Base case ( j > n)
    i = decPrint(i);
    i = incPrint(i);    
    j++;
    cycle(n, i, j);
  }
  
  public RAST(int in) { r = in;}
  
  // command line type in >        java RAST 5 4
  public static void main(String[] args) {
    int i = Integer.parseInt(args[0]);
    int n = Integer.parseInt(args[1]);

    RAST rast = new RAST(i);
    rast.cycle(n, i, 0);

    /*
    for (int j = 0; j < n; j++) {
      i = rast.decPrint(i);
      i = rast.incPrint(i);
    } */
  }
}

User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Recursion...need A Solution
15 Feb, 2007 - 12:10 PM
Post #4

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
I think the incPrint() comment needs to be changed to:
// Base case (i > r)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 05:29AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month