1 Replies - 2801 Views - Last Post: 16 July 2014 - 08:13 PM Rate Topic: -----

#1 HiTechRedneck3   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Printing Pattern Using Recursion

Posted 16 July 2014 - 05:51 PM

The program I'm working on is supposed to read input from a file and using recursion, print the pattern of asterisks for each value until the value is either < 0 or > 25.

For example, if the value was 4, the pattern would look like this

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

The values are stored in a file entitled prog3.dat which looks like this

4
3
15
26
0

I've never used recursion before and haven't been able to find anything showing how it would work with this particular type of problem.

Here is what I've been able to come up with so far, but I'm having problems still which I will show following the code.

 import java.util.Scanner; 
import java.io.*;

public class Program3 {
	public static void main(String[] args) throws Exception {
		int num = 0;
		java.io.File file = new java.io.File("../instr/prog3.dat");
		Scanner fin = new Scanner(file);
			
			while(fin.hasNext()) {
			System.out.println("Please enter an integer");
				 num = fin.nextInt();
				//System.out.println(num);
				if(num >=0 && num <= 25) 
					makePattern(num);
			} 
		
		
	}
	
	public static void makePattern(int num) { 
		if(num >= 0 && num <= 25) {
			makePattern(num-1);
			for(int i = 0; i < num; i++) {
				System.out.print("*" + "  ");
			}
			System.out.println("");
		}
	}
} 


I know I"m still not doing things correctly or even if this is actually recursion? Here is the output I get when I run this code:

Please enter an integer

*
* *
* * *
* * * *
Please enter an integer

*
* *
* * *
Please enter an integer

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
Please enter an integer
Please enter an integer

It appears to be reading the file correctly, but is only printing the top half of the pattern. Also, like I said, I'm not very familiar with recursion, so am not sure if this is actually recursion? Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Printing Pattern Using Recursion

#2 HiTechRedneck3   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Printing Pattern Using Recursion

Posted 16 July 2014 - 08:13 PM

I have played around with my code some more and have it in a format that looks more like recursion, but I'm still getting the same result. This is what the makePattern() method currently looks like.

 		if(num > 0) {
			makePattern(num-1);
				for(int i = 0; i < num; i++) 
					System.out.print("*" + "   ");
					System.out.println();
		}
		else if (num < 25) {
			//makePattern(num-1);
				for(int i = 0; i < num; i++) 
					System.out.print("*" + "   ");
					System.out.println();
		}
		else
			makePattern(num-1); 


Please help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1