#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define g 9.8
#define NUM_ROWS 2 // number of columns
#define N 51 //number of rows
#define T_IX 0 // index for number of rows in time
#define VEL_IX 1 // index to rows of velocity

typedef struct 
{
    double m; // weight
    double drag;
    double tf; 
    double Values[NUM_ROWS][N]; //array holding values of time and velocity. [NUM_ROWS] = 2 columns, when 0 = time, when 1 = velocity. N is the number of rows 
}DATA;

void getInput (DATA *);
void computeVelocity (DATA *, int n); 
void displayTable (DATA *, int n); 

void main()
{
    DATA mydata;
    getInput(&mydata);
    computeVelocity(&mydata, N);
    displayTable(&mydata, N); 
}

void getInput(DATA *dataPtr)
{
    printf("please input the parachutist's weight: ");
    scanf("%lf", &dataPtr->m);
    printf("please input the parachutist's drag coefficient: ");
    scanf("%lf", &dataPtr->drag);
    printf("please input the final time: ");
    scanf("%lf", &dataPtr->tf);
}

void computeVelocity(DATA *dataPtr, int n)
{
    double term1, term2;
    int ix = 0;
    double final_time;
    double increment = 0.24;
    double time=0.0; 
    for(ix = 0; ix<N; ix++) // cannot be ix<=N bc the array only goes <N, thus values will be messed up since it looks for a array element that doesn't exist
    {
        term1 = (g*dataPtr->m) / dataPtr->drag;
        term2 = 1 - exp(-(dataPtr->drag / dataPtr->m)*time);
        dataPtr->Values[VEL_IX][ix] = term1*term2;
        
        dataPtr->Values[T_IX][ix] = time;
        time = time +increment; 
    }
}

void displayTable(DATA* dataPtr, int n)
{
    int ix;
    double time;
    printf("The change in velocity of the parachutist with weight %.1lf kg and drag coefficient %.1lf kg/s is as follows.\n", dataPtr->m, dataPtr->drag);
    printf("%s\t %s\n", "Time t", "Velocity v");
    printf("-----------------------------------------------\n");
    for(ix = 0; ix<N; ix++)
        printf("%.2lf\t %.2lf\n", dataPtr->Values[T_IX][ix], dataPtr->Values[VEL_IX][ix]); 
}