Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- void ResolveV(float Angle, float Velocity, float *XVelocity, float *YVelocity);
- void TimeOfFlight(float YVelocity, float *TOF);
- int main(int argc, char *argv[])
- {
- float Angle, Velocity, XVelocity, YVelocity, TOF, Time[11], XDistance[11], YDistance[11];
- char AngleT[100], VelocityT[100], *next, save, FileName[50];
- int i;
- if (argc == 1)
- {
- printf("Please enter the angle in degrees.\n");
- scanf ("%s", &AngleT);
- printf("Please enter the velocity in m/s.\n");
- scanf ("%s", &VelocityT);
- printf("\n");
- }
- else if (argc != 3)
- {
- printf("You have entered %d variables, please enter only an angle in degrees and a velocity.\n", argc - 1);
- return(0);
- }
- else
- {
- strncpy(AngleT, argv[1], 100);
- strncpy(VelocityT, argv[2], 100);
- }
- Angle = strtof (AngleT, &next);
- if ((next == AngleT) || (*next != '\0') || (Angle <= 0) || (Angle > 90))
- {
- printf("%s is not valid, only positive numbers up to 90 are valid for the angle.\n", AngleT);
- return 0;
- }
- else
- {
- Velocity = strtof (VelocityT, &next);
- if ((next == VelocityT) || (*next != '\0') || (Velocity <= 0))
- {
- printf("%s is not valid, only positive numbers are valid for the velocity.\n", VelocityT);
- return 0;
- }
- else
- {
- printf("Angle = %f degrees\n", Angle);
- Angle = Angle * (M_PI / 180);
- printf("Velocity = %f m/s\n", Velocity);
- }
- }
- ResolveV(Angle, Velocity, &XVelocity, &YVelocity);
- TimeOfFlight(YVelocity, &TOF);
- printf("\nTime(s) |");
- for(i=0; i<11; i++)
- {
- Time[i] = TOF * (float)i / 10;
- XDistance[i] = XVelocity * Time[i];
- YDistance[i] = YVelocity * Time[i] + (-4.905 * Time[i] * Time[i]);
- printf("%7.2f|", Time[i]);
- }
- printf("\nX Distance(m/s)|");
- for(i=0; i<11; i++)
- {
- printf("%7.2f|", XDistance[i]);
- }
- printf("\nY Distance(m/s)|");
- for(i=0; i<11; i++)
- {
- printf("%7.2f|", YDistance[i]);
- }
- printf("\n\nWould you like to save this data to a file? \nEnter y if yes, n if no.\n");
- do
- {
- save = getch();
- if (save == 'n')
- {
- printf("\n\nData will not be saved.");
- }
- else if (save == 'y')
- {
- printf("\n\nEnter the name of the file.\n");
- scanf("%s", FileName);
- FILE *fptr;
- if((fptr = fopen(FileName, "w"))== NULL)
- {
- printf("\nError opening file");
- return(0);
- }
- fprintf(fptr, "Angle: %f degrees\nVelocity: %f m/s\n\nHorizontal velocity: %f m/s\nVertical velocity: %f m/s", Angle * 180 / M_PI, Velocity, XVelocity, YVelocity);
- fprintf(fptr, "\n\nTime(s) |");
- for(i=0; i<11; i++)
- {
- Time[i] = TOF * (float)i / 10;
- XDistance[i] = XVelocity * Time[i];
- YDistance[i] = YVelocity * Time[i] + (-4.905 * Time[i] * Time[i]);
- fprintf(fptr, "%7.2f|", Time[i]);
- }
- fprintf(fptr, "\nX Distance(m/s)|");
- for(i=0; i<11; i++)
- {
- fprintf(fptr, "%7.2f|", XDistance[i]);
- }
- fprintf(fptr, "\nY Distance(m/s)|");
- for(i=0; i<11; i++)
- {
- fprintf(fptr, "%7.2f|", YDistance[i]);
- }
- fclose(fptr);
- printf("\nFile saved.");
- }
- else
- {
- printf("\nInvalid input, press 'y' or 'n'.");
- }
- }
- while ((save !='y')&(save !='n'));
- return(0);
- }
- void ResolveV(float Angle, float Velocity, float *XVelocity, float *YVelocity)
- {
- *XVelocity = Velocity * (cos(Angle));
- *YVelocity = Velocity * (sin(Angle));
- printf("\nThe horizontal velocity is %.2f m/s\n", *XVelocity);
- printf("The vertical velocity is %.2f m/s\n", *YVelocity);
- }
- void TimeOfFlight(float YVelocity, float *TOF)
- {
- *TOF = -2 * (YVelocity / -9.81);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement