Okay you were definitely right about setting break 1 and 2 to a float, and i thought that looked kind of weird as far as
triangles = triangles + get_tri(triangles);
so I created a whole new variable to store the value if a triangle is formed. This is my new code, I also changed the place where the loop initiates because every single time the program runs its giving me a probability of 100%, and I don't believe thats what the project is asking for. But then again if you drop 46 pieces of glass you would have a 100% chance of making a triangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<iomanip>
using namespace std;
//Protocols
//Determining amount of triangles formed from rods
int get_tri(int, int&);
//Gets intial information for program
void get_info();
//Determines probability of triangles formed from amount
//of rods broken
float tri_prob(int, int, float&);
int main()
{
//Title and purpose of program
cout << "Broken Glass Simulator:"<< endl<<endl;
cout << "This simulator determines how many triangles"<< endl;
cout << "are formed from a set amount";
cout << " of dropped glass rods"<< endl<< endl;
get_info();
}
void get_info()
{
const int SENTINEL = 21; // Sentinel value to exit program
int num_rods; //Number of glass rods dropped
int num_tri; //Number of triangles formed
int triangles=0; //amount of triangles formed
float prob; //probability of triangles formed
int amt;
cout << "Enter amount of rods to be dropped:"<< endl;
cin >> num_rods;
if(num_rods != SENTINEL)
{
triangles = triangles + get_tri(num_rods, amt);
prob = tri_prob(num_rods, triangles, prob);
cout << "The probability that "<< num_rods << " rods will form triangles is:" <<
endl;
cout << setprecision (3)<< prob <<"%"<< endl;
}
else
cout << "Have a Nice Day!"<< endl;
}
int get_tri(int num_rods, int& amt)
{
float break1; //first break point
float break2; //second break point
float side1; //first side
float side2; //second side
float side3; //third side
int x=0;
srand(time(0)); //sets srand to pick random numbers
break1=((float)rand()/(float)RAND_MAX); //gets first breaking point
break2= ((float)rand()/(float)RAND_MAX);// second breaking point
if (break1>break2)
{
side1 = break1; //determines side 1
side2 = break2 - break1; //determines side 2
side3 = 1 - break2; //determines side 3
}
else
{
side1 = break2;
side2 = break1 - break2;
side3 = 1 - break2;
}
while (x <= num_rods)
{
if
(
(side1 + side2) > side3 && //Determines whether a
(side2 + side3) > side1 && //triangle is formed from
(side1 + side3) > side2 //breaking points
)
{
amt = 1;
return amt;
}
else
return 0;
} x++;
}
float tri_prob(int num_rods, int triangles, float& prob)
{
prob = (triangles/num_rods) * 100;
return prob;
}
|
Here is what the project is actually asking for, maybe i'm missing something
Experiments that are either too expensive or too dangerous to perform are often simulated on a computer when the computer is able to provide a good representation of the experiment. Find out how to call the random-number generator (usually a function returning a floating point value in the range 0 to 1) for your C++ system. (Look up the functions rand and srand in the library cstdlib on the website cplusplus.com). Write a program that uses the random-number generator to simulate the dropping of glass rods that break into three pieces. The purpose of the experiment is to estimate the probability that the lengths of the three pieces are such that they might form the sides of a triangle.
For the purposes of this experiment, you may assume that the glass rod always breaks into three pieces. If you use the line segment 0 to 1 (on the real number line) as a mathematical model of the glass rod, a random-number generator (function) can be used to generate two numbers between 0 and 1 representing the coordinates of the breaks. The triangle inequality (the sum of the lengths of two sides of a triangle are always greater than the length of the third side) may be used to test the length of each piece against the lengths of the other two pieces.
To estimate the probability that the pieces of the rod form a triangle, you’ll need to repeat the experiment many times and count the number of times a triangle can be formed from the pieces. The probability estimate is the number of successes divided by the total number of rods dropped. Your program should prompt the user for the number of rods to drop and allow the experiment to be repeated. Use a sentinel value of 21 to hale execution of the program.