I'm working on a project, and can't seem to get the project to get the triangle variable to increase when the conditions are met. I need this number to be accurate so I can work out the probability. Below you'll find the project, and my amateurish code. Any help is appreciated.
Problem
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.
1) Please remove extraneous variable declarations, they clutter up the code quite a bit. Remember variables are LOCAL where they are declared - doBreak() and doProbability() can't see any variables declared in main()! Even if you pass them in as arguments, you are still creating entirely new variables inside doBreak() and doProbability() that are given the same VALUE as the variable in main(), but changes to that variable in doBreak() or doProbability() still don't affect the original variable in main()
2) Does 'doBreak()' really need any parameters? Or are those really just local variables?
3) Line 93-96, a function can only return once - what are you trying to get out of this function?
4) Line 40, you call doBreak, which has a return value, but you don't check it. Do you need to?
5) The variable triangle is declared in both doBreak() and main(). What does this variable mean? where do you want it really? Note that those are two separate variables! They happen to have the same name, but they are completely unrelated - changing 'triangles' in doBreak() doesn't affect the triangles variable in main().
6) Lines 81-83, if break1== break2, the rod broke in the same place...twice...is that really possible? That would mean there were only 2 pieces, not 3. I doubt your teacher expects you to handle this outlying situation, but if you want, give it a shot.
7) You declare SENTINEL twice (in different areas, else it would give you an error) - one is enough. It is good practice to pull such 'magic numbers' out of main(), but you still have one there: Enter 21 to end program. Change this statement to say "Enter <whatever is in SENTINEL> to end"
I need to count the number of triangles formed, and then use that number to give me a probability of how often a triangle will form when a rod is broken into 3. I've tried to clean up the code taking your advice, but I still have triangle declared in both the main and doBreak. When I try to take it out of main, the compiler tells me triangle is undeclared (for doProbability). If I move doProbability to doBreak, then tests is undeclared.
Thought I would give it a shot myself, it's an interesting problem. However, I am not sure if you are supposed to generate three randoms or two, so I did three.