NuclearSilo wrote:It's not the extrema he wants, but the min or max between values.
Extrema is just plural for minima and maxima. Like I said, if he just wanted an extrema within the bounds -20≤x≤20 then the assignment would have been to calculate the extrema (which I may add could be done with derivatives as well as the shortcut, how it is done wouldn't matter in that case) and then check to see if it is within those bounds.
Here is what a program that uses your interpretation looks like:
Code: Select all
#include <stdio.h>
int main() {
/* declare variables */
float a, b, c, x, y;
/* request user input */
printf("y=ax\xFD+bx+c\n\n");
printf("Enter a: ");
scanf("%f", &a);
printf("Enter b: ");
scanf("%f", &b);
printf("Enter c: ");
scanf("%f", &c);
/* find the extrema */
x = (-1*b)/(2*a);
/* calculate y at extrema */
y = a*x*x+b*x+c;
/* check extrema against bounds */
if(x>=-20 && x<=20)
printf("\nx=%.2f\ny=%.2f\n", x, y);
else
printf("\nThere is no extrema within the bounds -20\xF3x%s20\n", "\xF3") ;
/* print my name */
printf("\nFirstname Lastname");
return 0;
}
And here's the derivative method:
Code: Select all
#include <stdio.h>
int main() {
/* declare variables */
float a, b, c, i, d, y;
/* request user input */
printf("y=ax\xFD+bx+c\n\n");
printf("Enter a: ");
scanf("%f", &a);
printf("Enter b: ");
scanf("%f", &b);
printf("Enter c: ");
scanf("%f", &c);
/* print the derivative */
printf("\nThe derivative is:\ndy/dx (x)=%.2fx%+.2f\n\n", a*2, b);
for(i=-20; i<=20; i+=0.01) {
/* calculate the value of the derivative at x */
d = a*2*i+b;
/* calculate y at x */
y = (a*i*i)+(b*i)+c;
/* print the results */
printf("dy/dx (%.2f)=%.2f; y=%.2f\n", i, d, y);
}
/* print my name */
printf("\nFirstname Lastname");
return 0;
}
Compare the
if/else statement in the first program to the
for loop in the second program. The
if/else statement has nothing to do with increments of 0.01 while the
for loop does. That is why I think he wanted the second solution.
Rereading it, I also realized that the y values should be printed as well.
In the first program the output looks like:
Or, if the extrema doesn't exist within the bounds:
Code: Select all
There is no extrema within the bounds -20≤x≤20
The output of the second program looks like:
Regardless of what his professor meant, the OP is now covered in either situation so I suppose it doesn't matter past that.