Thread: Glass Rod Problem

  1. #16
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    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. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by CoryMore View Post
    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. #18
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    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. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by megafiddle View Post
    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
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  4. The glass is half full
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-30-2002, 03:08 PM