Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming C Two dimensional Arrays Programmer defined functions (continues) Pa

ID: 3683878 • Letter: P

Question

Programming C

Two dimensional Arrays

Programmer defined functions (continues)

Passing Two dimensional array as argument to function

loops

Text file I/O

More User Defined Functions

Problem Description:

Using the 3 functions that you created, write a program to read a text file with set of initial angle and initial velocity and writes the result of calculation to another text file and on the screen.

Input file, call it trajin.txt : headings are not part of the file.

Initial angle (degrees) initial velocity (meter per second)

10                                            100

20                                            120

30                                            130

40                                            140

50                                            150

60                                            160

70                                            170

80                                            180

Output file, call it trajout.txt: heading are not part of the file but are part of the output on the screen.

Angle Velocity     MaxH    MaxV    TravTime

10            100            ????      ?????      ?????

20            120            ????       ????       ?????

And so on.

Requirement:

Must use functions for calculation .

Must use a function to read the file into an array (two dimensional) before calculating.

This is the three functions i've created:

----------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define GRAV 9.8
#define PI 3.14

int peak_height(double ivelocity, double itrajectory); /* Three functions for users to choose from */
int max_range(double ivelocity, double itrajectory);
int travel_time(double ivelocity, double itrajectory);

int main()
{
double ivelocity, itrajectory; /* Declaring variables */
int invalid_entry = 1;
int menu = 1;
double iheight, irange, itime;

printf("Please enter initial velocity."); /* Asking users for velocity and trajectory */
scanf("%lf", &ivelocity);
printf("please enter initial trajectory.");
scanf("%lf", &itrajectory);

while (invalid_entry < 3){ /* Using While loop to prevent wrong answer being inputed more then 3 times */

if (ivelocity < 20 || ivelocity > 800 || itrajectory < 5 || itrajectory > 80)
{
printf("INVALID ENTRY ");
invalid_entry = invalid_entry + 1;
printf("Please enter initial velocity.");
scanf("%f", &ivelocity);
printf("please enter initial trajectory.");
scanf("%f", &itrajectory);
}
else {
printf("what would you like the program to calculate?"); /* Asking user which formula to use */
printf(" 1 = peak height");
printf(" 2 = max range");
printf(" 3 = travel time");

printf(" Pleas make your selection now: ");
scanf("%d",&menu);

switch(menu) /* Using switch statement to choose what user whats to do. */
{
case 1: iheight = peak_height(ivelocity, itrajectory);
break;
case 2: irange = max_range(ivelocity, itrajectory);
break;
case 3: itime = travel_time(ivelocity, itrajectory);
break;
default: printf("Invalid option selected ");

}
}
invalid_entry = 4; /* Ending while loop */

}
}
/*----------------------------------------Peak Height Function----------------------------------------------------------------*/
int peak_height(double ivelocity, double itrajectory) /* Function for peak height */
{
double max_height_reached, ctrajectory; /* Declaring variable which will be sent back as an answer */

ctrajectory = (itrajectory * PI) / 180; /* Converting sin to radians */
max_height_reached = ((ivelocity * ivelocity) * (pow(sin(ctrajectory),2))) / (2*GRAV); /* Formula to calculate peak height */
printf("Maximum Height Reach is: %.2lf", max_height_reached); /* Print statement for max height answer */
return max_height_reached; /* Returning answering */
}

/*----------------------------------------Maximum Range Function----------------------------------------------------------------*/

int max_range(double ivelocity, double itrajectory) /* Function for maximum range */
{
double horizontal_range, ctrajectory; /* Declaring variable which will sent back as an answer */

ctrajectory = (itrajectory * PI) / 180; /* Converting sin to radians */
horizontal_range = ((ivelocity*ivelocity)*(sin(ctrajectory*2))/GRAV); /* Formula for horizontal range */
printf("Horizontal Range is: %.2lf", horizontal_range); /* Print statement for the answer */
return horizontal_range; /* Answer that will be return */
}

/*----------------------------------------Time of Flight Function----------------------------------------------------------------*/

int travel_time(double ivelocity, double itrajectory) /* Function for travel time */
{
double time_of_flight, ctrajectory; /* Declaring variable which will sent back as an answer */

ctrajectory = (itrajectory * PI) / 180; /* Converting sin to radians */
time_of_flight = ((2*ivelocity)*(sin(ctrajectory)) / GRAV) ; /* Formula for time of flight */
printf("Time of flight is: %.2lf", time_of_flight); /* print statement for the answer */
return time_of_flight; /* Answer that will be return */
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define GRAV 9.8
#define PI 3.14

/* Three functions for users to choose from */
int peak_height(double ivelocity, double itrajectory);
int max_range(double ivelocity, double itrajectory);
int travel_time(double ivelocity, double itrajectory);

//function to read file data
void readData(double arr[][5], int* n);
//function to calculate other 3 data
void calculate(double arr[][5], int n);
//method to write data into file
void writeDataAndPrint(double arr[][5], int n);

int main()
{
   //declaring 2-D array
   int n = 0;
   double data[30][5]; // max 30 interies

   readData(data, &n);

   calculate(data, n);

   writeDataAndPrint(data, n);

   return 0;
}
/*----------------------------------------Peak Height Function----------------------------------------------------------------*/
int peak_height(double ivelocity, double itrajectory) /* Function for peak height */
{
   double max_height_reached, ctrajectory; /* Declaring variable which will be sent back as an answer */

   ctrajectory = (itrajectory * PI) / 180; /* Converting sin to radians */
   max_height_reached = ((ivelocity * ivelocity) * (pow(sin(ctrajectory),2))) / (2*GRAV); /* Formula to calculate peak height */
   //printf("Maximum Height Reach is: %.2lf", max_height_reached); /* Print statement for max height answer */
   return max_height_reached; /* Returning answering */
}

/*----------------------------------------Maximum Range Function----------------------------------------------------------------*/

int max_range(double ivelocity, double itrajectory) /* Function for maximum range */
{
   double horizontal_range, ctrajectory; /* Declaring variable which will sent back as an answer */

   ctrajectory = (itrajectory * PI) / 180; /* Converting sin to radians */
   horizontal_range = ((ivelocity*ivelocity)*(sin(ctrajectory*2))/GRAV); /* Formula for horizontal range */
   //printf("Horizontal Range is: %.2lf", horizontal_range); /* Print statement for the answer */
   return horizontal_range; /* Answer that will be return */
}

/*----------------------------------------Time of Flight Function----------------------------------------------------------------*/

int travel_time(double ivelocity, double itrajectory) /* Function for travel time */
{
   double time_of_flight, ctrajectory; /* Declaring variable which will sent back as an answer */

   ctrajectory = (itrajectory * PI) / 180; /* Converting sin to radians */
   time_of_flight = ((2*ivelocity)*(sin(ctrajectory)) / GRAV) ; /* Formula for time of flight */
   //printf("Time of flight is: %.2lf", time_of_flight); /* print statement for the answer */
   return time_of_flight; /* Answer that will be return */
}

void readData(double data[][5], int* n){

   FILE *file;
   file = fopen("trajin.txt", "r");
   int i = 0;
   while(!feof(file)){
       fscanf(file, "%lf %lf", &data[i][0], &data[i][1]);
       i++;
   }

   *n = i;
   fclose(file);
}

void calculate(double data[][5], int n){

   int i=0;

   for(i=0; i<n; i++){
       //calculating peak height
       data[i][2] = peak_height(data[i][0], data[i][1]);
       //calculatting max_range
       data[i][3] = max_range(data[i][0], data[i][1]);
       //calculating travel time
       data[i][4] = travel_time(data[i][0], data[i][1]);
   }
}

void writeDataAndPrint(double data[][5], int n){

   FILE *file;
// char fileName[10];
   //printf("Enter ouput file name: ");
   //scanf("%s", fileName);

   file = fopen("output.txt", "w");
   int i = 0;
   while(i<n){
       printf("%.2lf %.2lf %.2lf %.2lf %.2lf ", data[i][0], data[i][1],data[i][2],data[i][3],data[i][4]);
       fprintf(file, "%.2lf %.2lf %.2lf %.2lf %.2lf ", data[i][0], data[i][1],data[i][2],data[i][3],data[i][4]);
       i++;
   }

   fclose(file);
}