Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

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

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

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?

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.

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("\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=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
http://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);

Russ
bobbitts@hotmail.com
http://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

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close