Difference between two programs

I've been messing around with my code from this topic cplusplus.com/forum/beginner/182322/ until I managed to get it to work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <time.h>
using namespace std;

void wait(int seconds)
{
	clock_t endwait;
	endwait = clock() + seconds * CLOCKS_PER_SEC;
	while (clock() < endwait) {}
}

int main()
{
	const double G = 6.67408e-11;
	double m1, m2, d, i, p, F, a1, a2, s1, s2, t, cont, distparz, v1, v2,r1,r2,v1k,v2k,conth,contg,distmin,ao1,ao2,sdiff1,sdiff2,datiMR[12] = {  5.972e+24 , 7.34767309e+22 , 1.989e+30 , 3.844e+8 , 6.371e+6 , 1.737e+6  , 6.96e+8 , 3.39e+6, 3.84403e+8 , 1.496e+11 , 9.2451e+10 , 2.279e+11 };
	                                                                                     	          					        //datimr         mterra        mluna           msole      mmarte    rterra     rluna         rsole    rmarte       dtl         dts         dtm        dsm   
	float caricamento;
	int sel,graph[80];
	printf("Questo programma analizza due corpi che collassano su se stessi senza attriti e con v0 = 0.\nTutti i dati inseriti vanno interpretati nelle unita' dell'MKS.\nInserisci la massa del corpo n'1. Se si inserisce 0 si potra' scegliere tra dati predefiniti:\n");
    scanf("%lf", &m1);
    if(m1 == 0) {
    	do{
			printf("O: Terra\n1: Luna\n2: Sole\n3: Marte\n");
			scanf("%i",&sel);
		}
		while(sel > 3);
		m1 = datiMR[sel];
		r1 = datiMR[sel+4];
		goto input2;
	}
    printf("Inserisci il raggio del corpo n'1. Inserire 0 per considerare un corpo puntiforme:\n");
    scanf("%lf", &r1);
    input2:
	printf("\nInserisci la massa del corpo n'2. Se si inserisce 0 si potra' scegliere tra dati predefiniti:\n");
    scanf("%lf", &m2);
    if(m2 == 0) {
    	do{
			printf("O: Terra\n1: Luna\n2: Sole\n3: Marte\n");
			scanf("%i",&sel);
		}
		while(sel > 3);
		m2 = datiMR[sel];
		r2 = datiMR[sel+4];
		goto input3;
	}
    printf("Inserisci il raggio del corpo n'2. Inserire 0 per considerare un corpo puntiforme:\n");
    scanf("%lf", &r2);
    input3:
    distmin = r1 + r2;
	printf("\nInserisci distanza tra i due corpi. Per inserire la distanza tra le due superfici inserire 1, poi il valore. Se si vuole inserire la distanza tra i due centri di massa inserire 2, poi il valore. Se si inserisce 0 si potra' scegliere tra dati predefiniti. ATTENZIONE! Se viene inserito 1 ricordarsi di digitare un valore maggiore alla distanza minima di %f m\n",distmin);
    do {
		scanf("%i", &sel);
	}
	while(sel<0||sel>2);
	if(sel == 0) {
    	do{
			printf("O: Terra-Luna\n1: Terra-Sole\n2: Terra-Marte\n3: Sole-Marte\n");
			scanf("%i",&sel);
		}
		while(sel > 3||sel < 0);
		d = datiMR[sel+8];
		goto input4;
	}
	scanf("%lf",&d);
	if(sel == 2) {
    	d = d - r1 - r2;
	}
	if(d < 0) {
		printf("ATTENZIONE! La distanza tra le superfici dei due corpi e' minore a zero! Questo significa che i due corpi sono l'uno dentro l'altro. Il programma si chiudera'...");
		goto endp;
	}
	input4:
	printf("Decidi la precisione del calcolo: piu' il numero e' basso piu' il calcolo sara' preciso ma occorreranno piu' risorse per svolgerlo:\n");
    scanf("%lf", &t);
    cont = 0.0;
	distparz = d;
	for (;;)
    {
    	F = G * m1 * m2 / (distparz*distparz);
    	//cout << "\n" << F << "\n";
    	a1 = F / m1;
        a2 = F / m2;
        s1 = 0.5 * a1 * t * t;
        s2 = 0.5 * a1 * t * t;
        v1 = a1 * t;
        v2 = a2 * t;
    	cont++;
    	s1 = v1 * t;
    	s2 = v2 * t;
    	distparz = distparz - s1 - s2;
    	caricamento = 100 - (100 * distparz) / d;
    	if(distparz <= 0) {
			sdiff1 = (-0.5) * distparz * s1 / s2;
			sdiff2 = -(distparz + sdiff1);
			cont = cont - (2 * sdiff1 / ao1 + 2 * sdiff1 / ao1);
			distparz = distparz + sdiff1 + sdiff2;
			break;    		
		}
		ao1 = a1;
		ao2 = a2;
    	std::cout << "Calcolo in corso... " << caricamento << "% completato...\n";
    }
    cont = cont * t;
    v1k = v1 * 3.6;
    v2k = v2 * 3.6;
    conth = cont/60;
    contg = conth/24;
	cout << "\nCALCOLI COMPLETATI\n";
    wait(2);
	cout << "\nSe questo valore e' 0 i calcoli sono corretti: " << distparz << "\n";
	printf("Dati: m1 = %lf m2 = %lf\nd = %lf (tra le superfici)\nI corpi collasseranno su se stessi in %lf s (%f h o %f giorni)", m1,m2,d,cont,conth,contg);
	printf("\nLa velocita' massima raggiunta dal corpo 1 e' %lf m/s o %lf km/h\nLa velocita' massima raggiunta dal corpo 2 e' %lf m/s o %lf km/h\n", v1,v1k,v2,v2k);
    endp:
	system("pause");
}


Now I compared it with Chervil's code but I can't find any differences on the formula and calculus part. The problem is that I am gettig very different reuslts and it takes very long (much longer) to get those results.
Can someone help me please?
I just had a quick look. As far as I can tell, the algorithm used inside the main for loop is very different to the one I used. It appears to be ignoring the fact that in all except the first iteration, the two bodies are in motion, they each have a non-zero velocity.

It's also a sign that something is wrong when at line 85 there is:
 
    s1 = 0.5 * a1 * t * t;
and at line 90:
 
    s1 = v1 * t; 

It looks like you were thinking in terms of the equation
s = ut + 0.5at2
but didn't quite get there.
You might also want to consider
v = u + at

... but whichever you use, remember to preserve the velocity at the end of each iteration. The final velocity in one pass becomes the initial velocity on the next pass.

For reference, here's a version of the main part of my code contained inside its own function. This makes it clearer which are the input parameters and what is the output value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
double fallTime(double mass1, double mass2, double start_dist, double interval)
{
    const double G = 6.67408e-11;  // Gravitational constant.
        
    std::cout << std::fixed << std::setprecision(6);
    
    long long int  count = 0;
    double distance_travelled = 0;  
    double v1 = 0;
    double v2 = 0;  

    while (distance_travelled < start_dist)
    {
        double separation = start_dist - distance_travelled;
        double F = G * mass1 * mass2 / (separation*separation);
        double a1 = F / mass1;
        double a2 = F / mass2;
        v1 += a1 * interval;
        v2 += a2 * interval;
        double s1 = v1 * interval;
        double s2 = v2 * interval;

        count++;
        
        distance_travelled += s1 + s2;
    }
                    
    return count * interval;;
}



One other comment, an error in my own code, or at least an inaccurate representation of the reality. In my code, the loop ends when the centres of the two bodies collide. But if we talk of a real situation such as Earth-Moon, the centre of the Moon would not collide with the centre of the Earth. We have to take into account the radius of each, and stop when their surfaces collide.
Thanks, now it has been corrected.
One other comment, an error in my own code, or at least an inaccurate representation of the reality. In my code, the loop ends when the centres of the two bodies collide. But if we talk of a real situation such as Earth-Moon, the centre of the Moon would not collide with the centre of the Earth. We have to take into account the radius of each, and stop when their surfaces collide.

Yea I already put that part in (line 94)
1
2
3
4
5
6
7
if(distparz <= 0) {
			sdiff1 = (-0.5) * distparz * s1 / s2;
			sdiff2 = -(distparz + sdiff1);
			cont = cont - (2 * sdiff1 / ao1 + 2 * sdiff1 / ao1);
			distparz = distparz + sdiff1 + sdiff2;
			break;    		
		}

Thanks again, I'll mark it as solved
Topic archived. No new replies allowed.