Advertisement
Guest User

Untitled

a guest
Nov 24th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.13 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. double const STDDEV_SCALAR = 1.5;
  7.  
  8. int *load_readings(char fileName[], int *numReadings);
  9. void lumens_std_def(int *listMemory, int numElements, double *average, double *stdDev);
  10. int get_outliers(int *firstReadingList, int numElements, int *secondReadingList, double *average, double *stdDev, double devMultiplier);
  11. void write_outliers(char outputName[], int *outlierList, int numElements);
  12.  
  13. /*        
  14.  * argc - # of command line arguments
  15.  * argv - Is the actual command line argument
  16.  *
  17.  * In this case, argv[1] will be our input file name
  18.  * and argv[2] will be the output file name.
  19.  */
  20. int main(int argc, char *argv[]) {
  21.     if (argc != 3) {
  22.         printf("Invalid number of command line parameters, exiting...\n");
  23.         exit(1);
  24.     }
  25.      
  26.     /*
  27.      * Program specifies that the first function
  28.      * should have a pointer to the number of readings,
  29.      * however, the other functions simply need an int,
  30.      * so I just take the value from the pointer and set it
  31.      * to a new variable
  32.      */
  33.     int *readingsPointer = malloc(sizeof *readingsPointer);
  34.     int *dataPointer = load_readings(argv[1], readingsPointer);
  35.     int numReadings = *readingsPointer;
  36.    
  37.     //Free memory allocated to the temporary pointer
  38.     free(readingsPointer);
  39.    
  40.     /*
  41.      * Allocate memory to the 'average and standard
  42.      * deviation pointers.
  43.      */
  44.     double *average = malloc(sizeof *average); 
  45.     double *stdDev = malloc(sizeof *stdDev);
  46.     lumens_std_def(dataPointer, numReadings, average, stdDev);
  47.    
  48.     int *outlierList = malloc(sizeof *outlierList);
  49.    
  50.     /*
  51.      * Why would you pass a constant to a function
  52.      * when the function can access it?
  53.      */
  54.     int numOutliers = get_outliers(dataPointer, numReadings, outlierList, average, stdDev, STDDEV_SCALAR);
  55.    
  56.  
  57.    
  58.     write_outliers(argv[2], outlierList, numOutliers);
  59.     //printf("Readings = %d\n", numReadings);
  60.     //printf("Average = %0.2f\n", *average);
  61.     //printf("Standard Deviation = %0.2f\n", *stdDev);
  62.     //printf("Outliers = %d\n", numOutliers);
  63.     return 0;
  64. }
  65.  
  66. int *load_readings(char fileName[], int *size) {
  67.     FILE *in = NULL;
  68.     in = fopen(fileName, "r");
  69.    
  70.     //ERROR CHECK: Check to see if there is a file name.
  71.     if (in == NULL) {
  72.         printf("Unable to open input file \"%s\", exiting...\n", fileName);
  73.         fclose(in);
  74.         exit(4);
  75.     }
  76.    
  77.     /*
  78.      * Go to the end of the file to get the size
  79.      * Divide that size by 4 to get number of elements
  80.      * Go back to beginning with 'SEEK_SET'
  81.      * It's like rewind(), but better.
  82.      */
  83.     fseek(in, 0L, SEEK_END);
  84.     size[0] = ftell(in) / 4;
  85.     fseek(in, 0L, SEEK_SET);
  86.    
  87.     //ERROR CHECK: Check file size
  88.     if (size == 0) {
  89.         printf("Input file contains no data, exiting...\n");
  90.         fclose(in);
  91.         exit(2);
  92.     }
  93.    
  94.     //Allocate memory to the pointer 'data'
  95.     int *data = (int *) calloc(size[0], sizeof(int));
  96.    
  97.     //ERROR CHECK: Check to see if enough memory was allocated
  98.     if (data == NULL) {
  99.         printf("Unable to allocated needed memory, exiting...\n");
  100.         fclose(in);
  101.         exit(3);
  102.     }
  103.    
  104.     int i = 0;
  105.     int *memoryAt;
  106.     for (i; i < size[0]; i++) {
  107.         memoryAt = (data + i);
  108.         fscanf(in, "%d", memoryAt);
  109.     }
  110.     //free(memoryAt);
  111.     fclose(in);
  112.    
  113.     return data;
  114. }
  115.  
  116.  
  117. void lumens_std_def(int *data, int numElements, double *average, double *stdDev) {
  118.     int i = 0;
  119.     int sum = 0;
  120.    
  121.     /* I just use a temporary address to clean up the code */
  122.     int *tempAddress;
  123.     for (i; i < numElements; i++) {
  124.         tempAddress = (data + i);
  125.         sum += *tempAddress;
  126.     }
  127.     *average = (double) sum / (double) numElements;
  128.    
  129.     /*
  130.      * Reset sum back to 0 for use again. No need
  131.      * in wasting memory making new variables.
  132.      */
  133.     sum = 0;
  134.     for (i = 0; i < numElements; i++) {
  135.         tempAddress = (data + i);
  136.         sum = (double) sum + pow((*tempAddress - *average), 2);
  137.     }
  138.    
  139.     *stdDev = sqrt((double) sum / (double) numElements);
  140. }
  141.  
  142. int get_outliers(int *firstReadingList, int numElements, int *secondReadingList, double *average, double *stdDev, double devMultiplier) {
  143.         int i = 0;
  144.         int *tempAddress, *tempOutlier;
  145.         int numOutliers = 0;
  146.         double plusOutlier = *average + (*stdDev * devMultiplier);
  147.         double minusOutlier = *average - (*stdDev * devMultiplier);
  148.        
  149.         /*
  150.          * Loop through the elements, and if
  151.          * the current value is either
  152.          * average +- (std. dev. * multiplier)
  153.          * then it is considered an 'outlier'
  154.          */
  155.         for (i; i < numElements; i++) {
  156.             tempAddress = firstReadingList++;
  157.             if ((double) *tempAddress > plusOutlier || (double) *tempAddress < minusOutlier) {
  158.                 *secondReadingList++ = *tempAddress;
  159.                
  160.                 numOutliers++;
  161.                 printf("%d ", *tempAddress);
  162.             }
  163.         }
  164.        
  165.         return numOutliers;
  166. }
  167.  
  168. void write_outliers(char outputName[], int *outlierList, int numElements) {
  169.     printf("\n");
  170.     //Create output file pointer and open it
  171.     FILE *out;
  172.     out = fopen(outputName, "w");
  173.    
  174.     if (out == NULL) {
  175.         printf("Unable to open out file '%s', exiting...\n", outputName);
  176.         exit(5);
  177.     }
  178.    
  179.     int i = 0;
  180.     int *tempAddress;
  181.    
  182.     //Loop through and write each element in the outlier list
  183.     for (i; i < numElements; i++) {
  184.         tempAddress = (outlierList + i);
  185.         printf("%d ", *tempAddress);
  186.         fprintf(out, "%d ", *tempAddress);
  187.     }
  188.     fclose(out);
  189.     printf("\n");
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement