Thread: Code generating wrong answer

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    12

    Post Code generating wrong answer

    hi i have made this code to calculate the density when a user input for altitude is given using two different functions.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    #define pi 3.14
    double getAltitude();
    double density(double a);
    
    
    int main()
    {
    double p, a;
    
    
    getAltitude();
    
    
    p = density(a);
    
    
    printf("density =%lf \n", p);
    
    
    return 0;
    }
    
    
    
    
    
    
    double getAltitude()
    {
        double a;
        double x;
    
    
        printf("\nEnter altitude (m):");
        scanf("%lf", &a);
    
    
    
    
        if (a > 9000)
        {
            printf("Invalid input! Altitude must be between 0 and 9000m");
            return getAltitude();
        }
        else if ( a < 0 )
        {
            printf("Invalid input! Altitude must be between 0 and 9000m");
            return getAltitude();
        }
        x = density(a);
        return a; 
    }
    
    
    
    
    double density(double a)
    {
         double density, d ;
         density = (1.2 - (1.33*pow(10, -4))*a);
         return density; 
    }
    however when the density is calculated it is always the wrong answer, for example if the user inputs an altitude of 1500 the density should then read 1.005 (using a calculator can easily be calculated), however i am getting a output of 1.2000.

    where am i going wrong and what needs to be fixed?

    cheers

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In main, write:
    Code:
    a = getAltitude();
    As it stands, you did not give a an initial value. You should remove this from getAltitude:
    Code:
    x = density(a);
    since you're already computing density in main
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    12
    cheers this worked however I'm now having the same problem with another code and i cannot see why. see below

    Code:
    int main()
    {
    printf("-- Wind turbine power calculator -- \n");
    
    
    double A, V, Cp, Ng, Nb, p, Pwr, d, a, h;
    
    
    A = getType();
    
    
    d = getDiameter();
    
    
    a = getAltitude();
    
    
    V = getVelocity();
    
    
    Cp = getPerformance();
    
    
    Ng = getGenerator();
    
    
    Nb = getGearbox();
    
    
    p = density(a);
    
    
    printf("density =%lf \n", p);
    Pwr = p*Cp*0.5*Ng*Nb*pow(V,3);
    
    
    printf("Power =%0.3lf \n", Pwr);
    
    
    return 0;
    }
    this is my main code i now have the correct density however same as before the power is outputting wrong values. the functions for the variables in the Pwr equation all look like this:

    Code:
    double getPerformance()
    {
    double Cp;
    
    
    printf("\nEnter performance coefficient:");
    scanf("%lf", &Cp);
    
    
    if ( Cp > 0.56)
    {
    printf("Invalid input! performance coefficient must be between 0.35 and 0.56");
    return getPerformance();
    }
    
    
    else if (Cp < 0.35)
    {
    printf("Invalid input! performance coefficient must be between 0.35 and 0.56");
    return getPerformance();
    }
    return Cp;
    }
    the equation for power is P = (0.5 x Ng x Nb x Cp x p x V^3)
    do you see anything wrong with these codes also? your help is greatly appreciated.

    cheers

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What I would do is start with this:
    Code:
    #include <stdio.h>
    
    double getType(void);
    double getDiameter(void);
    double getAltitude(void);
    double getVelocity(void);
    double getPerformance(void);
    double getGenerator(void);
    double getGearbox(void);
    double density(double altitude);
    
    int main(void)
    {
        printf("-- Wind turbine power calculator -- \n");
    
        double A, V, Cp, Ng, Nb, p, Pwr, d, a, h;
    
        A = getType();
        d = getDiameter();
        a = getAltitude();
        V = getVelocity();
        Cp = getPerformance();
        Ng = getGenerator();
        Nb = getGearbox();
        p = density(a);
        printf("density =%lf \n", p);
    
        Pwr = p * Cp * 0.5 * Ng * Nb * V * V * V;
        printf("Power =%0.3lf \n", Pwr);
    
        return 0;
    }
    
    double getType(void)
    {
        return 0.0;
    }
    
    double getDiameter(void)
    {
        return 0.0;
    }
    
    double getAltitude(void)
    {
        return 0.0;
    }
    
    double getVelocity(void)
    {
        return 0.0;
    }
    
    double getPerformance(void)
    {
        return 0.0;
    }
    
    double getGenerator(void)
    {
        return 0.0;
    }
    
    double getGearbox(void)
    {
        return 0.0;
    }
    
    double density(double altitude)
    {
        return 0.0;
    }
    Change all the return 0.0 to return your test values. Then, compile and run the program and see if you get your expected result. If you don't, then perhaps the problem lies with your equation, whether the one in your program, or the one you computed for the expected value.

    If you do get your expected result, you could change some other return values to keep checking that it wasn't a fluke. Once you're satisfied, implement getType. Test again. If getType works as expected, chose one (and only one) other function to implement, implement it then test. Keep on going until you have implemented all the functions. Done!

    This systematic and incremental approach to development allows you to spot bugs early and hence more easily. What you did instead was to try and implement everything at once, then test. If there's a bug... well, you can still find it, but you'll need to do a lot more work with a debugger since it is more difficult to see where the bug might be.

    EDIT:
    Another thing. Do not use recursion like this:
    Code:
    double getAltitude()
    {
        double a;
        double x;
    
    
        printf("\nEnter altitude (m):");
        scanf("%lf", &a);
    
    
    
    
        if (a > 9000)
        {
            printf("Invalid input! Altitude must be between 0 and 9000m");
            return getAltitude();
        }
        else if ( a < 0 )
        {
            printf("Invalid input! Altitude must be between 0 and 9000m");
            return getAltitude();
        }
    
        return a;
    }
    Rather, use iteration:
    Code:
    double getAltitude(void)
    {
        double altitude;
        for (;;)
        {
            printf("\nEnter altitude (m):");
            if (scanf("%lf", &altitude) == 1 && altitude <= 9000 && altitude >= 0)
            {
                break;
            }
            else
            {
                printf("Invalid input! Altitude must be between 0 and 9000m");
                int c;
                while ((c = getchar()) != '\n' && c != EOF);
            }
        }
        return altitude;
    }
    Last edited by laserlight; 04-04-2019 at 03:59 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    12
    thanks alot for the help mate i have one last question if it is ok with you, this is the last part i need to finish the code. these are the functions to find the area.

    Code:
    double vArea()
    {
    double vArea, h, d;
    vArea = (pi*d*h);
    return vArea;
    }
    
    double hArea()
    {
    double hArea, d, r;
    r = d/2;
    hArea = (pi*pow(r,2));
    return hArea;
    }
    i need to call the function for d = getDiameter into both these two and h = getHeight into the first one so that these functions can use the equations within them to calculate the area. I'm not 100% sure on how to call on these functions appropriately. the functions for getDiameter() and getHeight() look like this now after your suggestion to use iteration.

    Code:
    double getDiameter()
    {
        double d;
        for (;;)
        {
            printf("\nEnter rotor diameter (m):");
            if (scanf("%lf", &d) == 1 && d <= 20 && d >= 0)
            {
                break;
            }
            else
            {
                printf("Invalid input! Diameter must be between 0 and 20m");
                int c;
                while ((c = getchar()) != '\n' && c != EOF);
            }
        }
        return d;
    }
    appreciate the help alot

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should use function parameters.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    12
    Quote Originally Posted by laserlight View Post
    You should use function parameters.
    cheers this worked.

    I'm having trouble with the function getType();

    in the function getType the user is asked for input, jf the input is v or V the function getHeight should be run followed by the function vArea. if the input is h or H the function hArea should be run. if there is any other input the coed should display "invalid input" and then return to the function getType and ask the user for input again.

    i need the value calculated by either hArea or vArea to be assigned to the function getType so i can then use this value in the main function to solve an equation. the code i have written is in the old recursion form and is not working this is what the function getType looks like:

    Code:
    double getType()
    {
    char type;
    double getType, d, h;
    
    printf("\nEnter wind turbine type (v or h):");
    scanf("%c", &type);
    
    if (type == 'v' || type == 'V')
    {
    getHeight();
    vArea(d, h);
    }
    else if (type == 'h' || type == 'H')
    {
    hArea(d);
    }
    else 
    {
    printf("Invalid input!");
    int c;
    while ((c = getchar()) != '\n' && c != EOF)
    return getType;
    }
    return type;
    }


    cheers
    Last edited by ec661; 04-04-2019 at 09:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! always getting wrong answer.
    By V8cTor in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2015, 12:22 AM
  2. Getting Wrong answer ???
    By Fazot in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2012, 08:45 AM
  3. Always give me wrong answer
    By amroto in forum C Programming
    Replies: 8
    Last Post: 03-29-2012, 09:43 AM
  4. Wrong answer?
    By eViLrAcEr in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2009, 06:04 AM
  5. wrong answer?
    By cman in forum C Programming
    Replies: 5
    Last Post: 03-09-2003, 02:17 AM

Tags for this Thread