# Trying to troubleshoot a code involving a 'while' loop

Discussion in 'C' started by Von Fuzzball, May 4, 2011.

1. ### Von FuzzballNew Member

Joined:
May 4, 2011
Messages:
1
0
Trophy Points:
0
So, I'll just come out and say that I'm new to C programming. I took an introduction class for it this semester and I'm currently creating a code of my own.

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.

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

}```
It compiles and runs fine, but it returns the T values backwards. As in, it returns that when T = 1.87..., R = 0.45 (the initial R value), when it should be the flipped. R should be 0 when T = 1.87...

Could someone tell me how I could essentially reverse the order of the output? Or perhaps point out in my code why it's printing the results this way? Any help would be appreciated.

The compiler I use is Cygwin, by the way.

Joined:
Jan 22, 2011
Messages:
32