I wonder how to convert a recursive function/class to an iterative one. I have made a recursive Pascal\'s triangle, and now need to compare it to an iterative.
p
Plug these two functions below into a pascal class. I tested it and it works. That link that Prateek Darmwal posted is pretty much the same thing.
public void nonRecursivePrint()
{
nonRecursivePrint(n, true);
}
public void nonRecursivePrint(int n, boolean upsideDown)
{
if (!upsideDown)
{
for (int j=0; j<(n+1); j++)
{
for (int i=0; i<(j); i++)
{
System.out.print(binom(j - 1, i) + (j == i + 1 ? "\n" : " "));
}
}
}
else
{
for (int j=n; j>0; j--)
{
for (int i=0; i<(j); i++)
{
System.out.print(binom(j - 1, i) + (j == i + 1 ? "\n" : " "));
}
}
}
}