# Glass Rod Problem

This is a discussion on Glass Rod Problem within the C++ Programming forums, part of the General Programming Boards category; No worries, since everyone enjoyed it so much, I'm sure I'll be visiting more often! I'm not used to being ...

1. No worries, since everyone enjoyed it so much, I'm sure I'll be visiting more often! I'm not used to being so out of my depth, but I'm enjoying it. The way I like to describe learning C++ so far is like learning Spanish, except the text book is in Spanish... I truly appreciate the help, and hope one day to be able to return the favor to another person just starting out. Amazing help, and impressive patience. Now I just have to go add all the story telling //.

2. Originally Posted by CoryMore
I believe it is, after adding a print out:
Code:
```if
((side1 + side2) > side3 &&
(side1 + side3) > side2 &&
(side2 + side3) > side1)
{
triangle += 1;
cout << triangle << endl;
}
return triangle;
}```
I get:

\$ ./rods
Enter number of glassrods to demolish (Enter 21 to end program): 100
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
The probability that the broken glass rods will form a triangle is: 30%
I see you got it working as I was commenting about it (which I edited).
Good job!

Since the 'triangle' count value is only used in main(), that could be a logical place to
maintain it. Then doBreak() only needs to return 0 (no triangle) or 1 (triangle formed).

You could remove the 'triangle' variable completely from doBreak() and change this part:
Code:
```	if
((side1 + side2) > side3 &&
(side1 + side3) > side2 &&
(side2 + side3) > side1)
{
triangle += 1;
}
return triangle;```
to this:

Code:
```	if
((side1 + side2) > side3 &&
(side1 + side3) > side2 &&
(side2 + side3) > side1)
return 1;
else
return 0;```
Then, in main(), just declare:

float triangle = 0;

and use:

triangle = triangle + doBreak();

3. That put a nice bow on things. It's a clean, lean, mean fighting machine now. Thanks for all the help. Next program tomorrow, wish me luck, I'm hoping they want me to cout << "Hello World" << endl;

Probably a bit much to hope for.

4. Originally Posted by megafiddle
I would change this:

const int SENTINEL = 21; //sentinal value

to this:

#define SENTINEL 21 //sentinal value

as it doesn't need to be a variable.

Also, you want:

static float triangle = 0;
I would not. #defines are Evil™ since they are typeless by nature (only its value has a type), the disobey the scoping system and they can cause all sorts of evil text-substitution problems.
Even if the variable would take up memory, which it most likely will not, it is still premature optimization. Using 4 bytes of extra memory is a small price to pay for getting rid of #defines.

Also, static variables can be messy. I'd suggest learning how to pass parameters to functions if you must pass some value to it.
And for the heck of it, SourceForge.net: Do not remove parameter names - cpwiki

Page 2 of 2 First 12
Popular pages Recent additions