Code:
//void function2 random numbers>=1 <=999 with data validation, eliminate duplicates
//void function 2 IN, return 3 out for lengths of pieces (5 paramaters)
//bool function to accept 3 lenghts, return statement in bool value of false if no triangle made (3 paramaters)
//main function, loop to run 1,000,000 times. display results. seed random on system time.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void randomNumbers(int & firstBreak, int & secondBreak);
void lengthOfPieces(int & firstBreak, int & secondBreak, int & sideOne, int & sideTwo, int & sideThree);
bool isTriangle(int sideOne, int sideTwo, int sideThree);
void main ()
{
int count= 0;
int triangle = 0;
bool status;
int notTriangle = 0;
int firstBreak, secondBreak, sideOne, sideTwo, sideThree;
unsigned seed = time(0);
srand(seed);
randomNumbers (firstBreak, secondBreak);
lengthOfPieces (firstBreak, secondBreak, sideOne, sideTwo, sideThree);
for (count = 0; count <=1000000; count++)
{
status = isTriangle (sideOne, sideTwo, sideThree);
if (status == 1)
triangle++;
else if (status == 0)
notTriangle++;
}
cout << "The rod dropped 1,000,000 times and created a triangle " << triangle << " times." << endl;
cout << "The rod did not create a triangle " << notTriangle << " times." << endl;
}
void randomNumbers (int & firstBreak, int & secondBreak)
{
do
{
firstBreak = rand () % 999 + 1;
secondBreak = rand () % 999 + 1;
}
while (firstBreak == secondBreak);
}
void lengthOfPieces (int & firstBreak, int & secondBreak, int & sideOne, int & sideTwo, int & sideThree)
{
sideOne = 1000 - (firstBreak + secondBreak);
sideTwo = 1000 - sideOne - firstBreak;
sideThree = 1000 - secondBreak;
}
bool isTriangle (int sideOne, int sideTwo, int sideThree)
{
bool status;
if (sideOne + sideTwo > sideThree || sideTwo + sideThree > sideOne || sideOne + sideThree > sideTwo)
status = true;
return status;
}
I am compiling fine, but the answer I am getting is
"The rod dropped 1,000,000 times and made a triangle 1,000,0001 times.
The rod did not make a triangle 0 times."
I have done the counter, called the function inside because I need it to run 1000000 times, the trouble seems to be getting it to count the number of true/false properly. Status can return either one so I had to use another variable.