Like Tree2Likes
  • 1 Post By grumpy
  • 1 Post By jimblumberg

Formula Error

This is a discussion on Formula Error within the C++ Programming forums, part of the General Programming Boards category; So i am not getting the formula for the last one I'm not sure why it is not coming out. ...

  1. #1
    Registered User hungrymouth's Avatar
    Join Date
    Oct 2012
    Posts
    19

    Formula Error

    So i am not getting the formula for the last one I'm not sure why it is not coming out. And also for the Fahrenheit my value keeps equaling to 0 I am not sure why.


    Code:
    //study iomanip to depth for this project
    //to define a constant use #define PI 3.14159
    
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    
    #define PI 3.14159
    #define g  9.81
    using namespace std;
    
    
    int main()
    {
        
        double Volume,SurfaceArea,Circumference,Area,Sum, SideA;
        double Length,Width,Height,SlantHeight,Radius,Value;
        int F,K,C; // temperatures
        double Number, a, l;
        double P,V,R,T
    
        SideA = 15; // This is the base of the triangle
        Height = 3;   
        Area = 1 * SideA * Height / 2;
        cout << "\n\t\tThe area of this beautiful triangle = "
             << Area;
        
        //Volume of Sphere
        Radius = 6; 
        Height =  3; 
        Volume = 4/3 * PI * Radius * Radius;
        cout << "\n\t\t The volume of this cone = " << Volume;
    
        //The Sum
        Number = 4;
        a = 1;
        l = 7;
    
        Sum = Number/2 * (a+l);
        cout<<"\n\n\n The Sum of this sum = " << Sum;
    
    
        // I don't know
        Radius = 3;
        SlantHeight = 4;
    
        Area = PI * Radius * SlantHeight * 1/2;
        cout<<"\n\n\n The Area of this = " << Area;
    
        //Celcius
        C = 100;
        cout << "\n\t\tThe value of 5 / 9 * (F - 32) = "
             << 5/9* (F - 32) << " Farenheit";
    
        //Farenheit
        F = 150;
        cout <<"\n\t\tThe value of 9 / 5 * C + 32 = "
             << 9/5 * C + 32 <<" Celcius ";
    
        //  Last Formula
        P = 3;
        V = 6;
        R = 2;
        T = 7;
    
        Number = P*V/T*R;
    
        cout<<"\n\n\t The value of P * V/ T* R = " << Value;
        
        cout << "\n\n\n";
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,671
    Integer arithmetic. Let's look at the expression
    Code:
       cout << 5/9 * (F - 32);
    In your code, F is of type int, although that doesn't affect how 5/9 is computed.

    So what happens?

    5 is interpreted by the compiler as being a literal of type int. So is 9. Computing 5/9 therefore involves integer arithmetic - it produces a result of type int with rounding toward zero (since both 9 and 5 are positive). Hence 5/9 is computed as zero. Since F is an int, F - 32 is also computed, with type int. Then the two intermediate results (5/9 which has value zero, and F - 32) are multiplied, giving output of zero.

    The fact that you think 5/9 is a fraction does not mean the compiler does.

    To get the result you expect, you need to force the division 5/9 to be done as a floating point operation. The way that happens is to convert either the 5 or the 9 (or both) to be floating point. In the following code, each line has the same result.
    Code:
       cout << 5.0/9 * (F - 32);        /*   5.0 is of type double, so 9 is converted to double before doing division. */
    
       cout << 5/9.0 * (F - 32);        /*   9.0 is of type double, so 5 is converted to double before doing division. */
    
       cout << 5.0/9.0 * (F - 32);        /*   5.0 and 9.0 are both of type double, so dividing them produces a result of type double.  */
    
       cout << (double)5/9 * (F - 32);   /*  This forces the compiler to convert 5 to double.   9 is therefore converted to double before doing the division, and the result is of type double. */
    
       cout << 5/(double)9 * (F - 32);   /*  This forces the compiler to convert 9 to double.   5 is therefore converted to double before doing the division, and the result is of type double. */
    
        cout << (double)5/(double)9 * (F - 32);   /*  This forces the compiler to convert both 5 and 9 to double.  The result of division is of type double. */
    Note that, in every case above, F-32 is computed as an int, which is then converted to double (since 5/9 has been computed as a double) before doing the multiplication.
    Last edited by grumpy; 02-08-2013 at 06:33 PM.
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Also posted here.

    Jim
    Salem likes this.

  4. #4
    Registered User hungrymouth's Avatar
    Join Date
    Oct 2012
    Posts
    19
    It's a little late to reply but I sorted it thanks guys! Changed the 5/9 to 5.0/9.0 and it worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with a formula!!
    By brigas in forum C Programming
    Replies: 4
    Last Post: 03-15-2012, 02:49 PM
  2. need help on formula?
    By Radox in forum C Programming
    Replies: 3
    Last Post: 03-04-2011, 09:14 PM
  3. Anyone know how to use this formula?
    By gator6688 in forum C++ Programming
    Replies: 3
    Last Post: 11-09-2007, 10:42 PM
  4. Formula for Pi
    By zowen in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 06:17 AM
  5. formula
    By pete in forum C++ Programming
    Replies: 7
    Last Post: 05-26-2003, 09:32 PM

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