简体   繁体   English

Pascal的Triangle返回无意义的值

[英]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. 我尝试将其切换为unsigned和double,并且代码会合规并且仍然在终端中接受输入,但是在那之后似乎挂断了……什么也没打印,看起来好像陷入了一个循环。

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. 它不是完美的或漂亮的,但是它最多可以输入13个三角形(即第14行)。 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 problem? 关于如何解决此问题的任何想法? 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 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? 还是有一种内置的方式可以告诉打印语句仅出现在一行上?

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. 对于垃圾,您很可能会遇到整数溢出,也就是说,对于long数据类型,您的计算值变得太大。 You should correct it by calculating your factorial function without explicitely calculating n!. 您应该通过计算阶乘函数来纠正它,而不必显式计算n!。

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. 为了回答关于换行符的其他问题,您在打印语句中使用\\n显式打印换行符。 Remove it, and you will get answers printed on one line. 删除它,您将得到打印在一行上的答案。 You probably want to inlucde a final printf("\\n"); 您可能希望包含最后一个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; 您不需要第一个return 0; - the control will drop out of the bottom of the if block and on to the second (should be only) return 0; -控件将从if块的底部退出,并return 0;到第二个(应该是) return 0; and not cause any problems. 并且不会造成任何问题。
  • You're declaring kPtr but not using it anywhere 您在声明kPtr但未在任何地方使用它
  • You don't need to declare a separate variable nPtr to pass to scanf ; 您无需声明单独的变量nPtr即可传递给scanf you can pass &n directly. 您可以直接通过&n

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

scanf(" %ld", nPtr); 

and printf("\\n %i", ans); 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, ie use -Wall . 如果使用的是gcc ,请打开警告,即使用-Wall

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM