Thread: Reading a polynomial...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    24

    Reading a polynomial...

    Help please! I'm writing the main part of a program that utilizes several functions (in various files); I think I've got the functions (like poly.c that's mentioned here) but I'm confused about this.

    I don't know if my question makes any sense...

    I am having trouble with the part about inputting the coefficients.
    I use "printf" to ask the user to input the coefficients; but how many coefficients am I asking for? Also, do I use scanf to detect the coefficients? How can I use scanf if I don't know the number of coefficients that have to be inputted?)


    Write a program that:

    4.a) Reads a polynomial (p) from the keyboard using scanf. You should
    prompt the user to enter the degree of the polynomial (d). If d < 0
    you should exit the program. If d >= 0 prompt the user to input all
    coefficients from 0 to d. Input here is from the terminal keyboard,
    not a file!

    4.b) Exit with an error message if the coefficient of the monomial
    with the highest degree is zero.

    4.c) Using the functions you wrote in poly.c:

    4.c.i) Compute the first (p') and second (p'') derivatives of p;

    4.c.ii) Print the polynomials p, p' and p'';

    4.c.iii) Compute a zero (y) of p using Newton's method starting at x = 1;

    4.c.iv) If a zero of p has been found, print the zero (y) and the value
    of p(y);
    If a zero has not been found issue a message;
    HINT: Use the function isnan() to check for a NAN!

    4.c.v) Compute a zero (z) of p' using Newton's method starting
    at x = 1;

    4.c.vi) If a zero of p' has been found, print the zero (z) and the
    value of p'(z); If a zero has not been found issue a message;
    HINT: Use the function isnan() to check for a NAN!

    4.c.vii) Evaluate p''(z) and inform the user if z is a point of
    maximum, minimum or inflection. As we are computing
    approximate zeros, you should compare p''(z) not with zero
    but a small number, say 1E-4.

    4.c.viii) Destroy all polynomials you created during the execution of
    your program and print the value of poly_memory() to ensure
    it is zero.

    Thank you!

    BY THE WAY, here's what I have (it's pretty basic b/c I'm not sure what the prompt is asking me to do):

    Code:
    #include <stdio.h>
    
    main()
    {
    	int p, p', p'', d;
    	
    	printf("Enter degree (d) of polynomial:\n");
    	scanf("%d", &d);
    	if (d>=0)
    	{
    		printf("Input all coefficients from 0 to d:\n");
    		int i;
    		for (i = 0; i < d; i++)
    		{
    			scanf(
    		}
    
    		/* code */
    	}else
    	{
    		if (d<0)
    {
    	printf("ERROR! d<0\n");
    	return 0;
    }
    	}
    Last edited by sam...; 12-03-2010 at 06:32 PM.

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    You can use dynamic array to get data from scanf:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
      int d;
      double *p;  
      printf("Enter degree (d) of polynomial:\n");
      scanf("%d", &d);
      if (d>=0)
        {
          p = malloc((d+1)*sizeof(double));
          printf("Input all coefficients from 0 to d:\n");
          int i;
          for (i = 0; i < d; i++)
    	{
    	  scanf("%lf",&p[i]);
    	}
          
          /* code */
          
        }
      free(p);
      return 0;
    }
    Last edited by nimitzhunter; 12-03-2010 at 06:35 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    Thank you so much!
    I think this should work.

    What is free(p)?
    I've never used it before..

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by sam... View Post
    Thank you so much!
    I think this should work.

    What is free(p)?
    I've never used it before..


    p is a dynamic array. You allocate memory with "malloc"
    Code:
    p = malloc((d+1)*sizeof(double));
    After you finish using "p", you use "free" to free up the allocated memory.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quick note, you will probably need <= d, not < d, since you malloc'ed d+1 elements (having indexes from 0..d).
    Code:
    for (i = 0; i <= d; i++)

  6. #6
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    By the way, don't name your variable: p', p''. it's not valid name for C.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    Thank you!! That makes this so much more clear!

    I was wondering: what is the command to open a library file?
    Our instructor provided us with access to one (for the functions) but i don't know how to access it.
    Last edited by sam...; 12-03-2010 at 07:47 PM.

  8. #8
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by sam... View Post
    Thank you!! That makes this so much more clear!

    I was wondering: what is the command to open a library file?
    Our instructor provided us with access to one (for the functions) but i don't know how to access it.
    What library and functions are you talking about?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    It's a file (libpoly.a) that was provided by him that I was instructed to copy into my own directory.
    Here are his statements regarding this:

    libpoly.a is a library file that contains compiled versions of all
    functions you will develop in poly.c already compiled and bundled
    in a library file by your instructor. You can use it to help you
    develop your code.
    To compile my program (final.c) with libpoly.a, I'm supposed to use
    gcc -Wall final.c -L. -lpoly

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Btw, the code nimitzhunter posted has a bug if d < 0.
    Your instructor already told you what's the command to link with his library libpoly.a
    Maybe there should also be a header file associated with the library. say poly.h?

  11. #11
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by Bayint Naung View Post
    Btw, the code nimitzhunter posted has a bug if d < 0.
    Your instructor already told you what's the command to link with his library libpoly.a
    Maybe there should also be a header file associated with the library. say poly.h?
    That's part of his hw, he should be able to come up with the code for that.
    Sam, if there is the header file you could use "#include" to incorporate it with your code:
    Code:
    #include "poly.h"


    BY THE WAY, here's what I have (it's pretty basic b/c I'm not sure what the prompt is asking me to do)
    The purpose of this code is just to find the relative max/min values of the polynomial you input. So, p is a poly that you have. Solving for y such that p'(y)=0 gives you the location of either a min or a max(you don't know which). Then you use p'' to determine whether y is min or max ( check wikipedia for the condition).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Resetting a ifstream object after reading the whole file
    By Zeeshan in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2008, 08:03 AM
  3. reading command line or text file...expression tree (sort of)
    By mathwork orange in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2008, 09:52 AM
  4. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  5. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 10:36 PM