Thread: Working with 'while' loop.

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    Working with 'while' loop.

    Hi again everyone. So, I got past the segfault error in my code and now it compiles and returns an actual output. However, the output is backwards. For example, besides the numbers being off, the first line is T=1.87... and R = 0.45. However, it should be flipped. That is, when T=1.87..., R should be 0. Here's the code:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    double G, T, Mtot, R, v, B, A, C, Pi, distance;
    
    int main (void)
    {
    
    G = 6.67e-11;
    Pi = 3.14159;
    
    printf("Enter the combined mass of the systems (kg):");
    scanf("%lf", &Mtot);
    
    printf("Enter distance between galaxies (kpc):");
    scanf("%lf", &R);
    
    printf("Enter the relative velocity of the approaching galaxy (km/s):");
    scanf("%lf", &v);
    
    while (R > 1.0e-21)
    {
    
    C = ((pow(v,2))/2.0) - (G*Mtot)/R;
    
    T = (2.0*Pi*v*(pow(R,2)))/(C*G*Mtot);
    printf("\nWhen T = %d years",T);
    printf("  then the other galaxy is %f kpc away",R);
    R=R-0.05;
    }
    
    }
    Does anyone know why it's doing this or what I could do to fix it?

    In case another explanation of what my code should do is needed:

    The program is meant to calculate the time it would take for two galaxies to merge after meeting. Treating them both as point-masses, here are the inputs:

    R = distance between the galaxies = 0.45 (kpc)
    Mtot = total mass of the two galaxies = 3762e39 (kg)
    v = velocity of the approaching galaxy = 150 (km/s)

    The program plugs that into the equation for C and T = (2*Pi*v*R^2)/(C*G*Mtot) and outputs the value for T, which comes out to be 1.87 x 10^9.

    What I'm trying to do with the second part is have the program able to return that T in increments. For example, when T = 1 x 10^9 (1 billion years), then the other galaxy is ___ kpc away, and so on. And I want it to be able to do this starting from the initial R, 0.45 kpc, and ending when R = 0.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You could end it when R hits 0 but you'll get your segfault back because you'll be dividing by 0 in your first equation... as we've already seen.

    BTW.. since this is essentially the same as your last thread... there was no reason to create a new one.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    6
    Ah, I'm sorry. I wasn't entirely sure, since the problem I'm dealing with now is different.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop isn't working
    By cantinero74 in forum C Programming
    Replies: 3
    Last Post: 04-29-2010, 08:19 PM
  2. EOF not working in a loop
    By Malabux in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2003, 06:28 PM
  3. loop not working
    By Jarrette in forum C++ Programming
    Replies: 12
    Last Post: 04-27-2003, 11:34 PM
  4. for loop not working
    By clementd in forum C Programming
    Replies: 4
    Last Post: 01-01-2003, 10:29 AM
  5. while() loop isn't working right..
    By Captain Penguin in forum C++ Programming
    Replies: 20
    Last Post: 10-03-2002, 10:29 PM