gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:18 26 4
gpt4 key购买 nike

我目前是一名学生,正试图将阶乘打印为质数乘以某些指数,如下所示:

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;
}

最佳答案

使用 gdb,我可以看出它是 prim % num 中的被零除错误。

提示:

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

关于c - 不寻常的浮点异常(核心转储)C 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41917737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com