c-错误:分段故障(核心转储)



知道我为什么会出现这个错误吗?它是在运行并接受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);

并对其余的scanfs执行相同操作。此外,更改

if (v = 0)

if (v == 0)

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

main()

int main(void)

并移动

return 0;

在CCD_ 4的末尾。

接收浮点类型时,需要使用"%f"作为格式说明符。

否则程序行为是未定义的。

虽然不是任何运行时崩溃的原因,但比较作为多项式计算结果的浮点类型上的==是危险的。在这种特殊情况下,你可能很好,但一定要小心。

相关内容

  • 没有找到相关文章

最新更新