#include // used by all 3 #include // Used by square root only function used from it is pow() in square root //and doesn't really need to be here #include // used by Random numbers #include // used by time int main() { menu(); } int menu() { int select; double num; char flag; printf("1: Square Root\n"); printf("2: Random Numbers\n"); printf("3: Display Time/Date\n"); printf("4: Quit\n"); printf("What function would you like to use?: "); scanf("%d", &select); switch (select) { case 1: printf("Enter a number greater than 0: "); scanf("%lf", &num); sqroot(num); break; case 2: randn(); break; case 3: distime(); break; //any additional menu entries can be placed here and their numbers changed accordingly //conforming to the format already specified here. case 4: return(0); default: printf("Invalid Selection. Try again?(y/n): "); scanf("%c", &flag); if (flag == 'y') menu(); else return(0); //Test here to keep from infinitely looping when an input of the wrong type is used //on the menu. } } int sqroot(double num) { double lastNum = 0; double errorMargin = 0.000001, test = 1; //errorMargin: The precision to which the approximation of the square root //is to be held to int i = 0; while (num <= 0) { printf("Please enter a number greater than 0.\n"); printf("Enter: "); scanf("%lf", &num); } lastNum = num; while ((test * 2) < num) { test += test; //using the 2^n closest to the test number as the initial guess for Newton's method } while ((lastNum-test) > errorMargin) { lastNum = test; test = (test - ((pow(test, 2) - num)/(2 * test))); //Apply Newton's method until the difference between the previous approximation //and the current one is less than or equal to the errorMargin } printf("The square root of %f is: %f\n", num, test); menu(); } int randn() { int num; srand(time(NULL)); //Seed rand() with the current epoch time //better seeding strategies can be used here to make the number more random. num = rand(); //Should be a perfectly random number so long as it is not run twice in the same second. printf("Your random number is: %d\n", num); menu(); //To make this number random between 1 and some max value for say a dice roller, //use the modulus operator (%) on the num variable for the max number you want and //then add 1 to the number to make it 1-max rather than 0-(max-1) } int distime() { time_t now; time(&now); printf("%s", ctime(&now)); //give ctime the struct filled by time(&now) to output the time and date //in a nicely formatted string menu(); } //Place the definitions of any additional functions here. //should include their name, return type and all other things needed //to run them.