Loop causing run to fail, i believe

I have a project where I have to simulate amount of triangles formed from broken glass rods. The test is supposed to run multiple times depending on how many rods are put into the program. I believe the problem is with my loop, because I tested everything else as I went. Once again the program is compiling but once I enter the amount of rods compiler states that the run failed. 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
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<cstdlib>
#include<ctime>
#include<iomanip>

using namespace std;
//Protocols
//Determining amount of triangles formed from rods
int get_tri(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 x;  //counter for for loop
    int triangles=0;  //amount of triangles formed
    float prob;         //probability of triangles formed
    
    cout << "Enter amount of rods to be dropped:"<< endl;
    cin >> num_rods;
    
    if(num_rods != SENTINEL)
    {
        
        for (x = 1; x <= num_rods; x++)
        {
            triangles = triangles + get_tri(triangles); 
        }
        
     }
    
    else
        cout << "Have a Nice Day!"<< endl;
    
    prob = tri_prob(num_rods, triangles, prob);
    
    cout << "The probability that "<< num_rods << " rods will form triangles is:" << 
            endl;
    cout << setprecision (3)<< prob <<"%"<< endl;
}

int get_tri(int& triangles)
{
    int break1;         //first break point
    int break2;         //second break point
    float side1;        //first side
    float side2;        //second side
    float side3;        //third side
    
    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;
    }
    
    if (
         (side1 + side2) > side3 &&     //Determines whether a
         (side2 +  side3) > side1 &&    //triangle is formed from
         (side1 + side3) > side2        //breaking points
       )
    {
        triangles = 1;
    
    
        return triangles;
    }
    else 
        return 0;
 }

float tri_prob(int num_rods, int triangles, float& prob)
{
    prob = (num_rods/triangles) * 100;
    
    return prob;
}
Don't know too much about rand, but line 68 should be in main, or at least only call it once.

Line 70 and 71 seem to be the problem. I would guess that break1 and break2 always end up being zero (a number less than one being stored as an int). The if statement at line 86 is never true, triangles is always zero, and then you do division by zero when you call tri_prob.

I think all you want for your breaks is break1 = rand();, but again, don't know much about it.

Set up some couts or use a debugger to see what your values are.
I think that the function int get_tri(int& triangles); is invalid.

When you call it

triangles = triangles + get_tri(triangles);

it changes triangles twice because triangles is passed by reference.

What happens if you take the ampersand out of the function call for int get_tri(int& triangles)?

Right now, you're directly changing the value of triangle when you call the function, and then adding it again to itself. I'm not sure how that'll work out.

Also, there's a possibility of dividing by 0 in your other function.
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.
You should place the statement

srand(time(0)); //sets srand to pick random numbers

outside your function int get_tri(int num_rods, int& amt). Otherwise you will get the same random numbers each time you call the function, because time will not be changed.
You should not be placing the loop inside your function. Once it hits the return statement, the loop stops looping (ie, it only goes through it once).

Also, I can't see how you get 100%. From your set up, you should be getting 0%.

1
2
3
4
5
6
if (break1>break2)
    {
        side1 = break1;         //determines side 1
        side2 = break2 - break1;        //determines side 2
        side3 = 1 - break2;     //determines side 3
    }


Side2 is negative.
That's true, that sign should be flipped. Also (I'm pretty new to this, but actually just did this problem), Also, shouldn't the whole function be dependent on x<=numRods? This way, wouldn't it keep breaking rods, just not testing for triangles? Not positive. Here's where I ended up on my break function.

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
double doBreak()
{
	double break1;
	double break2;	
	double side1;
	double side2;
	double side3;
	double rodLength;
	rodLength = 1;	
		
	break1 = (double)rand()/RAND_MAX;
	break2 = (double)rand()/RAND_MAX;
	
	if (break1 < break2)
	{
		side1 = break1;
		side2 = (break2 - break1);
		side3 = rodLength - break2;
	}
	else
	{	
		side1 = break2;
		side2 = (break1 - break2);
		side3 = rodLength - break1;
	}
	if
	((side1 + side2) > side3 &&
	(side1 + side3) > side2 &&
	(side2 + side3) > side1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
@CoryMore

I have rewritten your function the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int doBreak()
{
	double break1 = (double)rand() / RAND_MAX;
	double break2 = (double)rand() / RAND_MAX;
	
	if ( break2 < break1 ) std::swap( break1, break2 ); 

	double side1 = break1;
	double side2 = break2 - break1;
	double side3 = 1.0 - break2;

	return ( ( side1 + side2 > side3 ) &&
	         ( side1 + side3 > side2 ) &&
	         ( side2 + side3 > side1 ) );
}
I did end up taking the loop out of that function all together and, I put a do while loop in get_info(), i'm guessing i'm going to have to break down, and compile get_tri and tri_prob individually to figure out why my probability keeps saying 0%
So i just went through the get_tri function, and it's working perfectly so either it's a problem returning the value, or a problem with the probability function, or maybe it's running perfect and i'm thinking it's supposed to do something else. I will figure it out hopefully when i get back from this other class
Topic archived. No new replies allowed.