2015-03-02 4 views
-3

Любая идея, почему я получаю эту ошибку? Он появляется после прохождения и принятия значений 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; 
} 



} 
+0

Ошибка сегментации - ошибка времени выполнения. Это указывает на неправильное использование указателей (неправильная адресация памяти). Приложите трассировку стека. –

+2

'gcc -Wall -Werror -g main.c && gdb./A.out', введите' r' и нажмите enter. –

ответ

1

Изменить

scanf("%d, &l"); 

в

scanf("%d", &l); 

и сделать то же самое для остальных scanf с. Кроме того, изменение

if (v = 0) 

в

if (v == 0) 

И правильный формат спецификатора для double является %lf. Кроме того, изменение

main() 

в

int main(void) 

и двигаться

return 0; 

в конце main.

+0

И с такой ошибкой, я думаю, что слишком долго смотрел на этот экран и нуждался в некотором сне. Спасибо, сэр. – user4622506

+2

@Downvoter, что-то не так в этом ответе? –

0

В качестве спецификатора формата при использовании типов с плавающей точкой необходимо использовать "%f".

В противном случае поведение программы не определено.

Хотя это не является причиной сбоев во время работы, вы находитесь на опасной почве, сравнивая == с типами с плавающей точкой, которые являются результатом вычисления полиномов. Вы , вероятно, отлично в этом конкретном случае, но будьте осторожны.

Смежные вопросы