Compute the value of a polynomial

Hi,I am new to c programming.

This program is supposed to find the value of a polynomial

with x being the number of terms

a[x] being the coefficients

b[x] being the exponents in each term

j being the variable

for example if

x=4

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

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

j=2

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

the value will be 23


however the value computed is wrong in the code below

I dont know whats wrong with it

please help ...thanks...



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

{
    int x;
    scanf("%d\n", &x);
    double a[x];
    int l;
    for(l=0; l<x; l++)
    {
        scanf("%lf", &a[l]);

    }
    int b[x];
    int m;
    for(m=0; m<x; m++)
    {
        scanf("%d", &b[m]);

    }
    double j,fj;

    scanf("%lf",&j);
    fj= pow( a[x]*j, b[x]);





    printf("value is %lf\n",fj);

    return 0;
}

pow(a[x]*j, b[x]) = (a[x]×j)b[x] = a[x]b[x]×jb[x]

Thanks a lot ,

I corrected my code but it isn't able to compute anything this time

can anybody tell me what is wrong this time ?

thanks

Last edited on
Thanks a lot ,

I corrected my code but it isn't able to compute anything this time

can anybody tell me what is wrong this time ?

thanks

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

{
    int x;
    scanf("%d", &x);
    double a[x];
    int l;
    for(l=0; l<x; l++)
    {
        scanf("%lf", &a[l]);

    }
    int b[x];
    int m;
    for(m=0; m<x; m++)
    {
        scanf("%d", &b[m]);

    }
    int n;
    double j,c[n],sum;
    scanf("%lf",&j);
    for(n=0; n<x; n++)
    {

        c[n]=a[n]*pow(j,b[n]);
        sum+=c[n];

    }



    printf("value is %lf\n",sum);

    return 0;
}
n has not been initialized when it's used on line 23.
Topic archived. No new replies allowed.