/* 
 * File:   main.c
 * Author: J Dean Brock
 *
 * Created for CSCI 255 lab
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdlib.h>
#include <malloc.h>

// Here's the prototype for genHistogram
float genHistogram(
    const int values[],         // promise not to change any!
    int size,
    int histogram[11],
    int *lowCount,
    int *highCount) ;

int main(int argc, char** argv) {

    int size ;
    int *sample ;
    int histogram[10] ;
    int tooLow, tooHigh ;
    float justAverage ;
    
    srand(time(NULL)) ;
    size = 64 + rand() % 16 ;
    printf("sample size is %d\n", size) ;
    // This is how your create "dynamic" arrays in C
    sample = (int *)malloc(size * sizeof(int)) ;
    printf("samples located at %lX\n\n", (unsigned long)(void *)&sample) ;
    // Stores random numbers from -3 to 13
    // Hmmm....  This looks like C99
    for (int i=0; i<size; ++i) {
        sample[i] = rand()%5 + rand()%5 + rand()%5 + rand()%5 - 3 ;
    }
    puts("Samples") ;
    for (int i=0; i<size; ++i) {
        printf("S[%2d] == %2d\n", i, sample[i]) ;
    }
    justAverage = genHistogram(sample, size, histogram, &tooLow, &tooHigh) ;
    puts("") ;
    puts("Results") ;
    printf("Too low:  %2d\n", tooLow) ;
    printf("Too high: %2d\n", tooHigh) ;
    for (int i=0; i<=10; ++i) {
        printf("H[%2d] == %2d\n", i, histogram[i]) ;
    }
    printf("Average:  %4.2f", justAverage) ;
    
    return (EXIT_SUCCESS);
}

// This is the routine we will work on
float genHistogram(const int V[], int size, int H[11], int *lowCount, int *highCount) {
    float vSum = 0 ;
    for(int i=0; i<=10; ++i) {
        H[i] = 0 ;
    }
    *lowCount = 0 ;
    *highCount = 0 ;
    for(int i=0; i<size; ++i) {
        int x = V[i] ;
        if (x < 0 ) {
            ++*lowCount ;
        } else if (x > 10) {
            ++*highCount ;
        } else {
            vSum += x;
            ++H[x] ;
        }
    }
    return vSum/size ;
}
