need help with c-code for polynomial calculation

This is a discussion on need help with c-code for polynomial calculation within the C Programming forums, part of the General Programming Boards category; aloha community, got a little problem with my c-code ... i need to write a program for polynom calculation. looks ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    6

    need help with c-code for polynomial calculation

    aloha community,

    got a little problem with my c-code ...

    i need to write a program for polynom calculation.

    looks like this:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    main()
    {int grad=0;
    float ergebnis=0;
    float ableitung=0;
    int x=0;
    
    	printf ("\n Dieses Programm berechnet den Funktionswert sowie die erste Ableitung eines beliebigen Polynoms aus\n");
    	printf ("\n Geben sie den Grad des Polynoms ein!\n");
    	scanf ("%d" &grad);
    	int *vielfache = new int grad;
    	for (int i=0; i-1 <grad; i++)
    		{vielfache[i]=0};
    
    	while (int k=0; int i=0 ; k-2 < grad; k++; i++)
    		{printf ("\n Geben sie das \n &k \n . Vielfache des Polynoms ein!\n");
    		scanf ("%d" &vielfache[i]);}
    
    	printf ("\n Geben sie einen Wert für x ein!\n");
    	scanf ("%d" &x);
    
    	int *zwischenwert = new int grad;
    	for (int l=0; l-1 < 2*grad; l++)
    		{zwischenwert[l]=0;}
    		
    	while (int i=0; int l=0; i-1<grad;)
    		{vielfache[i]*x=zwischenwert[l];
    		i++;
    		zwischenwert[l]+vielfache[i]=zwischenwert[l+1];
    		zwischenwert[l+1]=ergebnis;
    		l++;
    		l++;
    		i++;}
    
    	prinft ("\n Der Funktionswert des Polynoms ist\n &ergebnis \n!")
    
    return0;
    
    }
    when i try to compile i gett the following errors:

    Code:
    program.c: In function ‘main’:
    program.c:12: error: invalid operands to binary & (have ‘char *’ and ‘float’)
    program.c:13: error: ‘new’ undeclared (first use in this function)
    program.c:13: error: (Each undeclared identifier is reported only once
    program.c:13: error: for each function it appears in.)
    program.c:13: error: expected ‘,’ or ‘;’ before ‘int’
    program.c:14: error: ‘for’ loop initial declarations are only allowed in C99 mode
    program.c:14: note: use option -std=c99 or -std=gnu99 to compile your code
    program.c:15: error: expected ‘;’ before ‘}’ token
    program.c:17: error: expected expression before ‘int’
    program.c:19: error: invalid operands to binary & (have ‘char *’ and ‘int’)
    program.c:22: error: invalid operands to binary & (have ‘char *’ and ‘int’)
    program.c:24: error: expected ‘,’ or ‘;’ before ‘int’
    program.c:25: error: ‘for’ loop initial declarations are only allowed in C99 mode
    program.c:28: error: expected expression before ‘int’
    program.c:29: error: lvalue required as left operand of assignment
    program.c:31: error: lvalue required as left operand of assignment
    program.c:39: error: expected ‘;’ before ‘return0’
    i can´t figure out, what´s wrong...

    someone able to help me?

    please excuse my bad english.

    greetz!

    dreadkopp
    Last edited by dreadkopp; 12-22-2011 at 03:58 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,618
    You need to be a bit more systematic than just hacking together code and then panicking when the compiler complains. All of the errors are self-explanatory.

    In line 12, don't use %d format to read a floating point value - it is for reading ints. Use %f or %g.

    In line 13, operator new is C++, not C.

    Line 15 you're missing a closing brace.

    I could go on, but you really need to try harder.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    "new int" is just c++?

    ........... howto create infinite variables with c?

    have had no idea howto do this, so i did some google-research and found this code.... thought it was in c...
    Last edited by dreadkopp; 12-22-2011 at 03:53 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,618
    No such thing as an infinite variable in C (some floating point types do support a special value of infinity, but that's not what you mean).

    To dynamically allocate memory in C, use malloc() to allocate and free() to release. Read up on the documentation for them. Both those functions are declared in <stdlib.h>.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    made it small test programm for my infinite variables-issue.

    instead of


    Code:
    int *vielfache = new int grad;
        for (int i=0; i-1 <grad; i++)
            {vielfache[i]=0};
    i now use

    Code:
    #include <stdio.h>
    
    main()
        {int grad=0;
        printf ("\n geben sie den grad des polynoms ein!\n");
        scanf ("%d" &grad);
            int vielfache[grad+1];
    
            for (int i=0; i<grad; i++)
                {printf ("%d", vielfache[i]);
                }
    }
    but still get this c99 error....

    Code:
    test.c:6: error: invalid operands to binary & (have ‘char *’ and ‘int’)
    test.c:9: error: ‘for’ loop initial declarations are only allowed in C99 mode
    test.c:9: note: use option -std=c99 or -std=gnu99 to compile your code
    don´t know why i get an error in line 6... "grad" is int....

  6. #6
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    142
    Quote Originally Posted by dreadkopp View Post
    Code:
    test.c:6: error: invalid operands to binary & (have ‘char *’ and ‘int’)
    Why are you ignoring the advice in message #2 in this thread about line 12 in your original code? This is the line that corresponds to your line 6 in the new code.


    Quote Originally Posted by dreadkopp View Post
    Code:
    test.c:9: error: ‘for’ loop initial declarations are only allowed in C99 mode
    test.c:9: note: use option -std=c99 or -std=gnu99 to compile your code
    If this error is not clear enough for you, I am very surprised. There are two (and maybe now just recently three) versions of the Programming Language C standard (ANSI C89/ISO C90 and ISO C99). Apparently, your compiler compiles using ISO C90 as its default. But you are using a ISO C99 feature. Now is that enough information for you to find your own problem?

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    hope so... thx... now using c99 instead of gcc... seems to work

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    grrrr... thought i got everything correct now..... but it seens like i
    don´t...

    looks like this

    Code:
    #include <stdio.h>
    
    main()
    {int grad=0;
    float ergebnis=0;
    float ableitung=0;
    int x=0;
    int i=0;
    int k=0;
    int l=0;
    
    	printf ("\n Dieses Programm berechnet den Funktionswert sowie die erste Ableitung eines beliebigen Polynoms aus\n");
    	printf ("\n Geben sie den Grad des Polynoms ein!\n");
    	scanf ("%d", &grad);
    	int vielfache[grad+1];
    
    		for (i<grad; i++;)
    			{vielfache[i]=0;}
    									i=0;
    
    	while (k-2 < grad; k++; i++;)
    		{printf ("\n Geben sie das \n &k \n . Vielfache des Polynoms ein!\n");
    		scanf ("%d", &vielfache[i]);}
    									i=0;
    									k=0;
    
    	printf ("\n Geben sie einen Wert für x ein!\n");
    	scanf ("%d", &x);
    
    	int zwischenwert[grad+1];
    
    		for (l<2*grad; l++;)
    			{zwischenwert[l]=0;}
    									l=0;
    			
    	while (i-1<grad;)
    		{zwischenwert[l]=vielfache[i]*x;
    		i++;
    		zwischenwert[l+1]=zwischenwert[l]+vielfache[i];
    		ergebnis=zwischenwert[l+1];
    		l++;
    		l++;
    		i++;}
    
    	printf ("Der Funktionswert des Polynoms ist \n &ergebnis \n!");
    
    									i=0;
    									l=0;
    
    	while (i-1<grad;)
    		{zwischenwert[l]=vielfache[i]*grad*x;
    		i++;
    		zwischenwert[l+1]=zwischenwert[l]+vielfache[i];
    		ergebnis=zwischenwert[l+1];
    		l++;
    		l++;
    		i++;}
    	
    	ableitung=ergebnis/x;
    
    	printf ("Die erste Ableitung des Polynoms ist \n &ableitung \n!");
     
    return 0;
    
    }
    when i try to compile with c99 i get the following error:

    Code:
    program.c: In function ‘main’:
    program.c:21: error: expected ‘)’ before ‘;’ token
    program.c:36: error: expected expression before ‘)’ token
    program.c:50: error: expected expression before ‘)’ token
    i don´t get it.... call me stupid but please help...

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    26
    Read about how to write a main function and while loops.

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    nevermind my last posting.... got another problem....

    program compiles without problems and seems to work correctly... but the uotput is totaly wrong most of the times...

    the problem is, that the program is not using decimal numbers even if i declared them as float...

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {int grad=0;
    double ergebnis=0;
    double ableitung=0;
    float x=0;
    int i=0;
    int k=0;
    int l=0;
    
    	printf ("Dieses Programm berechnet den Funktionswert sowie die erste Ableitung eines beliebigen Polynoms aus\n");
    	printf ("Geben sie den Grad des Polynoms ein!\n");
    	scanf ("%d", &grad);
    	float vielfache[grad+1];
    
    		for (i<grad; i++;)
    			{vielfache[i]=0;}
    									i=0;
    
    	while (k < grad+1 )
    		{printf ("Geben sie das %i . Vielfache des Polynoms ein!\n" ,k+1);
    		scanf ("%f", &vielfache[i]);
    		//printf ("%f %d \n" , vielfache[i] , i);
    		k++;
    		i++;}
    									i=0;
    									k=0;
    
    	printf ("Geben sie einen Wert für x ein!\n");
    	scanf ("%f", &x);
    
    	double zwischenwert[grad+1];
    
    		for (l<2*grad; l++;)
    			{zwischenwert[l]=0;}
    									l=0;
    			
    	while (i<grad+1)
    		{zwischenwert[l]=vielfache[i]*x;
    		i++;
    		zwischenwert[l+1]=zwischenwert[l]+vielfache[i];
    		ergebnis=zwischenwert[l+1];
    		//printf ("%g %g %g \n" , ergebnis , zwischenwert[l], x);
    		l++;
    		l++;
    		i++;}
    
    	printf ("Der Funktionswert des Polynoms ist:  %f \n" ,ergebnis);
    
    									i=0;
    									l=0;
    
    	while (i<grad)
    		{zwischenwert[l]=vielfache[i]*grad*x;
    		i++;
    		zwischenwert[l+1]=zwischenwert[l]+vielfache[i];
    		ergebnis=zwischenwert[l+1];
    		l++;
    		l++;
    		i++;}
    	
    	ableitung=ergebnis/x;
    
    	printf ("Die erste Ableitung des Polynoms ist: %f \n" ,ableitung);
     
    return 0;
    
    }
    can someone explain why this happens and how to fix it? thank u very much

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    26
    You're printing out doubles as floats.

    edit: grumpy corrected error
    Last edited by Amberlampz; 12-22-2011 at 10:11 PM.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,618
    Converting from float to double requires neither typecasting nor compiler warnings. The set of values that a float can represent is a subset of the set of values that a double can represent, so no information can ever be lost converting float to double.

    The reverse is not true - converting double to float can lose information, so good quality compilers (can be configured to) give warnings when a double is implicitly converted to a float.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    26
    Quote Originally Posted by grumpy View Post
    Converting from float to double requires neither typecasting nor compiler warnings. The set of values that a float can represent is a subset of the set of values that a double can represent, so no information can ever be lost converting float to double.

    The reverse is not true - converting double to float can lose information, so good quality compilers (can be configured to) give warnings when a double is implicitly converted to a float.
    Aye, I think I got reverse-confused :P. Thanks for pointing out my error. Still, I would probably avoid having both floats and doubles(just for less confusion's sake) unless there's a convincing reason to use both.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. polynomial evaluation
    By jack_carver in forum C Programming
    Replies: 0
    Last Post: 10-01-2009, 01:08 AM
  2. Polynomial addition
    By treenef in forum C++ Programming
    Replies: 1
    Last Post: 12-17-2005, 10:45 AM
  3. Polynomial class
    By treenef in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2005, 04:01 AM
  4. Polynomial
    By CompiConQuiso in forum C++ Programming
    Replies: 4
    Last Post: 05-16-2005, 01:13 AM
  5. Polynomial
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2002, 06:00 PM

Tags for this Thread


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