/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * main.c
 * Copyright (C) 2014 drdev <drdev@localhost.com>
 * 
 * menu_xkcd_287 is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * menu_xkcd_287 is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
 Answers problem posed by xkcd comic number 287 (http://xkcd.com/287/)
 There are two answers; namely, Seven mixed fruit orders, or a combination
 of two orders of hot wings, one order of mixed fruit, and one sampler plate.
 */

#include <stdio.h>
#include <stdlib.h>

int i,j,k;
int trials;
int a,b,c,d,e,f;
char *item[] = {"mixed fruit", "french fries", "side salad", "hot wings",
                "mozzarella sticks","sampler plate"};
FILE *outdata;

int rand_num(int limit) 
{
//return a random number between 0 and limit inclusive.
//Note - This is the **best** way to generate uniform random numbers

    int divisor = RAND_MAX/(limit+1);
    int retval;

    do { 
        retval = rand() / divisor;
    } while (retval > limit);

    return retval;
}


int main(int argc, char *argv[])
{

if (argc<1)
{
printf("Usage: menu_xkcd number_of_trials\n");
exit(1);
}

//Open output datafile
if ((outdata = fopen("xkcd_data.txt","w"))==NULL)
	{printf ("\nOutput file cannot be opened.\n");
	exit (1);}

trials = atoi(argv[1]);

for(i=0;i<trials;i++)
{
 for(j=0;j<30000;j++) //arbitrary limit on how many random selections
 {
 a=rand_num(7); //more than seven items of this category will exceed limit
 b=rand_num(5); //etc.
 c=rand_num(4);
 d=rand_num(4);
 e=rand_num(3);
 f=rand_num(2);

 k = (215*a) + (275*b) + (335*c) + (355*d) + (420*e) + (580*f);
 if (k==1505)
 {
 printf("%d %s\t%d %s\t%d %s\t%d %s\t%d %s\t%d %s\n",a,item[0],b,item[1],c,item[2],d,item[3],e,item[4],f,item[5]);
 fprintf(outdata,"%d\n", j);
 break;
 }
 }
 }
 
 return(0);
}
