//========================================================================= #define PROGRAMMER "SOLUTIONS, NoFrills" #define PROG_CODE "soln" #define COURSE "ECE-1021" #define YEAR (2003) #define TERM "Fall" #define SECTION (0) #define ASSIGNMENT "HW 1C" #define REVISION (0) #define TITLE "Mystery Program" #define SUBTITLE "Extra Credit" #define EMAIL "wbahn@eas.uccs.edu" #define FILENAME "01c0soln.c" //========================================================================= // PROBLEM: // // Implement the algorithm pseudocode given in the homework assignment and // determine what function the algorithm performs. // // PSEUDOCODE: // // Given as part of the assignment // // ANSWER TO HOMEWORK QUESTION: // // After implementing the algorithm and testing it on several values // it became apparent that the output is the square root of the input. //== INCLUDE FILES ======================================================== #include // printf(), scanf() //== FUNCTION PROTOTYPES ================================================== void PrintHeader(void); //== MAIN FUNCTION ======================================================== int main(void) { double d,s,x,y,z; int j,k,n; PrintHeader(); printf("\n"); // Print a blank line //1) TASK: Get a number from the user printf("Enter a non-negative floating point value: "); scanf("%lf", &x); //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 = j + 1; } //5) TASK: Generate output one digit at a time s = 0.0; k = 0; while ( k < 16) { n = 0; do { d = (100.0*s + 5.0*(2*n+1))*z; y = y - d; n = n + 1; } while (!(y < 0)); y = y + d; n = n - 1; z = z / 100.0; s = 10.0*s + n; if(k>0) { k = k + 1; printf("%i", n); } else { if( n > 0) { k = k + 1; printf("%i", n); } else { if(j<0) printf("%i", n); } } if(j == 0) printf("."); j = j -1; } 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; }