不寻常的浮点异常(核心转储)与C的错误

时间:2017-01-29 04:55:57

标签: c runtime-error coredump floating-point-exceptions

我目前是一名学生,试图将阶乘法打印出来,因为素数乘以某些指数如此:

5! =(2 ^ 3)(3 ^ 1)(5 ^ 1)

然而,我一直收到一个不寻常的错误,这是在使用scanf检索我的输入后发生的(顺便说一下,我真的很感谢有人向我展示如何使用输入重定向从外部文件中检索多个输入来执行此操作因为这就是我们应该如何检索我们的输入)。

无论如何,我假设这个错误是我的while循环规范中的某个地方。我非常感谢任何帮助/提示/指针。谢谢!

#include <stdio.h> //headers
#include <stdbool.h>

//function prototypes - I will be using functions inside of each other 

int find_prime_count (int prime, int num); 
int find_next_prime (int prime); 
bool is_prime (int num);

int main(void) //main function 
{
    int primeCount[100] = {0}, prime = 2, fact, i = 2, temp = 2, currentPrimeCount, printCount = 0;  

    printf ("Enter number: "); 
    scanf ("%d", &fact);


    while (i <= fact)
    {
        printf ("i is less than factorial");
        while (temp != 1)
        {
            printf ("Temp is not equal to one");
            currentPrimeCount = find_prime_count (prime, temp); 
            printf ("currentPrimeCount calculated");
            temp = temp / (currentPrimeCount * prime);
            printf ("Temp updated");
            primeCount[prime + 1] += currentPrimeCount; 
            printf ("primeCount[prime + 1] updated");
            prime = find_next_prime (prime); 
            printf ("Next prime found");
        }

        i += 1; 
        temp = i; 
    }

    printf ("%3d! =  ", fact); 
    i = 0; 
    while (i < 100)
    {
        if (primeCount[i] != 0)
        {
            if (printCount == 0)
            {
                printf ("(%d^%d)", i, primeCount[i]); 
            }

            else if (printCount != 0)
            {
                printf (" * (%d^%d)", i, primeCount[i]); 
            }

            printCount += 1; 

            if ((printCount % 9) == 0)
            {
                printf ("/n");
            }

            if ((printCount > 9) && ((printCount % 9) == 0))
            {
                printf ("       ");
            }

        }

    }

    return 0;
}



bool is_prime (int num)
{
    bool check = true; //sets check variable to true
    int i = 2; //starts counter variable at 2 (will test all numbers >=2 && <num)

    while (i < num && check == true)
    {
        if ((num % i) == 0) //if it is divisible by any number other than 1 and itself
        {
            check = false; //it is not a prime number and the check becomes false
        }

        i += 1; //increasing counter 
    }

    return check; //returns boolean value
}

int find_next_prime (int prime)
{
    int i = prime; 
    bool check = false; 
    printf ("find_next_prime starts.");

    while (check == false)
    {
        i += 1;
        check = is_prime (i); 
    }

    printf ("find_next_prime ends.");

    return i; 
}

int find_prime_count (int prime, int num)
{
    int count = 0; 
    printf ("find_prime_count starts.");

        while ((prime % num) == 0)
        {
            count += 1; 
            num = num / prime; 
        }

    printf ("find_prime_count ends.");

    return count; 
}

1 个答案:

答案 0 :(得分:2)

使用gdb,我可以在prim % num中判断它是零除错误。

提示:

  1. 使用-g标志
  2. 进行编译
  3. 使用gdb
  4. 运行
  5. 设置断点...