Thread: Help: The code runs but my calculations don't

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    1

    Help: The code runs but my calculations don't

    Hi there I just wondered if someone could offer any advice as to how to rephrase or repair my calculations in the following code. I need to use a user inputted velocity (float 'v') and a user inputted duration (float 'tO') in the formula t=tO/square root of (1-(c^2/v^2)) But for some reason I'm having trouble. If anyone can help I'd really appreciate it (I did try to use code tags but may not have done so successfully):

    Code:
    #include <stdio.h>                    
    #include <math.h>
    
    
    int main()                            
    {
        int c=299792458,sqrt=0.5;        
        char again;
        float t=0,tO=0,v=0,calc_a=0,calc_b=0,calc_c=0,ans=0;
    
    
            do                        
        
        
                {
                    printf ("Hello user this calculator can calculate time dialation if you enter your velocity and journey duration.\n");
                    printf ("Please enter your velocity in m/s: ");
                    scanf  ("%f", &v);
    
    
                    if (v>=299792458)
                    {
                    }
    
    
                    if (v<=0)
                    {
                    }
    
    
                    else 
            
                    printf ("Please enter the duration of your journey to the nearest whole year: ");
                    scanf  ("%f", &tO);
    
    
                    if (tO<=0)
                    {
                    }
    
    
                    else
    
    
                    calc_a = (1-((c*c)/(v*v))); // problem area //
                    calc_b = (pow(calc_a,sqrt));
                    t = (tO/calc_b);
                    calc_c = (t-tO);
    
    
                    printf ("Therefore the time observed at the origin %g - the journey duration %g gives us the time contraction\n", t,tO);
                    printf ("Which equals %g years of time dialation",c);
                    getchar();
    
    
                    printf ("Do you want to do another calculation?\n");
                    printf ("If so press the 'y' key, otherwise press any other key: ");
                    scanf  ("%c",&again);
    
    
                }    while (again == 'y');
                    
    
    
                    getchar();
                    printf ("Thank you for using my program!\n");
                    printf ("Press enter to close the program\n");
                    getchar();
                    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Don't use magic numbers. Instead of using 299792458, put the following at the top of your code:
    Code:
    #define SPEED_OF_LIGHT    299792458.0  // the .0 ensures it's of type 'double'
    Then use SPEED_OF_LIGHT everywhere instead of the literal number.

    You declare c as an integer. c is approximately 300,000,000. If you square that, you get approximately 90,000,000,000,000,000, which is probably too large to fit in an integer on your system. floats also don't have the biggest representation. You should define all your variables as a double, and use %lf to read them with scanf.

    Code:
    calc_b = (pow(calc_a,sqrt));
    This is not a valid statement. Your compiler should be yelling at you about this. sqrt is a function itself, not a valid parameter to the pow function. Just use sqrt(calc_a).
    Last edited by anduril462; 05-30-2012 at 04:11 PM. Reason: v is not an integer

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The code I'm looking at doesn't have v declared as an integer.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by whiteflags View Post
    The code I'm looking at doesn't have v declared as an integer.
    You're right, my mistake (previous post edited for correctness). And I see now that sqrt is declared as a variable, so it's use as a parameter to pow looks a little more justified, except oddly it is declared as an integer, and assigned a non-integer value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runs on wxdevc++, but not on OS!
    By Lord Asriel in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2011, 07:16 AM
  2. Code runs on Windows 7 but not Vista?
    By gken05 in forum C++ Programming
    Replies: 7
    Last Post: 05-23-2011, 12:51 PM
  3. Replies: 6
    Last Post: 09-18-2010, 08:45 PM
  4. This runs..but why not this. Help me to understand, please
    By polymatrix in forum C Programming
    Replies: 21
    Last Post: 07-22-2008, 02:16 AM
  5. DirectX code runs on my machine, but not another
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 06-16-2002, 02:08 PM