I give up trying to post with indents visible, using the source code button doesn't help. Since C doesn't care about indentation this is just a bit less readable but perfectly compileable as it is. If you run it without any command line options it will give you a Usage statement
Edit: Manually stuck in the indentation
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* test numsamples numruns outputfilename val1 val2 val3 */
main(int argc,char *argv[])
{
int i, j, k;
int a, b, value, val1, val2, val3, currgap, maxgap, sum;
int numsamples, numruns;
int lookup[10][10] = {
{10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{ 1, 10, 9, 8, 7, 6, 5, 4, 3, 2},
{ 2, 1, 10, 9, 8, 7, 6, 5, 4, 3},
{ 3, 2, 1, 10, 9, 8, 7, 6, 5, 4},
{ 4, 3, 2, 1, 10, 9, 8, 7, 6, 5},
{ 5, 4, 3, 2, 1, 10, 9, 8, 7, 6},
{ 6, 5, 4, 3, 2, 1, 10, 9, 8, 7},
{ 7, 6, 5, 4, 3, 2, 1, 10, 9, 8},
{ 8, 7, 6, 5, 4, 3, 2, 1, 10, 9},
{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 10}};
/* Generate a random seed for the C library rand() function */
srand(time(NULL));
if (argc != 6) {
printf( "Usage: test numsamples numruns value1 value2 value3\n" );
return -1;
}
else {
numsamples = atoi( argv[1] );
numruns = atoi( argv[2] );
val1 = atoi( argv[3] );
val2 = atoi( argv[4] );
val3 = atoi( argv[5] );
/* Validate the inputs */
if (numsamples < 2) {
printf( "numsamples is %i, must be > 1\n", numsamples );
return -1;
}
if (numruns < 1) { /* Check for an invalid number of runs */
printf( "Specified numruns value of %i is invalid, must be > 0\n", numruns );
return -1;
}
if ((val1<1) || (val1>10) || (val2<1) || (val2>10) || (val3<1) || (val3>10)) {
printf( "values to check must be in the range of 1-10, you provided %i, %i, %i\n", val1, val2, val3 );
return -1;
}
}
printf ("Running test with %i samples, %i times for values of %i, %i, %i\n", numsamples, numruns, val1, val2, val3 );
sum = 0;
/* Run the test numruns times */
for (i=0; i<numruns; i++) {
/* Initialize for new run */
currgap = 0;
maxgap = 0;
/* Get the first sample, samples have a range of 0-9 */
b = rand()%10;
/* Run the test for numsamples-1 (we already have the first sample) */
for (j=0; j<(numsamples-1); j++) {
/* Get next sample */
a = rand()%10;
/* Look up value from lookup[][] matrix */
value = lookup[a][b];
/* Does it match one of our values, if so any current gap is complete */
if ((value==val1) || (value==val2) || (value==val3)) {
/* Check to see if the current gap is bigger than our longest gap found so far */
if (currgap > maxgap)
maxgap = currgap; /* We have a new maxgap */
currgap = 0;
}
/* Otherwise, bump the current gap length */
else
currgap++;
}
/* Our run is complete, print the maxgap found */
sum += maxgap;
printf( "%i\n", maxgap );
}
/* Print the average maxgap found */
printf( "\nAverage maxgap: %f\n", (float)sum/(float)numruns );
}