my program reads data from that file :
6 150
0 1.75
30 0.8
60 0.5
70 1
120 0.1
140 0.9
and inserts those numbers(It starts from the second row) into an array of structs and then calculates the 'time'. The results are fine but one; the third one('time') is 100 but the output is 99.999992.
Here is the program :
#include <stdio.h>
#include <stdlib.h>
int di,i,k,m;
float vi,time;
int n;
int l;
struct node
{
int distance;
float velocity;
}DV[500000];
struct timeslist_node
{
struct timeslist_node *left;
int winner;
int loser;
double time;
struct timelist_node *right;
};
double calctime(int d,float v);
void print_array();
main()
{
FILE *fp;
fp=fopen("candidates.txt","r");
if (fp==NULL) exit(2);
fscanf(fp,"%d %d",&n,&l);
printf("%d,%d\n",n,l);
for(i=0;i<n;i++)
{
fscanf(fp,"%d %f",&DV[i].distance,&DV[i].velocity);
}
calctime(DV[i].distance,DV[i].velocity);
print_array();
fclose(fp);
system("pause");
}
double calctime(int d,float v)
{
for(i=0;i<n;i++)
{
if (i == 0)
{
{
if (DV[n-1].velocity==DV[i].velocity)
time=-1;
}
time=((l-DV[n-1].distance)/(DV[n-1].velocity-DV[i].velocity));
m=1;
k=n;
}
else
{
{ if (DV[i-1].velocity==DV[i].velocity)
time=-1;
}
time=((DV[i].distance-DV[i-1].distance)/(DV[i-1].velocity-DV[i].velocity));
k=i;
m=i+1;
}
printf ("t %d %d=%lf\n",m,k,time);
}
}
void print_array()
{
for(i=0;i<n;i++)
printf("D[%d],V[%d] = %d %.2f\n ",i,i,DV[i].distance,DV[i].velocity );
}