Hello. I know I'm new, but could really use some help. I am almost done with my code, but my problem persists that I can not figure out how to terminate a recursion once the conditions are met. Any help would be great.
Yeah, I'm not the brightest when it comes to coding. I just know it is supposed to scan a file to find "blobs" of characters and count how many blobs there are. It runs all fine and dandy until it wants to count them. In the sample text file, there are 6 blobs, and it outputs in my program -8125678 (some random negative number). That's the part I'm stuck with.
@M031291 : I'm 100% self-taught,and I've already supported people.Going to class isn't the only way to be a programmer. :)
@OP : If you've put recursion and it doesn't terminate there'll be a stack overflow and terminates the problem (sometimes worse) but it didn't have any recursion so I couldn't figure out your problem.
my program is pretty much identical to yours, I'm still having trouble incorporating recursion as well...I'm sure that would resolve the hideous negative number result...
@hentaiw : Of course, the most effective way to learn is through trial and error on your own time. Wish I was as brilliant as you though! Lol. I'm having trouble and going to class helps me. I'm pretty sure the OP has the same assignment from the same professor as me so I asked.
I would eschew from using the term recursion where it does not apply.
Let me try to make the matter clear to you, this is recursion:
1 2 3 4 5 6 7
int factorial(int x)
{
if (x > 0)
return factorial(x-1) * x;
elsereturn 1;
}
This is the normal way of doing it:
1 2 3 4 5 6 7
int factorial(int x)
{
int ans = 1;
for (int i = 1; i <= x; ++i)
ans *= i;
return ans;
}
The term recursion does not apply to arrays, it applies to a function which calls itself, to form a loop.
Most loops and recursions are interchangeable.