array and for loops

This is a discussion on array and for loops within the C Programming forums, part of the General Programming Boards category; The problem is as follows, Write a program to read in an array containing the co-efficients of the polynomial and ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    2

    array and for loops

    The problem is as follows, Write a program to read in an array containing the co-efficients of the polynomial and compute the value of the polynomial for a given value of x.

    e.g :By Horners method 3+4x+5x^2+6x^3+7x^4 can be written as;
    3+(4+(5+(6+7x)x)x)x

    In my understanding this is like;

    a = 6+7x
    b = 5+ ax
    c = 4+ bx
    d= 3+cx
    where 6 = a(n-1), 7 = a(nx), 5 =a(n-2) , 4 =(a n-3) etc


    this is my code so far and i really really am lost as to where to go from here and finish the code.

    this is my code so far and i really really am lost as to where to go from here and finish the code.-----------------------------------------------------------------------------------------------------
    CODE
    ------------------------------------------------------------------------------------------------------


    Code:
    #include<stdio.h>
    #include<math.h>
    #define MAX_SIZE 50
    
    void main (void);
    
    void main(void)
    {
    
    int n,i;
    double coeffs[MAX_SIZE],x,result;
    
    printf("enter order of equation");
    scanf("%d",&n);
    
    printf("enter the value of x/n");
    scanf("%lf",&x);
                                          
                              for(i=n-1;i>=0; i--)
                              {
                                     printf("enter coeffs[%d]:", i+1);
                                     scanf("lf%",&coeffs[i]);
                              }
                                     result =(x*coeffs[i]);
                                     printf("the array elements are %d,coeffs[i]);
    
                                       for(i=n-1;i>=0;i--)
                                       {
                                           n+(n-1)x
                                       }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    1) It's int main(void), and I don't think you need a prototype for that.

    2)
    Code:
    scanf("lf&#37;",&coeffs[i]);
    The %-symbol must come before the control characters.

    3)
    Code:
    printf("the array elements are %d,coeffs[i]);
    Missing a terminating ", and the format character is wrong (%f for doubles).

    4)
    Code:
    n+(n-1)x
    C (and I think programming languages in general) don't support shorthands in mathematical notation. If you mean multiplication, you have to use the * operator at the right place.

    Also it looks like this line would have no side-effects since you don't assign the result to anything (doesn't change anything should you leave it out altogether).

    In general, don't try to write the whole program at once without testing it. Compile (and test what you have this far) more often, even after adding each line.
    Last edited by anon; 10-07-2008 at 05:19 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    2
    yes you are right about my syntax errors, but have you any idea of how to actually the code to solve the problem should look like?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Do you mean that the coefficents should be input by the user and stored in the array, so you'd have an array containing [3, 4, 5, 6, 7] and a variable x, e.g 3.4?

    In pseudocode this shouldn't be all that difficult:

    Code:
    get coefficents from user
    get x from user
    result = 0.0
    power_of_x = 1.0 //x ^ 0
    for each coefficent
         result += coefficent * power_of_x
         power_of_x *= x //next power of x
    output result
    Or you might also use the pow() function from <math.h>.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21