Help me fix this program please

I encountered some problems writing this code can you help me fix it please

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
#include <iostream>
#include <stdio.h>
#include <math.h>

int main()
{
	float m1, m2, d, i, p, F, a1, a2, s1, s2, t, count, pardist;
    printf("mass n'1\n");
    scanf("%f", &m1);
    printf("\nmass n'2\n");
    scanf("%f", &m2);
    printf("\ndistance\n");
    scanf("%f", &d);
	printf("precision (the lower the value the more precise; must not be 0)\n");
    scanf("%f", &t);
    count = 0;
    distparz = d;
    for (;;)
    {
    	F = (6,67 * 0,0000000001 * m1 * m2) / distpar*distpar;
    	a1 = F / m1;
        a2 = F / m2;
        s1 = 1 / 2 * a1 * t * t;
        s2 = 1 / 2 * a1 * t * t;
    	count++;
    	pardist = pardist - s1 - s2;
    	if(pardist == s1 + s2) break;
    }
    cont = cont * t;
	printf("time ");
	printf("%f", &count);
	printf("s\n");
    system("pause");
}  
What problems?

F = (6,67 * 0,0000000001 * m1 * m2) / distpar*distpar;
What's that , doing there? Did you mean 6.67 ?
Last edited on
Thank you, fixed it.
The problem is that the loop never ends because the break statement never works, and I don't know why
oh and also I saw there is a small error I committed copying the code. I've corrected it here's the version to look at:
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
#include <iostream>
#include <stdio.h>
#include <math.h>

int main()
{
	float m1, m2, d, i, p, F, a1, a2, s1, s2, t, count, pardist;
    printf("mass n'1\n");
    scanf("%f", &m1);
    printf("\nmass n'2\n");
    scanf("%f", &m2);
    printf("\ndistance\n");
    scanf("%f", &d);
	printf("precision (the lower the value the more precise; must not be 0)\n");
    scanf("%f", &t);
    count = 0;
    distparz = d;
    for (;;)
    {
    	F = (6.67 * 0.0000000001 * m1 * m2) / pardist*pardist;
    	a1 = F / m1;
        a2 = F / m2;
        s1 = 1 / 2 * a1 * t * t;
        s2 = 1 / 2 * a1 * t * t;
    	count++;
    	pardist = pardist - s1 - s2;
    	if(pardist == s1 + s2) break;
    }
    cont = cont * t;
	printf("time ");
	printf("%f", &count);
	printf("s\n");
    system("pause");
}  
The code does not compile.

distparz is not declared.
cont is not declared.

Perhaps you might post your actual code?

Meanwhile, the reason the loop doesn't end is because testing for equality with floating point values is unreliable. For example 2.0000001 and 1.9999999 may each be valid values which we can see are very close to the integer 2, but are not equal.

Suggestions:
use a condition which tests for <= or >= if that is appropriate. Otherwise, instead of testing a == b, you could try something like abs(a-b) < epsilon where epsilon is chosen as a suitably small value.

note: abs() is overloaded for floating-point types in C++ and is ok. But if you are coding in plain C, then use fabs().



Last edited on
I'm sorry my code is in my native language I was trying to change everything to english soyou guys could understand it...
So you are saying to use something like this in line number 27:
 
if(pardist >= s1 + s2) break;

I tested it and it worked, the only problem is that now the thing is broken because it always shows 0.00000s
Here's the original:
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
#include <iostream>
#include <stdio.h>
#include <math.h>

int main()
{
	float m1, m2, d, i, p, F, a1, a2, s1, s2, t, cont, distparz;
    printf("Inserisci la massa del corpo n'1\n");
    scanf("%f", &m1);
    printf("\nInserisci la massa del corpo n'2\n");
    scanf("%f", &m2);
    printf("\nInserisci distanza tra i due corpi\n");
    scanf("%f", &d);
	printf("Decidi la precisione del calcolo: piu' il numero e' basso piu' il calcolo sara' preciso ma occorreranno piu' risorse per svolgerlo\n");
    scanf("%f", &t);
    cont = 0;
    distparz = d;
    for (;;)
    {
    	F = (6.67 * 0.0000000001 * m1 * m2) / distparz*distparz;
    	a1 = F / m1;
        a2 = F / m2;
        s1 = 1 / 2 * a1 * t * t;
        s2 = 1 / 2 * a1 * t * t;
    	cont++;
    	distparz = distparz - s1 - s2;
    	if(distparz >= s1 + s2) break;
    }
    cont = cont * t;
	printf("I corpi collasseranno su se stessi in ");
	printf("%f", &cont);
	printf("s\n");
    system("pause");
}
I'm not sure that the calculations are being done correctly.

I don't think these equations apply here:
1
2
        s1 = 1 / 2 * a1 * t * t;
        s2 = 1 / 2 * a1 * t * t;

(even if they did, there's an error, 1/2 is an integer division, it should be 1.0/2.0)

Also, there's an error here:
F = (6.67 * 0.0000000001 * m1 * m2) / distparz*distparz;
it should be
F = 6.67 * 0.0000000001 * m1 * m2 / (distparz*distparz);

The way I would have done it is to give each object an initial velocity of zero. In each iteration the velocity will be increased according to the current acceleration, but scaled in proportion to the time interval of each iteration.

Next calculate the distance travelled during that iteration, which is simply current velocity * length of the time interval. Keep a running total of the distance travelled, which is the sum of the distance travelled in each iteration. When the sum of the distances travelled by the two objects is greater than or equal to the initial separation, the loop should end.
Last edited on
Ok thank you
I'm not sure that I got it right. But I played around with your program and gave it some actual values, the Earth-Moon system. Now the Moon is in orbit. But what if it was stationary. How long would it take to fall to Earth (and Earth to fall to Moon). I got about 4.8 days. (scary thought).

But is it reasonable?
Well, according to this page, it is (assuming those guys know what they are talking about):
https://www.physicsforums.com/threads/moon-fall-speed.183910/
Nice coding! how did you manage to get it to work? did you use the method you mentioned before? may I see your program?
Sure, it's based on your code, but chopped around quite a lot, partly so I could see which variables were the important ones, and which were just throwaway ones used in calculation. Also, I'm much more used to cout than printf() nowadays.
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
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>

int main()
{

    const double G = 6.67408e-11;  // Gravitational constant.

    double m1, m2, d, t;

    std::cout << "Inserisci la massa del corpo n'1\n";
    //scanf("%f", &m1);
    m1 = 7.349e22; // Moon
    std::cout << "\nInserisci la massa del corpo n'2\n";
    //scanf("%f", &m2);
    m2 = 5.9736e24; // Earth
    std::cout << "\nInserisci distanza tra i due corpi\n";
    //scanf("%f", &d);
    d = 384400e3; // mean distance
    std::cout << "Decidi la precisione del calcolo: piu' il numero e' basso piu' il calcolo sara' preciso ma occorreranno piu' risorse per svolgerlo\n";
    //scanf("%f", &t);
    t = 0.001;

    long long int  count = 0;
    double distance = 0;
    double v1 = 0;
    double v2 = 0;

    std::cout.precision(6);
    std::cout << std::fixed;

    for (;;)
    {
        double separation = d - distance;
        double F = G * m1 * m2 / (separation*separation);
        double a1 = F / m1;
        double a2 = F / m2;
        v1 = v1 + a1 * t;
        v2 = v2 + a2 * t;
        double s1 = v1 * t;
        double s2 = v2 * t;

        count++;
        distance = distance + s1 + s2;

        if (count%1000000LL == 0)
        {
            double days = count * t / 86400;
            std::cout << std::setw(12) << count
                      << "    separation: " << std::setw(18) << separation
                      << "  days: "       << std::setw(14) << days
                      << "  s1: "         << std::setw(14) << s1
                      << "  s2: "         << std::setw(14) << s2
                      << "  v1: "         << std::setw(14) << v1
                      << "  v2: "         << std::setw(14) << v2

                      << '\n';
        }

        if (distance >= d)
             break;
    }

    double time = count * t;

    std::cout << "I corpi collasseranno su se stessi in "
              <<  time << "s\n";

    //system("pause");
}

Wow thanks! You truly are a great help! I am learning c++ with school but we are doing just the basics and this stuff looks too advanced to me! I guess I'll have to watch some kind of tutorials online to learn more! Thanks again!
P.S. what's the difference between cout and printf anyways?
Last edited on
oh and one last thing: how much time does your program take to execute? I've been waiting for 5 minutes and it looks like the loop is still going...
It can indeed take a long time to run. You can speed it up by using a less precise time step. I didn't time it, but maybe fifteen minutes or so on my system. Watch the value of "days", it should end up somewhere around 4.8.

printf() is still valid in C++, but it is usually considered part of the C language. cout tends to be more verbose and is C++ only.

but we are doing just the basics and this stuff looks too advanced to me!

Well, it depends on whether it's the physics or the computing which is the issue. I think this is quite an advanced topic in terms of getting the equations correct. The coding itself, hard to say, things always seem difficult when one meets something new for the first time.


 
Well, it depends on whether it's the physics or the computing which is the issue. 

The major issue for now is the computing part, for the phyisics part I can just google it if I can't remember formulas. I'll mark it solved, thanks for all the help!
Topic archived. No new replies allowed.