Code:
#include <stdio.h>
#include <math.h>
#define G 9.81 // m/s
/**
* The user is supposed to make a selection of an element in the array(whether Rocket 1 , Rocket 2 ,...)
* then use that selection to access the element of the array with the data (u,m0,...) and use it in my calculation.
However, program shuts down after i enter the time.
I don't know what's going on I mean i am trying to make the "selection"
* a number of an array so that in computeSpeed it can be used to indicate which element the user selected
***/
typedef struct
{
char* name;
double u;
double m0;
double q;
}ROCKET;
int selectRocket(ROCKET rockets[]);
double computeSpeed(double t , ROCKET *rockets, int selection);
int main()
{
double t;
double v;
int sel;
ROCKET rockets[4]= {
{"Rocket 1", 2000.0, 150000.0, 2700.0},
{"Rocket 2", 1596.0, 300000.0, 5367.0},
{"Rocket 3", 3267.0, 543135.0, 8900.0},
{"Rocket 4", 984.0, 5468.0, 89.5}
};
sel = selectRocket(rockets);
printf("s = %d selection is = %s %f %f %f\n", sel, rockets[sel].name, rockets[sel].u,rockets[sel].m0,rockets[sel].q);
// v = computeSpeed (t, rockets,selection);
// printf(" v =%lf ",v);
return 0;
}
int selectRocket(ROCKET rockets[])
{
int s;
printf("%s : u = %.2lf m0 = %.2lf q = %.2lf \n", rockets[0].name ,rockets[0].u,rockets[0].m0,rockets[0].q);
printf("%s : u = %.2lf m0 = %.2lf q = %.2lf \n", rockets[1].name ,rockets[1].u,rockets[1].m0,rockets[1].q);
printf("%s : u = %.2lf m0 = %.2lf q = %.2lf \n", rockets[2].name ,rockets[2].u,rockets[2].m0,rockets[2].q);
printf("%s : u = %.2lf m0 = %.2lf q = %.2lf \n", rockets[3].name ,rockets[3].u,rockets[3].m0,rockets[3].q);
printf("Please select a rocket (1 to 4): ");
scanf("%d",&s);
//to compensate for zero base array.
return ( s - 1 );
}
double computeSpeed(double t , ROCKET *rockets, int selection)
{
double v;
// no math computations ??
v = rockets[selection].q;
return v;
}
Results: