Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Recursion FAIL

Name: Anonymous 2010-02-08 22:29

I'm learning to hate recursion terribly, but I need to do it for this project.
The point of the project is to go from pointA to pointB using recursion in java. And print out all paths.
my input is
5
3 0
1 3

output should be about 10 paths in form of (col,row)
can I get a few pointers and maybe a little help?
private void pathFinder(int row, int col, int xB, int yB, int[][] array)
{
    if(row==xB && col==yB)
    {
        System.out.println("("+row+","+col+") ");
    }
    if(canIGoNorth(row,xB)==true)
    {
        System.out.print("("+row+","+col+") ");           
        pathFinder(row-1,col,xB,yB,array);
        System.out.print("("+row+","+col+") ");
    }
    if(canIGoEast(col,yB)==true)
    {
        System.out.print("("+row+","+col+") ");           
        pathFinder(row,col+1,xB,yB,array);
        System.out.print("("+row+","+col+") ");
           
    }
       
       
    }
inb4 learn to code, everythings wrong, and java sucks.

Name: Anonymous 2010-02-09 9:26

>>14
Programs can't just reserve billions of gigabytes of memory on the off-chance that users will open that many files.
Of course they don't. The idea is that they fail gracefully, operating within their resource limits.

Most naive implementations of the Ackermann function, as you posted, would crash horribly when attempting to compute it for large inputs; either failing to malloc() or just blowing out the stack. Even if it gracefully unwinds in error after a failed malloc(), it has still disturbed the operating system requesting a shitload of pages, and left fragmented memory if the computation is part of a larger program shared for other tasks.

An implementation without recursion or dynamic memory allocation would know its limits beforehand; requesting something outside that range would immediately result in a simple error message leaving the system intact. (The Ackermann function is probably not a good example since the values you can usefully compute are so small, but you get the idea.)

The point is, recursion is *not* a natural product of having functions. Thankfully, the software that guides missiles and lands planes does not use recursion.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List