Smart questions
Smart people
 Find A ForumFind An Expert
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Remember Me

Are you a
Computer / IT professional?
Join Tek-Tips now!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Just copy and paste the

#### Feedback

"...with companys cutting back on training, lack of true support by makers of software, the forums are a great tool in your cyber-toolbox...."

#### Geography

Where in the world do Tek-Tips members come from?

# What does stack fault error mean?

 Forum Search FAQs Links Jobs Whitepapers MVPs
 RachelD (MIS) 22 Feb 01 15:21
 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.TIARachel#include #include int C (int n, int r){       if (n <= 1) return 1;     else return C(n, n-r);}int main (){   int  n, r;   do   {      printf("\n\nEnter n and r, or 0 to exit > ");      scanf("%d", &n);      if (n == 0) continue;      scanf("%d", &r);      printf("The number of combinations of %d items taken %d at a time is %d", n, r, C(n, r) );   } while (n != 0);   return EXIT_SUCCESS;}
 aphrodita (Programmer) 22 Feb 01 16:38
 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}http://www.hellforge.org
 RachelD (MIS) 22 Feb 01 18:31
 Thanks for the response, but your correction didn't work.  I got the same stack fault message.  could the refursion be causing it?
 aphrodita (Programmer) 22 Feb 01 18:39
 Yes, recursion can do this if your value of r is big. If it is big, my correction would not help either. Best Regards,aphrodita@mail.krovatka.ru     {uiuc rules}http://www.hellforge.org
 rbobbitt (Programmer) 22 Feb 01 19:16
 I don't know what the algorithm *should* be, but your function C will never end (eventually causing a "stack fault") given certain types of inputs. For example, if you use n=100 and r=14, the function continually does:n=100,r=14n=100,r=86n=100,r=14n=100,r=86Do 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 */} Russbobbitts@hotmail.comhttp://home.earthlink.net/~bobbitts
 rbobbitt (Programmer) 22 Feb 01 19:26
 Also, not related to your stack fault problem:do{     /* ... */     if (n == 0) break; /* instead of continue */     /* ... */} while (n != 0); Russbobbitts@hotmail.comhttp://home.earthlink.net/~bobbitts
 RachelD (MIS) 23 Feb 01 8:29
 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 "stop" 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

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!