15,795,581 members
1.00/5 (1 vote)
See more:
I have made few changes on my program but still not able to correct these errors
```/*emphasized text*following is my code to find derivates of a 3 degree polynomial function
But many error are generated

#include <stdio.h>
#include <conio.h>
#include <math.h>

float poly(float a[], int, float);
float deriv(float a[], int, float);
float deriv2(float a[], int, float);
float deriv3(float a[], int, float);

int main()
{
float x, a[10], y1, dy1,dy2,dy3;
int deg, i,deg2,deg3;

printf("Enter the degree of polynomial equation: ");
scanf("%d", °);

printf("Ehter the value of x for which the equation is to be evaluated: ");
scanf("%f", &x);

for (i = 0; i <= deg; i++) {
printf("Enter the coefficient of x to the power %d: ", i);
scanf("%f", &a[i]);
}

y1 = poly(a, deg, x);
dy1 = deriv(a, deg, x);
dy2 = deriv2(a, deg2, x);
dy3 = deriv3(a, deg3, x);

printf("The value of polynomial equation for the value of x = %.2f is: %.2f", x, y1);
printf("\nThe value of the derivative of the polynomial equation at x = %.2f is: %.2f", x, dy1);
printf("\nThe value of the derivative of the polynomial equation at x = %.2f is: %.2f", x, dy2);
printf("\nThe value of the derivative of the polynomial equation at x = %.2f is: %.2f", x, dy3);

return 0;
}

/* function for finding the value of polynomial at some value of x */

float poly(float a[], int deg, float x)
{
float p;
int i;

p = a[deg];

for (i = deg; i >= 1; i--) {
p = (a[i - 1] + x * p);
}

return p;
}

/* function for finding the derivative at some value of x */
float deriv(float a[], int deg, float x)
{
float d[10], pd = 0, ps;
int i;

for (i = 0; i <= deg; i++) {
ps = pow(x, deg - (i + 1));
d[i] = (deg - i) * a[deg - i] * ps;
pd = pd + d[i];
}

return pd;
}
float deriv2(float b[],int degg,float x)
{float d[10], pd=0, ps;
int j,degg,deg;
degg=deg-1;
for (j=degg;j>=0;j--)
{
if (b[j]>0)
c[j]=a[j]*b[j];
b[j]=b[j]-1;
else
break;
}

float deriv3(float c[],int deggg,float x)
{float d[10], pd=0, ps;
int k,deggg;
for (k=deg;k>=0;k--)
{
if (b[k]>0)
q[k]=c[k]*b[k];
b[k]=b[k]-1;
else
break;
}
deggg=degg-1;
}

````

What I have tried:

I have tried the program above
Posted
Updated 22-Jul-22 23:53pm
Patrice T 23-Jul-22 0:21am
And you plan to tell the error messages and positions ?

## Solution 1

Take the first error for example:
```main.c: In function ‘deriv2’:
main.c:71:7: error: ‘degg’ redeclared as different kind of symbol
71 | int j,degg,deg;
|       ^~~~
main.c:69:28: note: previous definition of ‘degg’ was here
69 | float deriv2(float b[],int degg,float x)
|           ```

It tells you the file: `main.c`
It tells you the line: `71`
It tells you the column: `7`
It points at the offending text: `degg`
It tells you what the problem is: `‘degg’ redeclared as different kind of symbol`
It gives you extra information: `previous definition of ‘degg’ was here`
And it shows you what the previous declaration was: `float deriv2(float b[],int degg,float x)`
Look at the code at that line, and you'll see that `degg` is declared as a parameter, and then you try to declare it again as a local variable - which means that the parameter would never be accessible.

Solution: remove the local variable of the same name.
Fix that, and recompile your code. Then look at the first error message this time.

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

v2

## Solution 2

Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C
```/*emphasized text*following is my code to find derivates of a 3 degree polynomial function
But many error are generated

#include <stdio.h>
#include <conio.h>
#include <math.h>

float poly(float a[], int, float);
float deriv(float a[], int, float);
float deriv2(float a[], int, float);
float deriv3(float a[], int, float);

int main()
{
float x, a[10], y1, dy1, dy2, dy3;
int deg, i, deg2, deg3;

printf("Enter the degree of polynomial equation: ");
scanf("%d", °);

printf("Ehter the value of x for which the equation is to be evaluated: ");
scanf("%f", & x);

for (i = 0; i <= deg; i++) {
printf("Enter the coefficient of x to the power %d: ", i);
scanf("%f", & a[i]);
}

y1 = poly(a, deg, x);
dy1 = deriv(a, deg, x);
dy2 = deriv2(a, deg2, x);
dy3 = deriv3(a, deg3, x);

printf("The value of polynomial equation for the value of x = %.2f is: %.2f", x, y1);
printf("\nThe value of the derivative of the polynomial equation at x = %.2f is: %.2f", x, dy1);
printf("\nThe value of the derivative of the polynomial equation at x = %.2f is: %.2f", x, dy2);
printf("\nThe value of the derivative of the polynomial equation at x = %.2f is: %.2f", x, dy3);

return 0;
}

/* function for finding the value of polynomial at some value of x */
float poly(float a[], int deg, float x)
{
float p;
int i;

p = a[deg];

for (i = deg; i >= 1; i--) {
p = (a[i - 1] + x * p);
}

return p;
}

/* function for finding the derivative at some value of x */
float deriv(float a[], int deg, float x)
{
float d[10], pd = 0, ps;
int i;

for (i = 0; i <= deg; i++) {
ps = pow(x, deg - (i + 1));
d[i] = (deg - i) * a[deg - i] * ps;
pd = pd + d[i];
}

return pd;
}

float deriv2(float b[], int degg, float x)
{
float d[10], pd = 0, ps;
int j, degg, deg;
degg = deg - 1;
for (j = degg; j >= 0; j--) {
if (b[j] > 0)
c[j] = a[j] * b[j];
b[j] = b[j] - 1; // Error here
else
break;  // Error here
}
// Error here

float deriv3(float c[], int deggg, float x)
{
float d[10], pd = 0, ps;
int k, deggg;
for (k = deg; k >= 0; k--) {
if (b[k] > 0)
q[k] = c[k] * b[k];
b[k] = b[k] - 1; // Error here
else
break;          // Error here
}
deggg = degg - 1;
// Error here
} // Error here```

Indentation style - Wikipedia[^]
Best C++ Formatter and Beautifier[^]
Online C/C++ Formatter, Indenter and Beautifier – Techie Delight[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]

v2
merano99 23-Jul-22 8:25am
Patrice T 23-Jul-22 9:35am
Not sure OP really care the hints given :)
merano99 23-Jul-22 10:13am
Either he can't or he doesn't understand what we're talking about. The fact that we have already received several almost identical, almost unchanged problem programs is not a good sign.

## Solution 3

Many people have already commented on the code. It's a mystery to me why the comments and suggestions are being ignored. In addition to the obvious errors that the compiler complains about, there are also logical problems.
Since Original Griff and PatriceT have already explained some problems very clearly (+5 for both), I'll try it again as an example at one point. For example, CPallini's suggestion of writing just one derivative function and then using it multiple times would lead to a much more universal solution. My comment that the derivative function produces a negative index and exponent was also ignored. Since the derivation is the most important function here, it should also be correct and perform as well as possible. Currently this code is used.

What is wrong with that?
C
```/* function for finding the derivative at some value of x */
float deriv(float a[], int deg, float x)
{
float d[10], pd = 0, ps;
int i;

for (i = 0; i <= deg; i++) {
ps = pow(x, deg - (i + 1));
d[i] = (deg - i) * a[deg - i] * ps;
pd = pd + d[i];
}

return pd;
}```

The last index i of the loop has the value deg. Plugging this value into the formula gives:
C
`deg - (deg + 1) = -1`

So the following is calculated:
C
`ps = pow(x, -1);`

However, this value is obviously not required and does not contribute in any way to the result. Here you are lucky that the result is not also wrong.

The locally defined field "float d[10]" is not really needed and massively restricts the function. If the variable deg is greater than 10, the program crashes or does nonsense. In fact, this can avoid weak points, greatly shorten the function and improve the calculation accuracy at the same time.
C
```double d = 0.0;
for (int i = 1; i <= deg; i++) {
d += i * a[i] * pow(x, i-1);
}
return (float)d;```

Patrice T 23-Jul-22 9:35am
+5
merano99 23-Jul-22 10:09am
thanks!