gpt4 book ai didi

c - 错误: Segmentation fault (core dumped)

转载 作者:行者123 更新时间:2023-11-30 21:02:41 24 4
gpt4 key购买 nike

知道为什么我会收到此错误吗?它在运行并接受 l 和 r 值后出现。当我编译时,我没有收到任何错误,只是当我运行它时。这只是我的计划的前半部分。但是,我不想让这个错误继续存在。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

main()
{
int y, n, q, v, w;
double a, b, c, d, e, l, r;
printf("Enter lower bound l:");
scanf("%d, &l");
printf("Enter upper bound r:");
scanf("%d, &r");
printf("The coefficients should be entered to match this form: ax^4+bx^3+cx^2+dx+e ");
printf("Enter value of a:");
scanf("%d, &a");
printf("Enter value of b:");
scanf("%d, &b");
printf("Enter value of c:");
scanf("%d, &c");
printf("Enter value of d:");
scanf("%d, &d");
printf("Enter value of e:");
scanf("%d, &e");
//initializing the sign counters to zero
q = 0;
w = 0;
//here we will count the lower bound sign variations with q holding the count
//I'm going to compare each term with the absolute value of it to check the sign
//then I will see if they are the same or equal and increment the count as necessary

if (a == abs(a))
{
y = 1;
}
else
{
y = 0;
}
if ((a*4*l + b) == abs(a*4*l + b))
{
n = 1;
}
else
{
n = 0;
}
//if they are different signs then one is added to the count
if (y != n)
{
q++;
}
if ((6*a*l*l + 3*b*l + c) == abs(6*a*l*l + 3*b*l + c))
{
y = 1;
}
else
{
y = 0;
}
if (y != n)
{
q++;
}
if ((4*a*l*l*l + 3*b*l*l + 2*c*l + d) == abs(4*a*l*l*l + 3*b*l*l + 2*c*l + d))
{
n = 1;
}
else
{
n = 0;
}
if (y != n)
{
q++;
}
if ((a*l*l*l*l + b*l*l*l + c*l*l + d*l + e) == abs(a*l*l*l*l + b*l*l*l + c*l*l + d*l + e))
{
y = 1;
}
else
{
y = 0;
}
if (y != n)
{
q++;
}

//now for the upper bounds sign changes
if (a == abs(a))
{
y = 1;
}
else
{
y = 0;
}
if ((a*4*r + b) == abs(a*4*r + b))
{
n = 1;
}
else
{
n = 0;
}
//if they are different signs then one is added to the count
if (y != n)
{
w++;
}
if ((6*a*r*r + 3*b*r + c) == abs(6*a*r*r + 3*b*r + c))
{
y = 1;
}
else
{
y = 0;
}
if (y != n)
{
w++;
}
if ((4*a*r*r*r + 3*b*r*r + 2*c*r + d) == abs(4*a*r*r*r + 3*b*r*r + 2*c*r + d))
{
n = 1;
}
else
{
n = 0;
}
if (y != n)
{
w++;
}
if ((a*r*r*r*r + b*r*r*r + c*r*r + d*r + e) == abs(a*r*r*r*r + b*r*r*r + c*r*r + d*r + e))
{
y = 1;
}
else
{
y = 0;
}
if (y != n)
{
w++;
}
//now I have the number of sign changes when both bounds are put in
//the lower bound value is held in q
//the upper bound value is held in w
//the absolute value of q-w is the number of roots this will be held in v
v = abs(q-w);

if (v = 0)
{
printf("The polynomial has no roots in the given interval.");
return 0;
}



}

最佳答案

改变

scanf("%d, &l");

scanf("%d", &l);

并对其余的 scanf 执行相同的操作。另外,改变

if (v = 0)

if (v == 0)

double 的正确格式说明符是 %lf。另外,改变

main()

int main(void)

然后移动

return 0;

main末尾。

关于c - 错误: Segmentation fault (core dumped),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28805155/

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