错误:分段错误(核心转储)

时间:2015-03-02 08:12:13

标签: c segmentation-fault

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



}

2 个答案:

答案 0 :(得分:1)

更改

scanf("%d, &l");

scanf("%d", &l);

并对scanf的其余部分执行相同的操作。此外,变化

if (v = 0)

if (v == 0)

double的正确格式说明符为%lf。此外,变化

main()

int main(void)

并移动

return 0;

main的末尾。

答案 1 :(得分:0)

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

否则程序行为未定义。

虽然不是任何运行时崩溃的原因,但是在比较==的浮点类型(多项式计算的结果)时,你处于危险的境地。 在这个特定的例子中你可能很好,但要小心。