//========================================================================= #define PROGRAMMER "INSTRUCTOR, The" #define PROG_CODE "INST" #define COURSE "ECE-1021" #define YEAR (2004) #define TERM "Summer" #define SECTION (0) #define ASSIGNMENT "Example Code for HW#7" #define REVISION (0) #define TITLE "Data Analysis, Sorting, Searching" #define SUBTITLE "Last Modified on 11 JUL 04" #define EMAIL "emailname@domain.com" #define FILENAME "randgen.c" //========================================================================= //========================================================================= // PROBLEM //========================================================================= // // This program accepts a command line argument consisting of the name of // the data file that the User wishes to create. If no filename is given, // the User is prompted for a name. // // The User is then asked to indicate how many data points they wish to // create and what the minimum and maximum values are. // // The program then creates random values and writes them to the file // per the entered constraints. // // This program can be used as demo code for the following topics: // // Processing Command Line Arguments. // Working with ASCII test files. // Detecting the end of a data file. // Allocating large blocks of memory from the heap. // //========================================================================= // PSEUDOCODE //========================================================================= // // 1) TASK: Get filename from User. // 1.1) TASK: Detect if filename provided on command line // 1.2) IF: Filename on command line // 1.2.1) TASK: Copy from command line to filename variable. // 1.3) ELSE: // 1.3.1) TASK: Prompt User and get filename from keyboard. // 2) TASK: Prompt for and get constraints from User. // 2.1) TASK: Get Number of data points. // 2.2) TASK: Get Minimum value. // 2.3) TASK: Get maximum value. // 3) TASK: Create data file. // 3.1) TASK: Open and verify text file for write operations. // 3.2) TASK: Create sufficient memory to hold data. // 3.3) TASK: Create data and store in memory. // 3.4) TASK: Dump contents of memory to file. // 3.5) TASK: Close file and free allocated memory // 4) TASK: Read data file back into memory. // 4.1) TASK: Open and verify text file for read operations. // 4.2) TASK: Make one pass to count data times and find min/max. // 4.3) TASK: Create sufficient memory to hold data. // 4.4) TASK: Read in data and store in memory. // 4.5) TASK: Close file // 5) TASK: Compute and display RMS value of data. // 5.1) TASK: Compute the sum of the squares of the data. // 5.2) TASK: Compute the root of the mean. // 5.3) TASK: Display result. // 5.4) TASK: Free allocated memory. // //========================================================================= // DEVIATIONS FROM SUBMITTED PSEUDOCODE //========================================================================= // //========================================================================= // WAIVED COMPILER WARNINGS //========================================================================= // // Linker Warning: No module definition file specified: using defaults // Reason for Waiver: Can't suppress. Does not have adverse impact. // //========================================================================= //========================================================================= // CODE SECTION //========================================================================= //========================================================================= //== INCLUDE FILES ======================================================== #include // printf(), scanf(), fgets() // FILE, fopen(), fclose(), rewind() // fprintf(), fscanf() #include // fgets(), strcpy(), strlen() #include // exit(), malloc(), free() // rand(), randomize(), RAND_MAX #include // randomize() #include // sqrt() //== MACRO DEFINITIONS ==================================================== #define FALSE (0) #define TRUE (!FALSE) #define BLANKLINE printf("\n") #define NUL ('\0') // Null character for string termination //== EXIT CODES =========================================================== #define NORMAL_EXIT (0) #define FAIL_BAD_FILENAME (1) #define FAIL_WRITE_OPEN (2) #define FAIL_READ_OPEN (3) #define FAIL_NO_DATA (4) //== FUNCTION PROTOTYPES (for Primary Functions) ========================== void PrintHeader(void); double rand_dbl(double min, double max); //== MAIN FUNCTION ======================================================== int main(int argc, char *argv[]) { char filename[81]; int i; int points; double min, max; double rms; double *data; FILE *fp; PrintHeader(); BLANKLINE; // 1) TASK: Get filename from User. if(argc > 1) strcpy(filename, argv[1]); else { printf("File to create (including extension): "); fgets(filename, 81, stdin); i = strlen(filename); if('\n' == filename[i-1]) // CR/LF at end of string. filename[i-1] = NUL; else // Didn't read entire string. { printf("Error in filename - aborting.\n"); exit(FAIL_BAD_FILENAME); } } // 2) TASK: Prompt for and get constraints from User. printf("Enter number of data points to create: "); scanf("%i", &points); printf("Enter the minimum data value: "); scanf("%lf", &min); printf("Enter the maximum data value: "); scanf("%lf", &max); // 3) TASK: Create data file. // 3.1) TASK: Open and verify text file for write operations. fp = fopen(filename, "wt"); if(NULL == fp) { printf("Error opening <%s> for writing - aborting.\n", filename); exit(FAIL_WRITE_OPEN); } // 3.2) TASK: Create sufficient memory to hold data. data = (double *) malloc(points*sizeof(double)); // 3.3) TASK: Create data and store in memory. randomize(); for(i = 0; i < points; i++) data[i] = rand_dbl(min, max); // 3.4) TASK: Dump contents of memory to file. for(i = 0; i < points; i++) fprintf(fp, "%.6f\n", data[i]); // 3.5) TASK: Close file and free allocated memory fclose(fp); free(data); // 4) TASK: Read data file back into memory. // 4.1) TASK: Open and verify text file for read operations. fp = fopen(filename, "rt"); if(NULL == fp) { printf("Error opening <%s> for reading - aborting.\n", filename); exit(FAIL_READ_OPEN); } // 4.2) TASK: Make one pass to count data items and find min/max. points = 0; if(1 == fscanf(fp, "%lf", &rms)) { min = max = rms; do { points++; if(rms < min) min = rms; if(rms > max) max = rms; } while(1 == fscanf(fp, "%lf", &rms)); // rms used as scratch space } else { printf("No data retrieved from file - aborting.\n"); exit(FAIL_NO_DATA); } printf("File <%s> contains %i data points.\n", filename, points); printf("Minimum = %.6f\n", min); printf("Maximum = %.6f\n", max); // 4.3) TASK: Create sufficient memory to hold data. data = (double *) malloc(points*sizeof(double)); // 4.4) TASK: Read in data and store in memory. rewind(fp); for(i = 0; 1 == fscanf(fp, "%lf", &data[i]); i++); // 4.5) TASK: Close file fclose(fp); // 5) TASK: Compute and display RMS value of data. // 5.1) TASK: Compute the sum of the squares of the data. for(i = 0, rms = 0.0; i < points; i++) rms += data[i]*data[i]; // 5.2) TASK: Compute the root of the mean. rms = sqrt(rms /(double) points); // 5.3) TASK: Display result. printf("The rms value of the data is %f.\n", rms); // 5.4) TASK: Free allocated memory. free(data); return(NORMAL_EXIT); } //========================================================================= // PRIMARY FUNCTIONS (functions called directly by main() ) //========================================================================= //== FUNCTION PROTOTYPES (for Support Functions) ========================== int printc(char c, int n); //== PRIMARY FUNCTIONS ==================================================== void PrintHeader(void) { printc('=', 79); 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); printc('=', 79); printf("\n"); return; } double rand_dbl(double min, double max) { return( min + (max-min)*( (double) rand() / (double) RAND_MAX ) ); } //========================================================================= // SUPPORT FUNCTIONS (functions not called directly by main() ) //========================================================================= int printc(char c, int n) { while(0 < n--) printf("%c", c); return(n); }