#include <nagdmc.h>
/*
handle_info() prints to screen information based on the value of the info
parameter.
*/
int
handle_info(const char func[], int info);
/*
imputed_values() is the function described in 'Explanatory Code' in
nagdmc_impute_simp.pdf.
*/
void
imputed_values(long nvar, double data[], long nrepl, long indexes[]);
int
main(void) {
const char file[] = {"continuous.dat"};
long rec1 = 0;
long nvar = 8;
long nrec = 200;
long dblk = 200;
double *data = 0;
long *ncat = 0;
long maxcat = 0;
long *cat = 0;
double mval = -999.0;
double *ival = 0;
long nrepl = 0;
int info = 0;
long *indexes = 0;
FILE *fp = 0;
long i, j;
/*
Memory allocation.
*/
if (!(data = (double *)malloc(dblk*nvar * sizeof(double)))) {
printf(" Memory allocation failure.\n\n");
return 2;
}
if (!(ival = (double *)malloc(nvar * sizeof(double)))) {
printf(" Memory allocation failure.\n\n");
free(data);
return 2;
}
/*
Read data values.
*/
if ((fp = fopen(file,"r")) == 0) {
printf(" Data file named %s was not found.\n\n",file);
return 2;
}
for (i=0; i<dblk; ++i) {
for (j=0; j<nvar; ++j)
fscanf(fp,"%lf ",&data[i*nvar+j]);
}
fclose(fp);
/*
Call data imputation function.
*/
indexes = nagdmc_impute_simp(rec1,nvar,nrec,dblk,data,ncat,maxcat,cat,mval,
ival,&nrepl,&info);
if (handle_info("nagdmc_impute_simp",info)) {
free(data);
free(ival);
nagdmc_free_impute(indexes);
return 2;
}
/*
Explanatory code from function document.
*/
imputed_values(nvar,data,nrepl,indexes);
/*
Return allocated memory to operating system.
*/
free(data);
free(ival);
nagdmc_free_impute(indexes);
return 0;
}
void
imputed_values(long nvar, double data[], long nrepl, long indexes[]) {
long i, j, k;
#define MISSING_ROW(I) indexes[I]
#define MISSING_COL(I) indexes[I+nrepl]
#define DATA(I,J) data[(I)*nvar+J]
printf("\n\tRow \tCol \tValue\n");
for (i=0; i<nrepl; ++i) {
j = MISSING_ROW(i);
k = MISSING_COL(i);
printf("\t%-4li\t%-4li\t%-8.4f\n",j,k,DATA(j,k));
}
}
int
handle_info(const char func[], int info) {
if (info == -999)
{
printf(" Invalid licence, please contact NAG.\n\n");
return 2;
}
else if (info > 0)
{
printf(" Error code %i from %s.\n\n",info,func);
return 1;
}
else if (info < 0)
printf (" Information code %i from %s.\n\n",info,func);
return 0;
}
syntax highlighted by Code2HTML, v. 0.8.11