# Thread: urgent assignment(due tonight) help

1. ## urgent assignment(due tonight) help

I am supposed to write a program to give the maximum/minimum of a parabola...using user-input values of a,b and c and values of x from -20 to 20 at increments of 0.01.
the output is only supposed to be the value for y and x where y is max/min depending on the parabola....
i wrote the program (below) but its not working prprly....can sumboldy plz plz help me out??

Code:
```#include <stdio.h>
int
main (void)
{

double a,b,c,x,y;

printf ("Enter values for a, b and c: ");
scanf ("%lf %lf %lf",&a,&b,&c);

for (x = -20.0;x<=20.0;x = x + 0.01)
{
if (a>0 && x == -(b/(4*a)))
{
y = a*(x*x) + b*x + c;
printf ("equation                max/min of y       value of x\n");
printf ("-------------------     --------------     ----------\n");
printf ("y = %lfx*x - %lf - %lf     %4.2f  (min)          %4.2f      \n");
printf ("-------------------     --------------     ----------\n");
}
else if (a<0 && x == -(b/(4*a)))
{
y = a*(x*x) + b*x + c;
printf ("equation                max/min of y       value of x\n");
printf ("-------------------     --------------     ----------\n");
printf ("y = %lfx*x - %lf - %lf     %4.2f  (max)          %4.2f      \n");
printf ("-------------------     --------------     ----------\n");
}
}

printf ("Report presented by ");
return (0);
}```

i've tried almost everything i can think of but nothing's helping...

the assignment:

What to do:
The equation of the parabola is
y = ax&#178; + bx + c
Write a complete C program capable of finding the maximum or
minimum of a parabola. As input, the program is to ask the user
data consisting of the values of a, b and c for the 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.
Your program must produce a nicely aligned report. Display the
minimum/maximum value of y and the value of x at that point.
Display your name(s) at the bottom of the report.
indentation.
All numerical values on the report must come from variables,
not constants.

output:

equation max/min of y value of x
------------------- -------------- ----------
y = 1.1x*x – 2x + 1 0.09 (min) 0.91
------------------- --------------- ----------

Report presented by your name(s) here.

3. It's very unlikely that "x == -(b/(4*a))" will ever be true for randomly inputted values of a and b, so you would expect the program to do nothing. Generally, comparing floating-point numbers for equality is unlikely to work properly due to the finite precision in which they're stored. I take it you're not allowed to do this using calculus (or just rewriting the equation of the parabola so its maximum/minimum is obvious from the equation)?

4. what if i remove that condition...n only keep a<0 or a>0 as conditions for min/max?
shud it work then?

5. i'm not sure abt the calculus or rewriting of the eqn...

6. You need to keep track of which of the approximately 400 values of x corresponds to the largest value of y, and you currently have nothing in your code to do that. You need to define a new variable, call it x0, so your program can remember what the value of x corresponding to the last found maximum/minimum value of y was.

Edit: BTW, you can simplify the code by noting that if a > 0, the parabola is concave up, so you need the minimum. If a < 0, it's concave down, so you need the maximum, but you can just flip the signs of a, b, and c to change to the equivalent minimum problem, so you only need to write the code for one of the two cases.