NAG Library Manual, Mark 29
```/* nag_stat_moving_average (g01wac) Example Program.
*
* Copyright 2023 Numerical Algorithms Group.
*
* Mark 29.0, 2023.
*/
/* Pre-processor includes */
#include <nag.h>
#include <stdio.h>

int main(void) {
/* Integer scalar and array declarations */
Integer i, ierr, lrcomm, m, nb, offset, pn, nsummaries;
Integer exit_status = 0;

/* NAG structures and types */
NagError fail;
Nag_Weightstype iwt;
Nag_Boolean want_sd;

/* Double scalar and array declarations */
double *rcomm = 0, *rmean = 0, *rsd = 0, *x = 0, *wt = 0;

/* Character scalar and array declarations */
char ciwt, cwant_sd;

/* Initialize the error structure */
INIT_FAIL(fail);

printf("nag_stat_moving_average (g01wac) Example Program Results\n\n");

/* Skip heading in data file */
scanf("%*[^\n] ");

/* Read in the problem size */
scanf("%39s%" NAG_IFMT "%*[^\n] ", ciwt, &m);
iwt = (Nag_Weightstype)nag_enum_name_to_value(ciwt);

/* Read in a flag indicating whether we want the standard deviations */
scanf("%39s%*[^\n] ", cwant_sd);
want_sd = (Nag_Boolean)nag_enum_name_to_value(cwant_sd);

/* Initial handling of weights */
if (iwt == Nag_WeightWindow) {
/* Each observation in the rolling window has its own weight */
if (!(wt = NAG_ALLOC(m, double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
for (i = 0; i < m; i++) {
scanf("%lf", &wt[i]);
}
scanf("%*[^\n] ");
}

/* Allocate memory for the communication array */
lrcomm = 2 * m + 20;
if (!(rcomm = NAG_ALLOC(lrcomm, double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}

/* Print some titles */
if (want_sd) {
printf("                               Standard\n");
printf("  Interval         Mean        Deviation\n");
printf(" ---------------------------------------\n");
} else {
printf("  Interval         Mean  \n");
printf(" ------------------------\n");
}

/* Loop over each block of data */
for (pn = 0;;) {
/* Read in the number of observations in this block */
ierr = scanf("%" NAG_IFMT, &nb);
if (ierr == EOF || ierr < 1)
break;
scanf("%*[^\n] ");

/* Reallocate X to the required size */
NAG_FREE(x);
if (!(x = NAG_ALLOC(nb, double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}

/* Read in the data for this block */
for (i = 0; i < nb; i++) {
scanf("%lf", &x[i]);
}
scanf("%*[^\n] ");

if (iwt == Nag_WeightObs) {
/* User supplied weights are present */

/* Reallocate WT to the required size */
NAG_FREE(wt);
if (!(wt = NAG_ALLOC(nb, double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}

/* Read in the weights for this block */
for (i = 0; i < nb; i++) {
scanf("%lf", &wt[i]);
}
scanf("%*[^\n] ");
}

/* Calculate the number of summaries we can produce */
nsummaries = MAX(0, nb + MIN(0, pn - m + 1));

/* Reallocate the output arrays */
NAG_FREE(rmean);
if (!(rmean = NAG_ALLOC(nsummaries, double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
if (want_sd) {
NAG_FREE(rsd);
if (!(rsd = NAG_ALLOC(nsummaries, double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
}

/* nag_stat_moving_average (g01wac):
Calculate the moving average (and optionally the standard deviation)
for this block of data
*/
nag_stat_moving_average(m, nb, x, iwt, wt, &pn, rmean, rsd, rcomm, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_stat_moving_average (g01wac).\n%s\n",
fail.message);
exit_status = -1;
if (fail.code != NW_POTENTIAL_PROBLEM)
goto END;
}

/* Number of results printed so far */
offset = MAX(1, pn - nb - m + 2);

/* Display the results for this block of data */
if (want_sd) {
for (i = 0; i < nsummaries; i++) {
printf(" [%3" NAG_IFMT ",%3" NAG_IFMT "]    "
"%10.1f    %10.1f\n",
i + offset, i + m + offset - 1, rmean[i], rsd[i]);
}
} else {
for (i = 0; i < nsummaries; i++) {
printf(" [%3" NAG_IFMT ",%3" NAG_IFMT "]    %10.1f\n", i + offset,
i + m + offset - 1, rmean[i]);
}
}
}

printf("\n");
printf(" Total number of observations : %3" NAG_IFMT "\n", pn);
printf(" Length of window             : %3" NAG_IFMT "\n", m);

END:
NAG_FREE(x);
NAG_FREE(wt);
NAG_FREE(rmean);
NAG_FREE(rsd);
NAG_FREE(rcomm);

return (exit_status);
}
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.56418777,"math_prob":0.9825533,"size":4011,"snap":"2023-14-2023-23","text_gpt3_token_len":1234,"char_repetition_ratio":0.13201897,"word_repetition_ratio":0.20919175,"special_character_ratio":0.3966592,"punctuation_ratio":0.22174536,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98935086,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-05-29T16:17:24Z\",\"WARC-Record-ID\":\"<urn:uuid:fc343611-8622-4944-ab36-a85adb1a1e4d>\",\"Content-Length\":\"37234\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:38f3ccfb-1925-4c29-89bc-c7d386953796>\",\"WARC-Concurrent-To\":\"<urn:uuid:b719072e-7971-4ce4-9633-cea42c072fff>\",\"WARC-IP-Address\":\"78.129.168.4\",\"WARC-Target-URI\":\"https://www.nag.com/numeric/nl/nagdoc_latest/examples/source/g01wace.c.html\",\"WARC-Payload-Digest\":\"sha1:2SJPCFRZWRTQKPPJ2YJRFZQ4JSHULX2R\",\"WARC-Block-Digest\":\"sha1:ZBFSKMOVRKR3BNBSUISRFCRBVNKZKORU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224644867.89_warc_CC-MAIN-20230529141542-20230529171542-00331.warc.gz\"}"}