由於某種原因,它曾經工作。但現在我得到一個SIGFPE .....有什麼不對?爲什麼會有SIGFPE?
#include "usefunc.h"
long factorial(long num) {
if (num > 1) {
long counter;
long fact = 1;
for (counter = num; counter > 0; counter--) fact *= counter;
return fact;
}
else return 0;
}
long combinations(long n, long k) {
return (factorial(n))/(factorial(k)*factorial(n-k));
}
int main()
{
printf("How many rows of Pascal\'s triangle should I print?\t");
int rows = GetInteger();
long pArray[rows][rows];
int counter;
int counter2;
for (counter = 1; counter <= rows; counter++)
{
int y = rows-counter;
for (; y > 0; y--) printf(" ");
for (counter2 = 0; counter2 <= counter; counter2++)
{
/*
THIS IS AN OUTPUT
*/
printf("%9.0lu", (long) combinations(counter, counter2));
pArray[counter][counter2] = (long) combinations(counter, counter2);
}
/*
THIS IS AN OUTPUT
*/
printf("\n");
}
return 0;
}
長分長不是浮點異常。而更好的解決辦法可能是在'k' = 0 && k <= n'的組合中添加一個檢查。 – 2011-06-04 04:39:53
謝謝。已解決:) – tekknolagi 2011-06-04 04:39:54
這是正確的 - 將一個整數除零將導致一個'SIGFPE'。有趣的是,將浮點數除以零不會導致'SIGFPE'。 – 2011-06-04 04:41:34