A C code stops working during execution

This C code attempts to model a 2D projectile motion that stops working right after the 2nd iteration for an unknown reason.

Pseudocode:
.Store position as (x[i],y[i] and velocity as (vx[i], vy[i])
.For each time step i calculate position and velocity at step i+1
--> x[i+1]=x[i]+vx[i]*delta_t
--> y[i+1]=y[i]+vy[i]*delta_t
Compute air drag force
Forcex=-B2*v*vx[i]
Forcey=-B2*v*vy[i]

vx[i+1]=vx[i]+(Forcex/m)+delta_t
vy[i+1]=vy[i]+(Forcey/m)+delta_t

Stop when y[i+1]<=0

Estimate landing point by interpolating between the last point with y>0 and the point that would have been below ground


EXE file: [Compiled using Code Blocks]
Kindly insert down delta t 1
Enter the initial value of x
2
Enter the initial value of y
2
Enter the initial value of vx
50
Enter the initial value of vy
10
The initial value of horizontal drag force is -2549.509766
The initial value vertical drag force is -509.901978


1 52.000000 12.000000 50.000000 0.200000 -2500.020020 -10.000080
2 102.000000 12.200000 64.411064 -9.656607 -4195.150879 628.943542
THE PROGRAM STOPS WORKING HERE

Why does it stop working?

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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
	int main()
	{
	int n, count=0, B2=1, i=0, input=2;
	float delta_t, xl, yloop, v, m=400, r=0;
 	float *f_dragx=NULL;
 	 float *f_dragy=NULL;
  	  float *x=NULL;
	float *y=NULL;
	float *vx=NULL;
	float *vy=NULL;
printf("\n Kindly insert down delta t \t");
	scanf("%f", &delta_t);

while(input!=0)
{
       vx = (float*) realloc (vx, count * sizeof(float));
    vy = (float *) realloc (vy, count * sizeof(float));
     x = (float*) realloc (x, count * sizeof(float));
     y = (float *) realloc (y, count * sizeof(float));
     f_dragx= (float*) realloc (f_dragx, count * sizeof(float));
     f_dragy= (float *) realloc (f_dragy, count * sizeof(float));

printf("Enter the initial value of x\n");
scanf("%f", &x[count]);
printf("Enter the initial value of y\n");
scanf("%f", &y[count]);
printf("Enter the initial value of vx\n");
scanf("%f", &vx[count]);
printf("Enter the initial value of vy\n");
scanf("%f", &vy[count]);
v=pow(vx[count]* vx[count]+ vy[count]* vy[count], 0.5);
f_dragx[count]=-(B2)*v*vx[count];
printf("The initial value of horizontal drag force is %f\n", f_dragx[count]);
f_dragy[count]=-(B2)*v*vy[count];
printf("The initial value vertical drag force is %f \n\n\n", f_dragy[count]);

count++;

while (yloop>=0)
	{
       vx = (float*) realloc (vx, count * sizeof(float));
    vy = (float *) realloc (vy, count * sizeof(float));
     x = (float*) realloc (x, count * sizeof(float));
     y = (float *) realloc (y, count * sizeof(float));
     f_dragx= (float*) realloc (f_dragx, count * sizeof(float));
     f_dragy= (float *) realloc (f_dragy, count * sizeof(float));

x[count]=x[count-1]+vx[count-1]*delta_t;

y[count]=y[count-1]+vy[count-1]*delta_t;

vx[count]=vx[count-1]+(f_dragx[count]/m)*delta_t;
vy[count]=vy[count-1]+(-9.8+(f_dragy[count]/m))*delta_t;

v=pow(vx[count]* vx[count]+ vy[count]* vy[count], 0.5);


f_dragx[count]=-(B2)*v*vx[count];
f_dragy[count]=-(B2)*v*vy[count];

yloop=y[count];

printf(" %d   %f   %f   %f   %f   %f   %f\n", count, x[count],y[count],vx[count],vy[count],f_dragx[count],f_dragy[count]);
count++;
  }


r=(-y[count-1])/(y[count]);
xl=((x[count-1]+r*x[count])/(r+1));


	printf("To restart the program, hit any positive integer other than 0, otherwise, hit 0\n");
	scanf("%d", &input);
	}
return 0;
	}
Last edited on
Topic archived. No new replies allowed.