/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* main.c
* Copyright (C) DMGualtieri 2011
*
* triangle 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.
*
* triangle 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 .
*/
/*
The problem - Can you make a triangle from a stick randomly broken into
three pieces? We use the property that the sum of any two sides of a
triangle must be larger than the remaining side, a principle known as the
triangle inequality.
*/
// Note - Written more for readability than speed!
#include
#include
#include
#include
long int i,j, trials, iterations;
int t_flag,triangle;
float point1,point2,side1,side2,side3, percentage;
FILE *outdata;
//generates a psuedo-random float between 0.0 and 0.999...
float randfloat()
{
return (((float)rand()/RAND_MAX));
}
int main(int argc, char *argv[])
{
if (argc<2)
{
printf("Usage: triangle number_of_trials number_of_iterations\n");
exit(1);
}
//Seed random number generator
srand ( (unsigned)time ( NULL ) );
//Open output datafile
if ((outdata = fopen("triangle_data.txt","w"))==NULL)
{printf ("\nOutput file cannot be opened.\n");
exit (1);}
else
{
printf("Output file = triangle_data.txt");
}
trials = (long int)atoi(argv[1]);
iterations = (long int)atoi(argv[2]);
printf("\ntrials = %ld\titerations = %ld\n",trials, iterations);
for(j=0;j (side2+side3)) t_flag = 0;
if (side2> (side1+side3)) t_flag = 0;
if (side3> (side1+side2)) t_flag = 0;
if (t_flag ==1) triangle++;
}
percentage = 100* (float)triangle/trials;
fprintf(outdata,"%f\n",percentage);
printf("%f\n",percentage);
}
//Close output file
fclose(outdata);
return (0);
}