recursive function, might have inf loop
I have to use a recursive function to print a ruler to the screen. it looks knida like this:
Code:
| | |
| | | | |
| | | | | | | | |
|||||||||||||||||
execept it will be about 65 characters long, and 7 lines deep.
here is my code:
PHP Code:
// ruler.cpp
// ---------------------
// uses a recursive function to print a ruler to the screen
#include <iostream.h>
void subdivide( int lo, int hi, int level, char line[])
{
if(level == '0')
return;
// get and mark midpoint
int mid = (lo + hi) / 2;
line[mid] = '|';
// call again for left side
subdivide(lo, mid-1, level-1, line);
// call for right side
subdivide(mid+1, hi, level-1, line);
return;
}
int main( void )
{
char line[66];
int i;
// prepair the array
for(i=0; i<66; i++)
{
line[i] = ' ';
}
line[0] = '|';
line[64] = '|';
cout << line << "\n\n\n";
for(i=0; i<7; i++)
{
subdivide(0, 65, i, line);
cout << line << endl;
}
return 0;
}
I dont get any errors when I compile it in VC++ 6, but when i run the exe it dies saying there is an error. when i try to use the debug thing, it says stack overflow. It all makes sense in my head, so i assume i have an infinate loop somewhere that i cant see because im a new programmer.
Can anyone help?