#include <stdio.h>
#include <math.h>   /* needed for ceil */
#include <string.h> /* needed for strlen */

void make_linspace(int a[], double start, double stop, int num) {
    /* Fills array a[] (in place) with linearly spaced values just like np.linspace in NumPy (Python) */
    double spacing = (stop-start)/(num-1);
    int i;
    for (i=0; i<num; i++){
        a[i] = start + i*spacing;
    }
}

void make_and_print_msgs(int n_proc, int msglength)
{
    /* Create a string called msg of length msglength + 1 (for the null character '\0') */
    char msg[msglength+1];
    int i;
    printf("msg intended to be:    ");
    for (i=0; i<msglength; i++) {
        msg[i] = 'a';
        printf("%c",msg[i]);
    }
    msg[i] = '\0';

    /* Print message to screen as a string and fine strlen(msg) and sizeof(msg) */
    printf("\n");
    printf("msg printed as string: %s\n", msg);
    printf("strlen(msg): %d\n", strlen(msg));
    printf("sizeof(msg): %d\n\n", sizeof(msg));

}

int main(int argc, char *argv[])
{
    int n_proc = 2;

    /* Create an array containing the lengths of strings to be printed (In this case, data_length should be {0, 2, 4, 6, 8} */
    int start = 0;
    int stop_range = 10;    /* the stop value if we are using range() */
    int step = 2;             /* spacing between the integers in the output of range() */
    int stop = stop_range - step;    /* the stop value if we are using linspace() */
    int npoints = (int) ceil( ((double)stop_range - (double)start) / (double)step );  /*  number of elements in the list produced by range(start, stop_range, step)  */

    int data_length[npoints];   /* 1D array of string lengths (# of non-null chars in each str) */
    make_linspace(data_length, start, stop, npoints);
    int i;


    /* For each length, call on make_and_print_msgs to make a string of that length (plus '\0') and then print to stdout */
    printf("   i    data_length[i]\n--------------------\n");
    for (i=0; i<npoints; i++) {
        printf("%4d %7d\n", i, data_length[i]);
        make_and_print_msgs(n_proc, data_length[i]);
    }
    return 0;
}
