/* Rodney Biggs COP3223 Summer 2005 */ /* prg2.c 06.14.05 */ /* Add functions and a loop to program 1 */ #include #include #define PI 3.14159 int root(), trig(); /* Declares the functions root() and trig() as integers */ /* The following lines ask the user to select from three choices. */ /* The program then calls the appropriate function based on the */ /* users input. */ int main(void) { int choice; /* Declares choice as an integer variable */ do /* Added for program 2 assignment */ { /* The following three choices have been put into a box to be more readable */ /* when redisplayed after the calculations. The 3rd choice was added for */ /* the program 2 assignment. */ printf("\n -----------------------------------------------------------"); printf("\n| Please choose an option: |\n"); printf("| |\n"); printf("| 1) Calculate the distance between two points in a plane |\n"); printf("| 2) Calculate the trigonometric function of an angle |\n"); printf("| 3) Exit this program |\n"); printf(" -----------------------------------------------------------\n"); scanf("%d", &choice); if (choice < 2) root(); else if (choice == 2) trig(); else break ; /* Added for program 2 assignment */ } while (choice < 3); /* Added for program 2 assignment */ return 0; } /* The following function will calculate the distance between two points. */ int root() { float x1, y1, x2, y2, distance; /* Defines variables as type float */ /* The following lines of code request the user to input 2 pairs of */ /* numbers. Those numbers are then printed onto the screen in paranthesis. */ /* The numbers are then ran through the distance formula to determine the */ /* distance between the two coordinate points. The resulting distance is */ /* then printed onto the screen with two decimal places. */ printf("\n Please enter any two coordinate pairs. (Ex. x y x y)\n"); scanf("%f%f%f%f", &x1, &y1, &x2, &y2); printf("\n(%.2f ,%.2f) (%.2f ,%.2f)\n", x1, y1, x2, y2); distance = sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))); printf("\nDistance = %.2f\n", distance); return 1; } /* The following lines of code will calculate the trigonometric function of an angle */ int trig() { float angle_deg, angle_rad; /* Defines variables */ float angle_sin, angle_cos, angle_tan; /* as type float */ /* The following lines of code ask the user to input an angle (in degrees) */ /* and then converts it into radians. The program then finds the sin/cos/tan */ /* of the radian measure of the angle and then prints the results. */ printf("\nPlease enter the angle measure, in degrees.\n"); scanf("%f", &angle_deg); angle_rad = angle_deg * PI / 180.0; printf("\nsin(%.1f) = %f\n", angle_deg, angle_sin = sin(angle_rad)); printf("\ncos(%.1f) = %f\n", angle_deg, angle_cos = cos(angle_rad)); printf("\ntan(%.1f) = %f\n", angle_deg, angle_tan = tan(angle_rad)); return 1; }