# Glass Rod dilemma.

• 11-19-2012
les
Glass Rod dilemma.
Good Morning all:

I am new to programming and I have a homework assignment that is giving me some difficulty. The problem is the glass rod problem. Where you break a glass rod in 3 pieces an see if the pieces will for a triangle.
The main problem is how the professor wants the program written.
We have to write a main and three functions that do most of the work. I have my code written and it compiles but it returns erroneous answers. Here is my code, can anyone point me in the right direction to fixing it. Please

Code:

```#include <iostream> #include <cmath> #include <cstdlib> #include <ctime> #include <cstdlib> using namespace std; void doBreak (int & side1, int & side2, int & side3); bool attempts (int); void randomNumberGen (int & num1, int & num2); void main() {     int count = 1;     int tests = 1000000;     double probability;     int attempts;     int triangle = 0;     int val;         srand (time(0));     do         {             triangle = triangle + attempts;             count++;         }while (count <= tests);     probability = (triangle / tests) * 100;     cout <<"The probability that the broken glass rod will form a triangle is"         << probability <<"% \n"; } bool attempts (int) {     int side1, side2, side3;     doBreak (side1, side2, side3);     if         ((side1 + side2) > side3 &&         (side1 + side3) > side2 &&         (side2 + side3) > side1)     {         return true;     }     else     {         return false;     } } void doBreak (int & side1, int & side2, int & side3) {     int num1, num2;     randomNumberGen (num1, num2);     side1 = num1;     side2 = (num2 - num1);     side3 = 1000 - num2; } void randomNumberGen (int & num1, int & num2) {     do   {     num1 = rand() % 999 + 1;     num2 = rand() % 999 + 1;   } while (!(num1 < num2)); }```
• 11-19-2012
Salem
> triangle = triangle + attempts;
You're not calling the function.
• 11-19-2012
les
Hello Salem:
If I am reading your response correctly, ( triangle = triangle + attempts;) is line 30 of the code. do I need to add the (>) symbol?
• 11-19-2012
Salem
No, you need to make it into a function call.

attempts(0);
is a call.

attempts;
is not
• 11-19-2012
les
I have tried ( attempts ();, attempts (int);, attempts (int, int, int);) It always says that " term does not evaluate to a function taking however many arguments. What am I doing wrong?
• 11-19-2012
Salem
Ah, you also have a local variable called attempts as well.
Which is otherwise unused and useless.
• 11-19-2012
les
I made the following changes and now my answer comes back 0% every time.
Code:

```do                 {                         attempts();                         triangle = triangle + attempts();                         count++;                 }while (count <= tests);         probability = (triangle / tests) * 100;         cout <<"The probability that the broken glass rod will form a triangle is"                 << probability <<"% \n"; } bool attempts () {         int side1, side2, side3, triangle;         doBreak (side1, side2, side3);         if                 ((side1 + side2) > side3 &&                 (side1 + side3) > side2 &&                 (side2 + side3) > side1)         {                 return  triangle = true;         }         else         {                 return  triangle = false;         } }```
• 11-19-2012
Salem
> probability = (triangle / tests) * 100;
This will be done in integer arithmetic, giving you zero no doubt.

Try perhaps
triangle * 100 / tests;
• 11-19-2012
les
Thank you Salem. I made the change you suggested plus a couple more and now it works great thank you very much. Here is the working code.
Code:

```#include <iostream> #include <cmath> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; void doBreak (int & side1, int & side2, int & side3); bool attempts (); void randomNumberGen (int & num1, int & num2); void main() {         int count = 1;         double tests = 1000000;         double probability;         double triangle = 0;                         srand (time(0));                 do                 {                         attempts();                         triangle = triangle + attempts();                         count++;                 }while (count <= tests);         probability = ((triangle * 100) / tests);         cout << setprecision(5);         cout <<"The probability that the broken glass rod will form a triangle is"                 << probability <<"% \n";         } bool attempts () {         int side1, side2, side3, triangle;         doBreak (side1, side2, side3);         if                 ((side1 + side2) > side3 &&                 (side1 + side3) > side2 &&                 (side2 + side3) > side1)         {                 return true;         }         else         {                 return false;         } } void doBreak (int & side1, int & side2, int & side3) {         int num1, num2;         randomNumberGen (num1, num2);         side1 = num1;         side2 = (num2 - num1);         side3 = 1000 - num2; } void randomNumberGen (int & num1, int & num2) {         do   {     num1 = rand() % 999 + 1;     num2 = rand() % 999 + 1;   } while (!(num1 < num2)); }```
• 11-19-2012
Salem
> attempts();
> triangle = triangle + attempts();
Why are you calling attempts() twice now?