Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What does stack fault error mean?

Status
Not open for further replies.

RachelD

MIS
Jun 14, 2000
136
US
This is a low-level question, but I'm just learning...

I am attempting to write a program (lotto.c) in C that essentially calculates chances of winning the lottery. It must include recursion, so that's what I've done. program code is below - the function simply runs the formula that comes up with the numbers in Pascal's triangle (if you've ever taken discrete math this might ring some bells).

The problem is that when I put numbers in at the prompt (say n= 4 and r = 2), I get the following error: LOTTO caused a stack fault in module LOTTO.EXE at 0187:00401150.

Compiler is the free Borland C Builder.
If answering this requires the identities for Pascal's triangle I can give them, but suffice it to say that when n or r is less than 1, the return should be 1.

not trying to get someone to write the code for me, just trying to figure out what I'm doing wrong and maybe get pointed in the right direction.

TIA
Rachel

#include <stdio.h>
#include <stdlib.h>

int C (int n, int r)
{
if (n <= 1) return 1;
else return C(n, n-r);
}

int main ()
{
int n, r;

do
{
printf(&quot;\n\nEnter n and r, or 0 to exit > &quot;);
scanf(&quot;%d&quot;, &n);
if (n == 0) continue;
scanf(&quot;%d&quot;, &r);
printf(&quot;The number of combinations of %d items taken %d at a time is %d&quot;, n, r, C(n, r) );
} while (n != 0);

return EXIT_SUCCESS;
}

 
Your function C does not stop calling itself, that is why you have Stack Overflow.
I do not know what you're trying to do, but if you change it to this:

int C (int n, int r)
{
if (r <= 1) return 1;
else return C(n, n-r);
}

it will stop Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
Thanks for the response, but your correction didn't work. I got the same stack fault message. could the refursion be causing it?
 
I don't know what the algorithm *should* be, but your function C will never end (eventually causing a &quot;stack fault&quot;) given certain types of inputs. For example, if you use n=100 and r=14, the function continually does:

n=100,r=14
n=100,r=86
n=100,r=14
n=100,r=86

Do you, perhaps, mean:

int C (int n, int r)
{
if (r <= 1) return 1; /* r<=1 instead of n<=1 */
else return C(n, r-r); /* r-r instead of n-r */
}





Russ
bobbitts@hotmail.com
 
Thanks for all the posts. I figured it out last night. As a couple people wrote, my function C would never end because my values never decreased. I needed to use a different formula, and I needed to specify additional &quot;stop&quot; values for n & r. (when n and r are equal, C = 1,

However, at least along the way I've learned to solve other types of compile errors...and really really learned recursion.

cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top