Error: Segmentation fault (core dumped)
Error: Segmentation fault (core dumped)
知道我为什么会收到此错误吗?它在 运行 宁通过并接受 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
结束时。
接收浮点类型时需要使用"%f"
作为格式说明符。
否则程序行为未定义。
虽然不是任何运行时崩溃的原因,但您在比较 ==
作为多项式计算结果的浮点类型上处于危险的境地。在这种情况下,您可能 没问题,但请务必小心。
知道我为什么会收到此错误吗?它在 运行 宁通过并接受 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
结束时。
接收浮点类型时需要使用"%f"
作为格式说明符。
否则程序行为未定义。
虽然不是任何运行时崩溃的原因,但您在比较 ==
作为多项式计算结果的浮点类型上处于危险的境地。在这种情况下,您可能 没问题,但请务必小心。