Thread: need help with c-code for polynomial calculation

1. 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()
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");
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?

greetz!

2. 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.

3. "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...

4. 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>.

5. made it small test programm for my infinite variables-issue.

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()
printf ("\n geben sie den grad des polynoms ein!\n");

{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....

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.

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. hope so... thx... now using c99 instead of gcc... seems to work

8. grrrr... thought i got everything correct now..... but it seens like i
don´t...

looks like this

Code:
```#include <stdio.h>

main()
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");

{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);

{zwischenwert[l]=0;}
l=0;

{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;

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```

9. Read about how to write a main function and while loops.

10. 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()
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");

{vielfache[i]=0;}
i=0;

{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);

{zwischenwert[l]=0;}
l=0;

{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;

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. You're printing out doubles as floats.

edit: grumpy corrected error

12. 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.

13. Originally Posted by grumpy
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.