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 3 of 3

Thread: Using recursion to calculate a path through an array

  1. #1
    Junior Member jpetticrew's Avatar
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using recursion to calculate a path through an array

    I've got an assignment that says to "take a number (size of grid - no larger than 10) and a starting location and an ending location and recursively computes all of the NorthEast paths." I'm having trouble wrapping my head around how to do this... Here's what I have so far. (I know it's not much code, I've been working on paper mostly trying to work this out.

    import java.util.*;
    import java.io.*;
     
    public class prog2
    {
    	public static void main(String[] args) throws FileNotFoundException
    	{	
    		File paths = new File("prog2.dat");
    		Scanner input = new Scanner(paths);
     
    		int gridSize = input.nextInt();
    		boolean[][] grid = new boolean[gridSize][gridSize]; //creates 2D array
    	}
     
    	public static int input(boolean[][] grid)
    	{
     
    		return ;
    	}
    }

    My prog2.dat file has exactly this in it:
    5
    3 0
    1 3

    Meaning that the array is to be 5x5, which I've got. And I know that the start/end points are 3,0 and 1,3. The problem I'm having is figuring out how to code that.

    In the end, it's going to output a list of paths to get from start to end.

    I'd like some help with the syntax of what I'm trying to do... I'm also slightly confused by how to pass my array to my other methods.

    Thanks, guys!


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Using recursion to calculate a path through an array

    Define "Northeast paths".

    "take a number (size of grid - no larger than 10) and a starting location and an ending location"

    As a parameter?

    Recursion is used like this:
    public int factorial(int number)
    {
    if (number == 0)
    return 1;
    else 
    return (number * factorial(number -1));
    }

    passing arrays to methods:

    int value = input(grid);

    Just use the name of the array variable to pass.
    Last edited by javapenguin; September 17th, 2011 at 11:15 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using recursion to calculate a path through an array

    jpettigrew: I'm assuming this is for Strader's 202?

    What you need to do is set up a recursive function, say called findNortheastPath().

    Inside the recursive function, you'll need a base case, which would need to basically check to see if the current location (x,y) you are checking is the same as the ending location, if it is return true. If this is not true, the function would need to recursively call the same function twice, but with different starting locations:
    1. For going north (x-1, y)
    2. For going east (x, y+1)

    You can provide output in the recursive function to print out the coordinates as you follow the paths to the ending location.

    I know this is vague, but I hope this helps.

Similar Threads

  1. Cannot calculate. Please help me....
    By safarina02 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 15th, 2011, 02:11 PM
  2. GUI - calculate BMI (need help asap)
    By jahead in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2011, 05:10 AM
  3. Relative path issue with Context path struts
    By chinnu in forum Web Frameworks
    Replies: 1
    Last Post: March 31st, 2011, 11:17 AM
  4. Calculate primes help
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 28th, 2009, 12:41 AM
  5. how to get full path name from realtive path
    By priyanka3006 in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: August 10th, 2009, 05:28 AM