[SD]happynoobing wrote:lol dont ever doubt cruor when it comes to programming xD
I am guessing Cruor will join the SRF's Hall Of Fame with Bakemaster
2 smart people in our HOF... who will be the 3rd one?!
Cruor wrote: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.NuclearSilo wrote:It's not the extrema he wants, but the min or max between values.
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
x=0.20
y=2.80Code: Select all
There is no extrema within the bounds -20≤x≤20
The output of the second program looks like:Code: Select all
dy/dx (0.20)=0.00; y=2.80
Regardless of what his professor meant, the OP is now covered in either situation so I suppose it doesn't matter past that.
Code: Select all
float max(float a, float b) {
If (a<b)
return b;
else
return a;
}Ah, I see now. He's looking for the local extrema then?NuclearSilo wrote:No. U didnt fully understand what i want to say. Hell, i dont understand what his teacher wants too...
The exercise requires u to find the max or min value located at the range -20,20 and it's not about the extrema of the parabola. So it has nothing to do with -b/(2a) or df=0. Or maybe im wrong. So i think pipigrande, u should ask your teacher again what he really wants.
Code: Select all
#include <stdio.h>
int main() {
/* declare variables */
float a, b, c, i, j, k;
/* 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);
/* check for quadratic function */
if(a==0){
printf("\nThat function is linear.");
return 0;
}
/* loop from x=-20 to x=20 in increments of 0.01 */
for(i=-20; i<=20; i+=0.01) {
/* compute y for x */
j = a*i*i+i*b+c;
/* if function opens down and maximum is passed.. */
if(i>-20 && a<0 && k>j){
/* find the previous value of x */
i-=0.01;
break;
}
/* if function opens up and minimum is passed.. */
else if(i>-20 && a>0 && k<j){
/* find the previous value of x */
i-=0.01;
break;
}
/* store the value of y at x for comparison later */
k = j;
}
/* print the results */
printf("\nx=%.2f\ny=%.2f", i, k);
printf("\n\nFirstname Lastname");
return 0;
}Eww, try wxDev-C++ or MSVC Express.pipigrande wrote:Now im gonna copy them in the program which I will dowload tonight and see how it works. (Program's Name: Quincy).

Write a complete C program capable of finding the maximum or
minimum of a parabola
Find the minimum value (or maximum depending on the parabola)
of y for all values of x between -20.0 and +20.0 in increments
of 0.01.
Code: Select all
#include <stdio.h>
int main() {
/* declare variables */
float a, b, c, i, j, k;
int min;
/* 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);
/* check for quadratic function */
if(a==0){
printf("\nThat function is linear.");
return 0;
}
/* loop from x=-20 to x=20 in increments of 0.01 */
for(i=-20; i<=20; i+=0.01) {
/* compute y for x */
j = a*i*i+i*b+c;
/* if function opens down and critical point is passed.. */
if(i>-20 && a<0 && k>j){
/* find the previous value of x */
i-=0.01;
min=0;
break;
}
/* if function opens up and critical point is passed.. */
else if(i>-20 && a>0 && k<j){
/* find the previous value of x */
i-=0.01;
min=1;
break;
}
/* store the value of y at x for comparison later */
k = j;
}
/* print the results */
printf("\nequation max/min of y value of x\n");
printf("------------------- ------------ ----------\n");
printf("y = %.1fx\xFD%+.1fx%+.1f", a, b, c);
if(a>0)
printf(" ");
else
printf(" ");
printf("%.2f ", k);
if(min)
printf("(min)");
else
printf("(max)");
if(k>=0)
printf(" ");
else
printf(" ");
printf("%.2f", i);
printf("\n\nReport presented by Firstname Lastname.");
return 0;
}
Cruor wrote:I think that nothing more is needed, save for that formatted output.
Here's one more program so your professor can have his nice formatted output:Code: Select all
#include <stdio.h>
int main() {
/* declare variables */
float a, b, c, i, j, k;
int min;
/* 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);
/* check for quadratic function */
if(a==0){
printf("\nThat function is linear.");
return 0;
}
/* loop from x=-20 to x=20 in increments of 0.01 */
for(i=-20; i<=20; i+=0.01) {
/* compute y for x */
j = a*i*i+i*b+c;
/* if function opens down and critical point is passed.. */
if(i>-20 && a<0 && k>j){
/* find the previous value of x */
i-=0.01;
min=0;
break;
}
/* if function opens up and critical point is passed.. */
else if(i>-20 && a>0 && k<j){
/* find the previous value of x */
i-=0.01;
min=1;
break;
}
/* store the value of y at x for comparison later */
k = j;
}
/* print the results */
printf("\nequation max/min of y value of x\n");
printf("------------------- ------------ ----------\n");
printf("y = %.1fx\xFD%+.1fx%+.1f", a, b, c);
if(a>0)
printf(" ");
else
printf(" ");
printf("%.2f ", k);
if(min)
printf("(min)");
else
printf("(max)");
if(k>=0)
printf(" ");
else
printf(" ");
printf("%.2f", i);
printf("\n\nReport presented by Firstname Lastname.");
return 0;
}
It gets a little chunky at the end in order to keep things spaced properly when accounting for signs. I could have made the leading signs always display and skipped the spacing conditionals but it doesn't look as nice.
I suppose the only thing left now is whether or not there is anything left that you don't understand. It looks like you are expected to know how to sign and truncate floats, so I would assume that you also know the hexadecimal escape sequence "\xFD" used to print ² in the console. Past that, I don't see any problems.