Polynomial number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Budiman
    New Member
    • Nov 2006
    • 21

    Polynomial number

    Hi friends, I need your help in making program in polybomial number.
    The program let us give input:constanta and x
    And the output is: the result
    Let me give the example of the program's output
    "Please input coefficient = 5 4 3 2 1"
    "Please input x value = 3"
    "The result is 547"

    Note: bold number can be changed depends on our input and sorry if my english is bad.

    Thx for all your help
  • rajesh6695
    New Member
    • Oct 2006
    • 96

    #2
    Post the code you had written so far....

    Comment

    • Budiman
      New Member
      • Nov 2006
      • 21

      #3
      Code:
      #include<stdio.h>
      #include<conio.h>
      #include<math.h>
      
      main()
      {
           int x,y,i,j, total;
           int bilangan[100];
           awal:
           clrscr();
           for(y=0;y<=99;y++)
               bilangan[y]=0;
           printf("Insert polynomial number:");
           for(i=0;;i++)
           {
              scanf("%d",&bilangan[i]);
              scanf(" ");
              if(bilangan[i]==13)
                  break;
           }
           printf("\nInsert x value:");
           scanf("%d",&x);
           total=0;
           for(j=0;j<=i;j++)
              total +=bilangan[j]*pow(x, j);
           printf("\nThe result is %d",total);
           getch();
      }
      I hope all of you check the false of this program.
      I don't where's part wrong.
      I hope all your help soon coz the deadline of this assignment is tommorrow morning.
      Last edited by horace1; Feb 6 '07, 11:39 AM. Reason: added code tags

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Let me try to explain the problem as I see it:

        The user will enter some number of integers. These integers represent the coefficients of a polynomial. Thus, if the user enters 5 4 3 2 1, this is to be interpreted as 5x^4 + 4x^3 + 3x^2 + 2x + 1. The user is then asked for an x value, and the polynomial will be evaluated at that x value. Thus, if the user enters 3 for the x value, then the output will be f(3) = 5(3)^4 + 4(3)^3 + 3(3)^2 + 2(3) + 1 = 547.

        Is this correct?

        Comment

        • Budiman
          New Member
          • Nov 2006
          • 21

          #5
          Yes, you correct. but that program still have the mistakes.
          I hope you help me to solve the problem.
          Please tell me where is the wrong part of that program

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            I think your first problem is in reading the polynominal numbers
            Code:
                 for(i=0;;i++)
                 {
                    scanf("%d",&bilangan[i]);
                    scanf(" ");
                    if(bilangan[i]==13)
                        break;
                 }
            i guess the ==13 is testing for the newline character - this will not work as newline is whitespace and skipped by scanf() when using the conversion specification %d. What you can do is terminate the input on a 0, e.g.
            Code:
                 for(i=0;;i++)
                 {
                    scanf("%d",&bilangan[i]);
                  //  scanf(" ");  you don't need this!
                    if(bilangan[i]==0)  // replace 13 with 0
                        break;
                 }
            so you would enter 5 4 3 2 1 0 then the value of x

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              But that would not be desriable, as there may be a polynomial to be evaluated with a coefficient of 0, such as x^2 + 1, which would be entered as 1 0 1.

              Perhaps you could ask the user what degree the function is (the degree equals the highest exponent) and read in that many (plus 1 for the constant value) integers as coefficients.

              Comment

              • Budiman
                New Member
                • Nov 2006
                • 21

                #8
                Ok I'll try it first

                Comment

                • Budiman
                  New Member
                  • Nov 2006
                  • 21

                  #9
                  I think yhe program still don't work well.
                  Pls help me soon

                  Comment

                  Working...