Thread: Pascal's Triangle Program Only Accurate to 12 Rows

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    FL, United States
    Posts
    4

    Pascal's Triangle Program Only Accurate to 12 Rows

    Hello, I'm new to programming in C, so I've been working on some of the challenges. I've completed the Pascal's Triangle challenge, however my program doesn't give accurate values past the 12th row of the triangle. I'm wondering if this is a logic error on my part, or maybe a memory problem with my factorial function? Any help is greatly appreciated.

    Code:
    #include <stdio.h>
    
    int factorial( int n )
    {
         if ( n <= 1 )
              return 1;
         else
              return n * factorial( n - 1 );
    }
    
    int main()
    {
         int n;
         int r;
         int value = 0;
    
         do {
                   n = 0;
                   r = 0;
                   printf( "Input the row#: " );
                   scanf( "%d", &n );
                   printg( "Input the value's position within the row: " );
                   scanf( "%d", &r );
    
                   if ( r > n )
                        printf( "That is not a valid position within Pascal's Triangle\n" );
         } while ( r > n );
    
         value = factorial( n ) / (factorial( r ) * factorial( n - r ));
    
         printf( "The value at row-%d, position-%d is %d\n", n, r, value );
    
         return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're probably running into the upper limit of the range of int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    FL, United States
    Posts
    4
    Ah, I see. Well I guess that makes sense, I've never programmed in a language that didn't at least have a longint datatype. Is there any work-around for this, other than a more efficient algorithm? Thanks for the insanely speedy reply!

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Well, this language does have a longint datatype.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    FL, United States
    Posts
    4
    I'm an idiot, I just assumed that when most searches initially just showed the basic datatypes, that was it. Thanks again.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It may be the case that the range of int is the same as that of long. If you are compiling with respect to the 1999 edition of the C standard then you could use unsigned long long. However, you would eventually run into an upper limit again. If you find that problematic, consider the use of a bignum library like the GMP.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2010
    Location
    FL, United States
    Posts
    4
    long long int did the trick. Even an unsigned long int was too small. The GMP looks pretty sweet, I'll definitely be checking it out. Thanks once more!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  2. Help with Day Month Year Program..due by 12
    By jgassen15 in forum C Programming
    Replies: 1
    Last Post: 12-06-2007, 11:21 PM
  3. Stupid Logic Problem Need Outside Viewpoint
    By RP319 in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2005, 10:59 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Can't figure out why?
    By kwigibo in forum C Programming
    Replies: 10
    Last Post: 10-14-2001, 10:58 PM

Tags for this Thread