开发者

Pascal's Triangle returning nonsense values

This is a homework project I was assigned some time ago... I've been successful in getting this far on my own, and the only hiccup I have left is (I believe) an issue with data types and overflow.

I've tried changing over to unsigned and double, and the code complies and still accepts input in the terminal, but it seems to hang up after that... nothing is printed and it looks like it's caught in a loop.

Here is the code...

    /* pascaltri.c
     * A program that takes a single integer as input and returns the nth line of
     * Pascal's Triangle. Uses factorial() function to help find items of
     * individual entries on a given row.
     */
    #include <stdio.h>
    #include <stdlib.h>
    long factorial(long i) 
    {
        long fact = 1;

        while(i > 1)
        {
            fact = fact * i;
            i = i - 1;
        }
        return fact;
    }
    main(void)
    {   
        long n;
        long *nPtr;
        nPtr = &n;
        scanf(" %i", nPtr); 
        if (n >= 0)
        {
            long k;
            long *kPtr;
            kPtr = &k;                
            for(k = 0; k <= n; k++)
            {
                long ans;
                long *ansPtr;
                ansPtr = &ans;

                ans = factorial(n) / (factorial(k) * factorial(n - k));
                printf("\n %i", ans);
            }
            return 0;
        }
        return 0;
    }

It's not perfect or pretty, but it works up to an input of 13 (that is, row 14) of the triangle. Beyond that I start getting gibberish and even negative values sprinkled throughout the returns... much larger values break the code and return nothing but an exit error message.

Any ideas on how I can correct this prob开发者_StackOverflow中文版lem? I've been staring at the screen for much to long to really see anything myself. Also, it's not essential, but I would like to print my return values on one line, rather than having them separated by a newline character.

1 5 10 10 5 1

Would the easiest way be to load the values into an array as they are computed, and then print the array? Or is there a built-in way I can tell the print statement to occur on only one line?


You are suffering from integer overflow. You may need to find a different approach to the algorithm to avoid having to calculate the large numbers.

In answer to your other point about the newline, you are explicitly printing the newline with the \n in your print statement. Remove it, and you will get answers printed on one line. You probably want to inlucde a final printf("\n"); at the end so the whole line is terminated in a newline.

Some other observations:

  • You don't need the first return 0; - the control will drop out of the bottom of the if block and on to the second (should be only) return 0; and not cause any problems.
  • You're declaring kPtr but not using it anywhere
  • You don't need to declare a separate variable nPtr to pass to scanf; you can pass &n directly.


For the garbage, you are most likely running into an integer overflow, that is, your calculated values become too large for the long data type. You should correct it by calculating your factorial function without explicitely calculating n!.


Change scanf(" %i", nPtr); to

scanf(" %ld", nPtr); 

and printf("\n %i", ans); to

printf("\n %ld", ans);

to get printout on one line, use:

printf(" %ld", ans);

If you are using gcc, turn on warnings, i.e. use -Wall.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜