C help

Hi
I am new to c programming.

I dont know whats wrong with my code.

This program is supposed to find the root of a polynomial

with x being the number of terms (x≤100)

a[x] being the coefficients

b[x] being the exponents in each term (must>=0)

j k is any 2 numbers for the interval of the root

provided that j<k and f(j)*f(k)<0

for example if

x=4

a[x]=1,2,1,5

b[x]=3,2,1,0

j=-4 , k=1

then the polynomial is (x*x*x)+2(x*x)+x+5

and the root between interval j and k will be -2.4334

please help me...thanks...

This is my coding.



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
#include <stdio.h>
#include <math.h>
int main()

    {
        int x ;
        x<=100;
        scanf("%d\n", &x);
        double a[x];
        int l;
        for(l=0; l<x; l++)
        {
            scanf("%lf", &a[l]);
        }
        int b[x];
        b[x]>=0;
        int m;
        for(m=0; m<x; m++)
        {
            scanf("%d", &b[m]);
        }
        double j,k,c,fj,fk,fc;
        j<k;
        scanf("%lf %lf",&j,&k);
        fj= pow( a[x]*j, b[x]);
        fk= pow( a[x]*k, b[x]);
        while( fj * fk < 0 )
        {
            c = (j+k)/2;
            fc = pow(a[x]*c, b[x]);

            if(fabs( fc ) < 1e-9)
            {
                break;
            }

            else if  ( fabs( fc )> 0&&fc*fk < 0 )
            {
                j = c;
            }
            else if  ( fabs( fc )> 0&&fj*fc < 0 )
            {
                k = c;
            }


        }

        printf("root is %lf\n",c);

    return 0;
}
Last edited on
Why do you think something's wrong with it? What behaviour are you seeing that seems incorrect to you?

We're not mind-readers.
Sorry, I think there is something wrong with the input of the polynomial

row1 to row 26

resulting in the value of the function being incorrect

The lower part of my code row27 to row50 seems to be all right ,

since I tested it with another code and the roots came out correct .
Last edited on
Where did you pull those numbers from? What causes you to think "something's wrong with rows 1 to 26 but rows 27 to 50 are fine"? You need to give more details about what the problem is.
Thanks for replying

I tested row27 to row 50 with a simpler code

and the roots were correct according to my calculator

root = 1.167304

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
#include <stdio.h>
#include <math.h>

int main()
{
    double a=-0.5, b=1.5, c, fa, fb, fc;

    fa = pow(a, 5.0) - a - 1;
    fb = pow(b, 5.0) - b - 1;

    while( fa * fb < 0 )
    {
        c = (a+b)/2;
        fc = pow(c, 5.0) - c - 1;

        if(fabs( fc ) < 1e-9)
        {
            break;
        }

        else if  ( fabs( fc )> 0&&fc*fb < 0 )
        {
            a = c;
        }
        else if  ( fabs( fc )> 0&&fa*fc < 0 )
        {
            b = c;
        }

    }

    printf("root is %lf\n",c);

    return 0;
}



But with the longer code which the root is supposed to be

-2.4334 for (x*x*x)+2(x*x)+x+5 between -4 and 1

it came out wrong

so I think the problem is with rows 1 to 26

which is supposed to scanf with the first input x

being the number of terms (x≤100)

than input the next x numbers which i defined as a[x] in my code being the coefficients

after that input another x numbers which is exponents in each term

which is defined as b[x] here (must>=0)

as for j and k it is just for defining the interval of the root

provided that j<k and f(j)*f(k)<0













Last edited on
I tried entering random numbers , and all the roots came out the same

so I think somethings wrong with the scanf
Topic archived. No new replies allowed.