/*  This program is used to generate all possible pi vectors with no combination
    or permutation.  */
#include <stdio.h>
#define LEN 8  /* This is the number of elements in the pi vector  */
#define MAX 20 /* This is the 1/ the interval--for 0.1 it is 10, for 0.05 it is
                  20 etc...*/
int pi[LEN];

FILE *fout; 

main () {
  
  void write_pi(), gen_pi(), copy_pi();
  int sumvec(), i;
  fout=fopen("/mit/smmadana/value.dat","w");

  for(i=0;i<LEN;i++)
    pi[i]=0;
  gen_pi(pi,0,MAX,0); 
  fclose(fout);	
}
/*****************************************************************************/
void gen_pi (pi, slot, value, total)
int pi[], slot, value, total; {
  int i,j,k,diff, savetot, temp[LEN], new=0;

  if((slot!=LEN-1) && value) {
    for(i=1;i<=value;i++) {
      copy_pi(pi,temp);
      savetot=total;
      for(j=slot;j<LEN;j++) {
        if((sumvec(pi,j)+i)<MAX) { 
          pi[j]=i;
          total=sumvec(pi,j);
          gen_pi(pi, j+1, i-1, total);
        }
        else {
          if((total+i)<=MAX) {
            new=1;
            diff=i-pi[j];
            pi[j]=i;
            for(k=LEN-1;k>j;k--) {
              while((diff>0) && (pi[k]>0)) {
                pi[k]-=1;
                diff-=1;
              }
            }
          }
          if((sumvec(pi,j)>=MAX) && new) {
            write_pi(pi);
            new=0;
            total=sumvec(pi,j);
            gen_pi(pi, j+1, i-1, total);
          }
        } 
      }
      total=savetot;
      copy_pi(temp,pi);
    }
  }
}

/*****************************************************************************/
void write_pi(pi)
int pi[]; {

int i;

for (i=0;i<LEN;i++) {
  printf("%d ",pi[i]);
  fprintf(fout, "  %1.3f", (1./((float)MAX)) * (float) pi[i]);
}
printf("\n");
fprintf(fout,"\n");
}
/*****************************************************************************/
int sumvec(vec, i)
int vec[], i; {
  int j, total=0;

  for(j=0;j<=i;j++)
    total+=vec[j];
  
  return(total);
}
/*****************************************************************************/
void copy_pi(old, new)
int old[], new[]; {
  int i;

  for(i=0;i<LEN;i++)
    new[i]=old[i];
}
