Pascal's Triangle Program Only Accurate to 12 Rows

This is a discussion on Pascal's Triangle Program Only Accurate to 12 Rows within the C Programming forums, part of the General Programming Boards category; Hello, I'm new to programming in C, so I've been working on some of the challenges. I've completed the Pascal's ...

  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
    20,975
    You're probably running into the upper limit of the range of int.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,831
    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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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-26-2008, 11:24 PM
  2. Help with Day Month Year Program..due by 12
    By jgassen15 in forum C Programming
    Replies: 1
    Last Post: 12-06-2007, 10:21 PM
  3. Stupid Logic Problem Need Outside Viewpoint
    By RP319 in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2005, 09:59 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21