/* This is some simple c code that does a percent of control calculation off of some randomly generated numbers. Compare it with some of the other programming languages and techniques for doing this calculation. Some straight forward array subscripting with a couple of linear equations so it should be easy to compare with the other examples. The code can be compiled on Jacob Navia's c compiler. For a different c compiler you might have to use a different random number generator. If you are interested in statistical programming an excellent book that covers the topic is Modeling with Data, Tools and Techniques for Scientific Computing by Ben Klemens. There are a lot of interesting and useful examples within this book. A lot of tough concepts put together very well. By C. Eric Cashon */ #include "stdio.h" #include "random.h" #include "math.h" /* A function to calculate percent of control off of the random numbers in the data array and to put them in a percent array. */ void CalculatePercentControl(double DataArray[], double PercentArray[]) { double dPosControlSum=0; double dPosControlAvg; double dNegControlSum=0; double dNegControlAvg; for(int i=0; i<=99; i++) { for(int j=(i*96); j<(i*96+4); j++) { dPosControlSum += DataArray[j]; } dPosControlAvg = dPosControlSum/4; for(int j=(i*96+4); j<(i*96+8); j++) { dNegControlSum += DataArray[j]; } dNegControlAvg = dNegControlSum/4; for(int j=(i*96); j<(i*96+96); j++) { PercentArray[j] = ((DataArray[j]-dNegControlAvg)/(dPosControlAvg-dNegControlAvg))*100; } dPosControlSum=0; dNegControlSum=0; } } int main() { double DataArray[9600]; double PercentArray[9600]; double RndNumber; FILE *f = NULL; for (int i=0; i<=99; i++) { for(int j=(i*96); j<(i*96+4); j++) { do { RndNumber = genrand_real1() * 1000; } while (RndNumber < 800); DataArray[j] = RndNumber; } for(int j=(i*96+4); j<(i*96+8); j++) { do { RndNumber = genrand_real1() * 1000; } while (RndNumber > 200); DataArray[j] = RndNumber; } for(int j=(i*96+8); j<(i*96+96); j++) { DataArray[j] = genrand_real1() * 1000; } } CalculatePercentControl(DataArray, PercentArray); /* Print the results to a text file. It is pretty fast. */ if (!f) { f = fopen("percentdata.txt", "w"); for(int i=0; i<=9599; i++) { fprintf(f,"%i\t%f\t%f\n", i, DataArray[i], PercentArray[i]); } /* for(int i=0; i<=9599; i++) { printf("%i\t%f\t%f\n", i, DataArray[i], PercentArray[i]); } */ fclose(f); } else printf("Couldn't open a file.\n"); return 0; }