urgent assignment(due tonight) help

This is a discussion on urgent assignment(due tonight) help within the C Programming forums, part of the General Programming Boards category; I am supposed to write a program to give the maximum/minimum of a parabola...using user-input values of a,b and c ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    4

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

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    please please help...
    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.
    Your code must have good style including comments and proper
    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. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    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. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    i'm not sure abt the calculus or rewriting of the eqn...

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.
    Last edited by robatino; 10-29-2007 at 04:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Minimax, URGENT :(
    By Dark-MX0Z in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2007, 05:29 AM
  2. PROGRAM MENU, urgent, please help!
    By Anfernee001 in forum C Programming
    Replies: 13
    Last Post: 09-27-2002, 03:29 AM
  3. Help Needed: Borland C++ 5.5 Installation - URGENT!
    By Linette in forum C++ Programming
    Replies: 12
    Last Post: 03-09-2002, 05:44 PM
  4. tonight... tonight...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 11-07-2001, 10:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21