15,957,890 members
1.00/5 (1 vote)
See more:
Following is my program but according the errors
In deriv2 the degree isn't getting saved
Can anyone please tell me what is the error

What I have tried:

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

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

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

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

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

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 deg,float x)
float d[10], pd=0, ps;
int j,deg2,deg;
deg2=deg-1;
for (j=deg;j>=0;j--)
{
if (b[j]>0)
c[j]=a[j]*b[j];
b[j]=b[j]-1;
else
break;
}

// Running same deriv function and replacing deg with dy2 and a[] with c[]

float deriv3(float c[],int deg2,float x)
float d[10], pd=0, ps;
int k,deg 3;
for (k=deg;k>=0;k--)
{
if (b[k]>0)
q[k]=c[k]*b[k];
b[k]=b[k]-1;
else
break;
}
deg3=deg2-1;
// Running same deriv function and replacing deg3 with dy3 and a[] with q[]
Posted
Updated 21-Jul-22 1:52am
11917640 Member 21-Jul-22 6:00am
In deriv2 function: int j,deg2,deg; deg2=deg-1; - deg is not initialized. Don't ignore compiler warnings.

## Solution 1

scanf("%d", °);

There is no variable called ° (nor can there be, letters and numbers only are permitted, no symbols).

Did you mean
scanf("%d", &deg);

v2

## Solution 2

You really don't need deriv2 and deriv3.
The derivative of a polynomial is a polynomial as well.
You could just implement a derive_polynomial function and call it multiple times.

## Solution 3

The program cannot be compiled because there are some problems.

This function cannot work like this
C
scanf("%d", °)

The curly brackets are missing from deriv2.
C
float deriv2(float b[], int deg, float x)
{
...
}

Also, c and a are undefined and curly bracketsare missing.
C
c[j] = a[j] * b[j];

The deriv3() function is also incomplete.

The loop in the deriv function is repeated 1 time too many, resulting in a negative exponent.
C
for (i = 0; i <= deg; i++) {
ps = pow(x, deg - (i + 1));

The fact that the result could still be correct is due to multiplication by 0.

v3