//========================================================================= #define PROGRAMMER "SOLUTIONS, NoFrills" #define PROG_CODE "soln" #define COURSE "ECE-1021" #define YEAR (2004) #define TERM "Spring" #define SECTION (0) #define ASSIGNMENT "HW #4A" #define REVISION (0) #define TITLE "Mystery Algorithm" #define SUBTITLE "Function: Square Root" #define EMAIL "ece1021@eas.uccs.edu" #define FILENAME "04a0soln.c" //========================================================================= // PROBLEM // // This is the pseudocode for a classic computational algorithm. // It is very complete meaning that you should be able to code it // directly from the pseudocode. // Once it is coded, run it and try to determine what the functional // relationship is between the input value and the output value. // PSEUDOCODE // // 1) TASK: Get a number and number of sig figs from the user // 2) TASK: Print the output header // 3) TASK: Initialize working value // 4) TASK: Initialize certain variables based on size of input // 5) TASK: Generate output one digit at a time // 5A) TASK: Determine next digit // 5B) TASK: Update Partial Results (z and s) // 5C) TASK: Output digit if significant and/or needed placeholder // 5D) TASK: Output decimal point if appropriate // RUNTIME RESULTS // // INPUT SIG OUTPUT // 2.0 3 1.41 // 20000 3 141 // 0.002 3 0.0447 // 100 3 10.0 // 90000 3 300. // // The function appears to be the square root function. //== INCLUDE FILES ======================================================== #include // printf() //== FUNCTION PROTOTYPES ================================================== void PrintHeader(void); //== MAIN FUNCTION ======================================================== int main(void) { double x; // value entered by user int sig; // number of significant figures to print double y; // working value double z; int j; double s; int k; double d; int n; PrintHeader(); printf("\n"); // Print a blank line // 1) TASK: Get a number and number of sig figs from the user printf("Enter a non-negative floating point value: "); scanf("%lf", &x); printf("Enter number of significant digits to produce: "); scanf("%i", &sig); // 2) TASK: Print the output header printf("The output value is "); // 3) TASK: Initialize working value y = 5.0*x; // 4) TASK: Initialize certain variables based on size of input z = 1.0; j = 0; while( (y - 100.0*z) >= 0.0 ) { z = 100.0*z; j++; } // 5) TASK: Generate output one digit at a time s = 0.0; k = 0; while( k < sig ) { n = 0; do { d = (100.0*s + 5.0*(2*n + 1))*z; y = y - d; n = n + 1; } while(y >= 0); // UNTIL ( y < 0 ) y = y + d; n--; z = z / 100.0; s = 10*s + n; if( k > 0) { k++; printf("%i", n); } else { if( n > 0) { k++; printf("%i", n); } else { if( j < 0 ) printf("%i", n); } } if( 0 == j ) printf("."); j--; } return(0); } //== SUPPORT FUNCTIONS ==================================================== void PrintHeader(void) { printf("========================================" "=======================================\n"); printf("Course....... %s-%i (%s %i)\n", COURSE, SECTION, TERM, YEAR); printf("Programmer... %s (%s)\n", PROGRAMMER, PROG_CODE); printf("Assignment... %s (Rev %i) (Source Code in %s)\n", ASSIGNMENT, REVISION, FILENAME); printf("Description.. %s\n", TITLE); printf(" %s\n", SUBTITLE); printf("========================================" "=======================================\n"); return; }