Help with my polynomial program..

Discussion in 'C' started by askmewhy25, Jan 24, 2010.

  1. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    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();
     
    Last edited by a moderator: Jan 25, 2010
  2. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    THIS IS THE UPDATED VERSION
    now the problem is the conputation the evaluated value is always wrong..pls help
    Code:
    [COLOR="SeaGreen"]#include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>[/COLOR]
    
    main(){
           double poly[11]={};
           double polysum;
           int w, v, x, y, n, z,QA;
           
           do{
              do{
                 system([COLOR="Red"]"cls"[/COLOR]);
                 printf([COLOR="Red"]"Polynomial Evaluation\n\n"[/COLOR]);
                 printf([COLOR="Red"]"Enter the Order of Polynomial (max. 10): "[/COLOR]);
                 scanf([COLOR="Red"]"%d"[/COLOR],&n);
                 if(n>10||n<1){
                               system([COLOR="Red"]"cls"[/COLOR]);
                               printf([COLOR="Red"]"Invalid input. Please try again!!!\n"[/COLOR]);
                               printf([COLOR="Red"]"Please press the spacebar to continue!!"[/COLOR]);
                               getche();
                               }
                 }while(n>10||n<1);
                 y=n+1;
                 z=n;
                 v=n;
                 while(y!=0){
                             printf([COLOR="Red"]"Please enter the constant for X raised to the %dth: "[/COLOR], z);
                             scanf([COLOR="Red"]"%lf"[/COLOR],&poly[z]);
                             y--;
                             z--;
                             }
                 printf([COLOR="Red"]"Enter the value of X: "[/COLOR]);
                 scanf([COLOR="Red"]"%d"[/COLOR],&x);   
                 for(w=0;w<n+1;w++){
                                    polysum+=poly[w]*pow(x,n);
                                    n--;
                                    }
                 polysum=polysum+poly[0];
                 printf([COLOR="Red"]"The evaluated value of the %dth Order Polynomial  %lf\n"[/COLOR], v, polysum);
                 printf([COLOR="Red"]"Please press the spacebar to continue!!\n"[/COLOR]);
                 getche();
                 do{
                    system([COLOR="Red"]"cls"[/COLOR]);
                    printf([COLOR="Red"]"Do you want to repeat the computation?\n\n"[/COLOR]);
                    printf([COLOR="Red"]"1 - Yes\n"[/COLOR]);
                    printf([COLOR="Red"]"2 - No\n"[/COLOR]);
                    scanf([COLOR="Red"]"%d"[/COLOR], &QA);
                    switch(QA){
                               case 1:
                                    break;
                               case 2:
                                    break;
                               default:
                                       system([COLOR="Red"]"cls"[/COLOR]);
                                       printf([COLOR="Red"]"Invalid input. Please try again!!!\n"[/COLOR]);
                                       printf([COLOR="Red"]"Please press the spacebar to continue!!"[/COLOR]);
                                       getche();
                                       break;   
                               }
                    }while(QA>2||QA<1);
                    polysum=0;
                 }while(QA==1);
    }
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice