Segmentation Fault (Core Dumped) Error

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <math.h>
#include <stdlib.h>


double evaluate(double f(double), double a, double b, double error) {
        double mid;
        double f_mid = f(mid);
        double f_a = f(a);
        double f_b = f(b);
        

        while(fabs(a- b) > error) {
                mid = (a+b)/2;
                
                if(f_mid == 0)
        {
                break;
                }
        else if((f_mid * f_a) < 0.0)
        {
                b = mid;
                }
                else
                {
                a = mid;
        }
    }

        return mid;
}

double func1(double x) {
        return 3*x*x*x - 3*x*x + 0.2;
}

double func2(double x) {
        return x*x*x*x - 3*x*x - 8;
}

double func3(double x) {
        return 2*sin(2*x) - 3*cos(3*x);
}

int main(void)

{
int o, p;

double a, b, error, root, root_value;



        printf("Which function do you want to choose (1,2,3): ");
error1:
        scanf("%d", &o);

switch ( o ) {

case 1:

     printf("Which interval do you want to choose (1,2): ");
error2:
     scanf("%d", &p);
     printf("What error is accepted (epsilon): ");
     scanf("%f", &error);

        switch ( p ) {
        
        case 1:
                a = -1.0;
                b = 0.0;

        root = evaluate(func1, a, b, error);
                
                root_value = func1(root);

                printf("The root of the function 1 in the interval 1 is %g.\nThe function value at the root is %f\n", root, root_value);
                break;

        case 2: 
                a = 0.0;
        b = 0.5;

                root = evaluate(func1, a, b, error);
              
        root_value = func1(root);

                printf("The root of the function 1 in the interval 2 is %g.\nThe function value at the root is %f\n", root, root_value);
                break;

        default:
               printf("ERROR: Enter 1 or 2 for your interval: ");
                goto error2;
                break;
        }

     break;

case 2:
 
     printf("Which interval do you want to choose (1,2): ");
error3:
     scanf("%d", &p);
     printf("What error is accepted (epsilon): ");
     scanf("%f", &error);

        switch ( p ) {

        case 1:
                a = -4.0;
                b = -2.0;

                root = evaluate(func2, a, b, error);
                root_value = func1(root);

                printf("The root of the function 2 in the interval 1 is %g.\nThe function value at the root is %f\n", root, root_value);
                break;

        case 2:
                a = 2.0;
                b = 4.0;

                root = evaluate(func2, a, b, error);
                root_value = func2(root);

                printf("The root of the function 2 in the interval 2 is %g.\nThe function value at the root is %f\n", root, root_value);
                break;
        default:
                printf("ERROR: Enter 1 or 2 for your interval: ");
                goto error3;
                break;
        }
        break;

case 3:

     printf("Which interval do you want to choose (1,2): ");
error4:
     scanf("%d", &p);
     printf("What error is accepted (epsilon): ");
     scanf("%f", &error);

        switch ( p ) {

        case 1:
                a = 0.0;
                b = 1.0;

                root = evaluate(func3, a, b, error);
                root_value = func3(root);

                printf("The root of the function 3 in the interval 1 is %g.\nThe function value at the root is %f\n", root, root_value);
                break;

        case 2:
                a = 1.0;
                b = 2.0;

                root = evaluate(func3, a, b, error);
                root_value = func3(root);

                printf("The root of the function 3 in the interval 2 is %f.\nThe function value at the root is %f\n", root, root_value);
                break;
        default:
                printf("ERROR: Enter 1 or 2 for your interval: ");
                goto error4;
                break;
        }
    
     break;

default:
     printf("Enter a number between 1 and 3: ");
     goto error1;
     break;
}

return 0;
}


Ok, I've really gone and cleaned it up. This function is intended to find the roots at a certain interval. It now ouputs an accurate point of the graph, just not a root. I need to have it find the root and root y-value
1
2
3
double evaluate(double f(double), double a, double b, double error){
  double mid;
  double f_mid = f(mid);
Like Pax already pointed, mid has garbage, so f_mid will too. And inside the while, you never change the value of f_mid.
closed account (D80DSL3A)
I think you're syntax for the function pointer is wrong in evaluate() on line 6.

First argument in parameter list:
double evaluate(double f(double), double a, double b, double error)

should be:
double evaluate(double (*f) (double), double a, double b, double error)
@ ne555: Yes, of course f_mid isn't going to have different values. That is because this program is about finding roots (y=0 at all roots). What "garbage" are you talking about?
In c++, when the variable is declared, it is not automatically initialized. (you can't be sure about what value the variable holds)

if(f_mid == 0) That's your break condition (you found a root). So you need to evaluate the function in every step.
Also, you are using the bisection method, so you need to evaluate the function to decide with what interval you stay.
Last edited on
So, does this mean the only thing wrong in my code is in my "evaluate" function, not the switch?

I evaluate the function in every case in main, but what do you mean in every step?

Last question: Why will the program work perfectly for function 1, but result in no output for functions 2 and 3?
When I choose function 2, and input my error as just 1 (as opposed to 1e-6), it works. However if there is an e in the error, it fails to produce output.

So, does this mean the only thing wrong in my code is in my "evaluate" function, not the switch?
[/code]

No.

[quote]When I choose function 2, and input my error as just 1 (as opposed to 1e-6), it works. However if there is an e in the error, it fails to produce output.


You don't seem to be doing anything to parse the number from scientific notation. I'm not sure if scanf can do this automatically, but I'm assuming it can't.
scanf
cplusplus wrote:
Floating point: Decimal number containing a decimal point, optionally preceded by a + or - sign and optionally folowed by the e or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4
You need to use %lf to read doubles.

line 115 should be root_value = func2(root);

I evaluate the function in every case in main, but what do you mean in every step?
I mean inside the while(fabs(a- b) > error), f_mid should refresh its value with the result of the function in the midpoint
Last edited on
You guys were all wrong. I just had to change it to switch ( x ). Thanks anyways.
Topic archived. No new replies allowed.
Pages: 12