Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help with my polynomial program.. (http://www.go4expert.com/forums/help-polynomial-program-t20748/)

Help with my polynomial program..

My program can only evaluate less than 2nd order polynomials but greater than that the evaluation is always wrong due to the term before the constant term is not having any value because of the loop..

example:
the order of polynomial is 3rd
the constants are 1,2,3,4
x=3
1(3)^3+2(3)^2+3(3)^1+4=58
but the program will only print
1(3)^3+2(3)^2+4=49

Code:

```#include <stdio.h> #include <conio.h> #include <math.h> #include <stdlib.h> main(){       double poly[11];       double polysum;       int n, i, x, y, z;             printf("Enter the order of polynomial: ");       scanf("%d", &n);       y=n;       z=n;       for (i = 0; i < y+1; i++){           printf("Please enter the constant for x raised to the %dth: ", y--);           scanf("%lf", &poly[i]);           }       printf("Please enter the constant for x raised to the 0th: ");       scanf("%lf", &poly[10]);       printf("Enter the value of X: ");       scanf("%d", &x);        for (i = 0; i < n+1; i++){           polysum+=poly[i]*pow(x,n);           n--;           }       polysum=polysum+poly[10];       printf("The evaluated value of the %dth Order Polynomial  %lf\n", z, polysum);       getche();```

Re: Help with my polynomial program..

THIS IS THE UPDATED VERSION
now the problem is the conputation the evaluated value is always wrong..pls help
Code:

```#include <stdio.h> #include <conio.h> #include <math.h> #include <stdlib.h> main(){       double poly[11]={};       double polysum;       int w, v, x, y, n, z,QA;             do{           do{             system("cls");             printf("Polynomial Evaluation\n\n");             printf("Enter the Order of Polynomial (max. 10): ");             scanf("%d",&n);             if(n>10||n<1){                           system("cls");                           printf("Invalid input. Please try again!!!\n");                           printf("Please press the spacebar to continue!!");                           getche();                           }             }while(n>10||n<1);             y=n+1;             z=n;             v=n;             while(y!=0){                         printf("Please enter the constant for X raised to the %dth: ", z);                         scanf("%lf",&poly[z]);                         y--;                         z--;                         }             printf("Enter the value of X: ");             scanf("%d",&x);              for(w=0;w<n+1;w++){                                 polysum+=poly[w]*pow(x,n);                                 n--;                                 }             polysum=polysum+poly[0];             printf("The evaluated value of the %dth Order Polynomial  %lf\n", v, polysum);             printf("Please press the spacebar to continue!!\n");             getche();             do{                 system("cls");                 printf("Do you want to repeat the computation?\n\n");                 printf("1 - Yes\n");                 printf("2 - No\n");                 scanf("%d", &QA);                 switch(QA){                           case 1:                                 break;                           case 2:                                 break;                           default:                                   system("cls");                                   printf("Invalid input. Please try again!!!\n");                                   printf("Please press the spacebar to continue!!");                                   getche();                                   break;                            }                 }while(QA>2||QA<1);                 polysum=0;             }while(QA==1); }```

 xpi0t0s 26Jan2010 05:23

Re: Help with my polynomial program..

Try printing the values of variables as it uses them. It's a good debugging technique. Then you can check what is displayed matches what you expect, and if not then you've found a difference between what you thought the program was doing and what it is actually doing.

Since the problem appears to be at the evaluation part, I'd suggest doing something like:
Code:

```for(w=0;w<n+1;w++) { printf("Adding ( poly[w=%d]=%d *pow(x=%d,n=%d)=%d )=%d\n", w, poly[w], x, n, pow(x,n), poly[w]*pow(x,n));     polysum+=poly[w]*pow(x,n);     n--; }```
Obviously %d means int, so if any of those aren't ints then you'll need to change the relevant %d's accordingly.

 All times are GMT +5.5. The time now is 03:41.