Glass Rod Project

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.

Code:
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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cfloat>
#include <iomanip>
#include <stdlib.h>
#include <time.h>

using namespace std;

float doBreak (float, float);
float doProbability (float, float);

const int SENTINEL = 21;		//sentinal value

int main()
{
	float break1;
	float break2;
	float side1;
	float side2;
	float side3;
	float count;
	float tests;
	float triangle;
	float probability;

	const int SENTINEL = 21;
	count = 1;
	srand (time (NULL));

	cout << "Enter number of glassrods to demolish (Enter 21 to end program): ";
	cin >> tests;

	if 
	(tests != SENTINEL)
	{
		do
		{
			doBreak(break1, break2);
			count++;
		}while (count <= tests);

	doProbability(triangle, tests);
	cout << "The probability that the broken glass rods will form a triangle is: " << probability << "%" << endl;
	}
	else
	{
			cout << "Aww, I was hoping to break stuff..." << endl;
	}
	return 0;
}

float doBreak
	(float break1, 
	float break2)
{
	float side1;
	float side2;
	float side3;
	float triangle;
	triangle = 0;
		
	break1 = (float)rand()/RAND_MAX;
	break2 = (float)rand()/RAND_MAX;
	
	if (break1 < break2)
	{
		side1 = break1;
		side2 = (break2 - break1);
		side3 = 1 - break2;
	}
	else if (break2 < break1)
	{	
		side1 = break2;
		side2 = (break1 - break2);
		side3 = 1 - break1;
	}
	else
	{
		side1 = break1;
		side2 = break1;
		side3 = break1;
	}
	if
	((side1 + side2) > side3 &&
	(side1 + side3) > side2 &&
	(side2 + side3) > side1)
	{
		triangle += 1;
		cout << triangle << endl;
	}
	return side1;
	return side2;
	return side3;
	return triangle;
}
	
float doProbability 
	(float triangle,
	float tests)
{
	float probability;

	probability = (triangle / tests) * 100;
	return probability;
}
Last edited on
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"
Last edited on
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.

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

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cfloat>
#include <iomanip>
#include <stdlib.h>
#include <time.h>

using namespace std;

float doBreak ();
float doProbability (float, float);

const int SENTINEL = 21;		//sentinal value

int main()
{
	float count;
	float tests;
	float triangle;
	float probability;

	count = 1;
	srand (time (NULL));

	cout << "Enter number of glassrods to demolish (Enter " << SENTINEL << " to end program): ";
	cin >> tests;

	if 
	(tests != SENTINEL)
	{
		do
		{
			doBreak();
			count++;
		}while (count <= tests);
		
	doProbability(triangle, tests);
	cout << "The probability that the broken glass rods will form a triangle is: " << probability << "%" << endl;
	}
	else
	{
			cout << "Aww, I was hoping to break stuff..." << endl;
	}
	return 0;
}

float doBreak()
{
	float break1;
	float break2;	
	float side1;
	float side2;
	float side3;
	float triangle = 0;
	
		
	break1 = (float)rand()/RAND_MAX;
	break2 = (float)rand()/RAND_MAX;
	
	if (break1 < break2)
	{
		side1 = break1;
		side2 = (break2 - break1);
		side3 = 1 - break2;
	}
	else
	{	
		side1 = break2;
		side2 = (break1 - break2);
		side3 = 1 - break1;
	}
	if
	((side1 + side2) > side3 &&
	(side1 + side3) > side2 &&
	(side2 + side3) > side1)
	{
		triangle += 1;
		cout << triangle << endl;
	}
	return triangle;
}
	
float doProbability 
	(float triangle,
	float tests)
	
{
	float probability;
	
	probability = (triangle / tests) * 100;
	return probability;
}
Hi,

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.

Here's my code:

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


float random(float a, float b)
{
	return ((b-a)*((float)rand()/RAND_MAX))+a;
}
bool get_triangle(){
	double s1,s2,s3;
	s1=random(0,1);//length of first piece
	s2=random(0,1);//lenght of second piece
	s3=random(0,1);//length of third piece
	if(s1==s2)exit(1);			
	if(s1>(s2+s3))return false;	
	if(s2>(s1+s3))return false;	
	if(s3>(s2+s1))return false;	
	return true;				
}	
int main()
{
	
	char choice;

	do{
		srand((unsigned)time( NULL ) );
		int n, counter=0, triangle_count=0;
		cout<<"Number of glass rods to break: ";
		cin>>n;
		while(n<0){
			cout<<"Can't have negative rods, enter again: ";
			cin>>n;
		}
	
		while(counter<n){
			bool triangle=get_triangle();
			if(get_triangle())
				triangle_count++;
			counter++;
		}
		cout<<"Out of "<<n<<" rods dropped, you have "<<triangle_count<<" triangles!\n";
		double solution=static_cast<double>(triangle_count)/n*100;
		cout<<"Probability estimate: "<<solution<<"%";


		cout<<"\n\nAgain? (y/n): ";
		cin>>choice;
	}while(choice=='y');
	cout<<"Have a nice day: ";
	exit(1);

	_getch();	
    return 0;
}



I appreciate all the feedback. Below is what I ended up with, thanks to the help here, and on another board.

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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cfloat>
#include <iomanip>
#include <stdlib.h>
#include <time.h>

using namespace std;

float doBreak ();
float doProbability (float, float);

const int SENTINEL = 21;		//sentinal value


int main()
{
	float count;
	float tests;
	float probability;
	float triangle = 0;
	

	count = 1;
	srand (time (NULL));

	cout << "Enter number of glassrods to demolish (Enter " << SENTINEL << " to end program): ";
	cin >> tests;

	if 
	(tests != SENTINEL)
	{
		do
		{
			triangle = triangle + doBreak();
			count++;
		}while (count <= tests);

	probability = doProbability(triangle, tests);
	
	cout << "The probability that the broken glass rods will form a triangle is: " << probability << "%" << endl;
	}
	else
	{
			cout << "Aww, I was hoping to break stuff..." << endl;
	}
	return 0;
}

float doBreak()
{
	float break1;
	float break2;	
	float side1;
	float side2;
	float side3;
	
	
	
	
		
	break1 = (float)rand()/RAND_MAX;
	break2 = (float)rand()/RAND_MAX;
	
	if (break1 < break2)
	{
		side1 = break1;
		side2 = (break2 - break1);
		side3 = 1 - break2;
	}
	else
	{	
		side1 = break2;
		side2 = (break1 - break2);
		side3 = 1 - break1;
	}
	if
	((side1 + side2) > side3 &&
	(side1 + side3) > side2 &&
	(side2 + side3) > side1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
	
float doProbability 
	(float triangle,
	float tests)
	
{
	float probability;
	
	probability = (triangle / tests) * 100;
	return probability;
}
Topic archived. No new replies allowed.