jweinst1
4/30/2020 - 8:11 PM

Calculates the average entropy / distance of a group of integers.

Calculates the average entropy / distance of a group of integers.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <limits.h>


double calc_entropy(size_t amount, ...)
{
    va_list args;
    size_t i;
    size_t j;
    int* buf = malloc(sizeof(int) * amount);
    int* iter = buf;
    int total = 0;
    const size_t to_iter = amount;
    va_start(args, amount);
    while (amount--) {
        *iter++ = va_arg(args, int);
    }
    va_end(args);
    // calculation
    double distances = 0.0;
    for (i=0; i < to_iter; i++) {
        for (j=0; j < i; j++) {
            printf("I %d J %d\n", buf[i], buf[j]);
            printf("DISTANCE %d\n", buf[i] - buf[j]);
            distances += 1.0f;
            total += abs(buf[i] - buf[j]);
        }
    }
    free(buf);
    return ((double)total) / distances; 
}

int main(void) {
    // Calculations for entropy/ average distance
    printf("Entropy is %f\n", calc_entropy(4, 1, 2, 3, 4));
    printf("Entropy is %f\n", calc_entropy(5, 100, 23, 65, 78, 3));
    printf("Entropy is %f\n", calc_entropy(5, 1110, 3, 6005, 53228, 400));
    return 0;
}