Thread: Glass Rod Problem and big loop problem!

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    11

    Glass Rod Problem and big loop problem!

    I have been having some trouble with the loop in this problem. It is supposed to run 1,000,000 times, but I can not seem to get it to initialize more than 2 times even though I have tried all different types of loops. Can anyone give me a suggestion or where to start?
    First semester programmer here.

    The top comments are the basic instructions of how to format the program.

    Code:
    //void function 2 random numbers>=1 <=999 with data validation to eliminate duplicates
    //void function 2 IN, return 3 out for lengths of pieces (5 paramaters)
    //bool function to accept 3 lenghts, return statement in bool value of false if no triangle made (3 paramaters)
    //main function, loop to run 1,000,000 times. display results. seed random on system time.
    
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    
    void randomNumbers(int & firstBreak, int & secondBreak);
    void lengthOfPieces(int & firstBreak, int & secondBreak, int & sideOne, int & sideTwo, int & sideThree);
    bool isTriangle(int & sideOne, int & sideTwo, int & sideThree);
    
    
    void main ()
    {
    	int count= 0;
    	int triangle = 0;
    	bool status;
    	int notTriangle = 0;
    	int firstBreak, secondBreak, sideOne, sideTwo, sideThree;
    
    
    	unsigned seed = time(0);
    	srand(seed);
    
    
    	randomNumbers (firstBreak, secondBreak);
    	lengthOfPieces (firstBreak, secondBreak, sideOne, sideTwo, sideThree);
    	isTriangle (sideOne, sideTwo, sideThree);
    	
    //	if (status = true)
    //	{
    //		triangle++;
    //	}
    //	else if (status = false)
    //	{
    //		notTriangle++;
    //	}
    	
    	
    	
    	notTriangle = 1000000 - triangle;
    
    
    	cout << "The rod dropped 1,000,000 times and created a triangle " << triangle <<  " times." << endl;
    	cout << "The rod did not create a triangle " << notTriangle <<  " times." << endl;
    }
    
    
    void randomNumbers (int & firstBreak, int & secondBreak)
    {
    	do 
    	{
    	firstBreak = rand () % 999 + 1;
    	secondBreak = rand () % 999 + 1;
    	}
    	while (firstBreak == secondBreak);
    }
    
    
    void lengthOfPieces (int & firstBreak, int & secondBreak, int & sideOne, int & sideTwo, int & sideThree)
    {
    	if (firstBreak < secondBreak)
    (sideOne = firstBreak + 1) && (sideTwo = secondBreak - (firstBreak + 1)) && (sideThree = 1000 - (sideOne + sideTwo));    
    	else 
    (sideOne = secondBreak + 1) && (sideTwo = firstBreak - (secondBreak + 1)) && (sideThree = 1000 - (sideOne + sideTwo)); 
    }
    
    
    bool isTriangle (int & sideOne, int & sideTwo, int & sideThree)
    {
    	bool status;
    	if (sideOne + sideTwo > sideThree || sideTwo + sideThree > sideOne || sideOne + sideThree > sideTwo)
    		status = true;
    	else
    		status = false;
    
    
    	return status;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > if (status = true)
    You need to know that there is a difference between = and ==

    > (sideOne = firstBreak + 1) && (sideTwo = secondBreak - (firstBreak + 1)) && (sideThree = 1000 - (sideOne + sideTwo));
    You NEED to know that if the left hand side of the && evaluates to false, the right hand side DOES NOT HAPPEN!
    Embedded assignments in an && or || expression are a train wreck!

    Do it properly, and stop trying to overly compress your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Guest
    Guest
    Unless I'm blind, you haven't specified a loop yet. A basic one might look as follows:
    Code:
    for(int i = 0; i < 1000000; ++i) {
        // put stuff here to be executed a million times
    }
    You can also use scientific notation (i < 1e6), which is convenient for large numbers.

    Since the isTriangle() function returns bool, you will need to query it for that, otherwise the result simply gets discarded.
    Code:
    int main()
    {
        bool status;
        // ...
        status = isTriangle(sideOne, sideTwo, sideThree);
    }
    If you don't modify the integer arguments passed to the function, they should be passed by value instead if reference:
    Code:
    bool isTriangle (int sideOne, int sideTwo, int sideThree)
    {
        // ...
    }
    Also consider that your (sideOne + SideTwo > ...) expression returns bool. So you're basically saying if(true), then value = true; return true;
    Try to think of a way to simplify that logic. (The 6 lines can be reduced to just a single expression)

    main()'s return type is always int:
    Code:
    int main()
    { }
    All beginnings are hard, good luck!

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    Code:
    //void function2 random numbers>=1 <=999 with data validation, eliminate duplicates
    //void function 2 IN, return 3 out for lengths of pieces (5 paramaters)
    //bool function to accept 3 lenghts, return statement in bool value of false if no triangle made (3 paramaters)
    //main function, loop to run 1,000,000 times. display results. seed random on system time.
    
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    
    void randomNumbers(int & firstBreak, int & secondBreak);
    void lengthOfPieces(int & firstBreak, int & secondBreak, int & sideOne, int & sideTwo, int & sideThree);
    bool isTriangle(int sideOne, int sideTwo, int sideThree);
    
    
    void main ()
    {
    	int count= 0;
    	int triangle = 0;
    	bool status;
    	int notTriangle = 0;
    	int firstBreak, secondBreak, sideOne, sideTwo, sideThree;
    
    
    	unsigned seed = time(0);
    	srand(seed);
    
    
    	randomNumbers (firstBreak, secondBreak);
    	lengthOfPieces (firstBreak, secondBreak, sideOne, sideTwo, sideThree);
    	
    	
    	for (count = 0; count <=1000000; count++)
    	{
    		status = isTriangle (sideOne, sideTwo, sideThree);
    		if (status == 1)
    			triangle++;
    		else if (status == 0)
    			notTriangle++;
    	}
    
    
    
    
    	cout << "The rod dropped 1,000,000 times and created a triangle " << triangle <<  " times." << endl;
    	cout << "The rod did not create a triangle " << notTriangle <<  " times." << endl;
    }
    
    
    void randomNumbers (int & firstBreak, int & secondBreak)
    {
    	do 
    	{
    	firstBreak = rand () % 999 + 1;
    	secondBreak = rand () % 999 + 1;
    	}
    	while (firstBreak == secondBreak);
    }
    
    
    void lengthOfPieces (int & firstBreak, int & secondBreak, int & sideOne, int & sideTwo, int & sideThree)
    {
    	sideOne = 1000 - (firstBreak + secondBreak);
    	sideTwo = 1000 - sideOne - firstBreak;
    	sideThree = 1000 - secondBreak;
    }
    
    
    bool isTriangle (int sideOne, int sideTwo, int sideThree)
    {
    	bool status;
    	if (sideOne + sideTwo > sideThree || sideTwo + sideThree > sideOne || sideOne + sideThree > sideTwo)
    		status = true;
    		return status;
    }
    I am compiling fine, but the answer I am getting is
    "The rod dropped 1,000,000 times and made a triangle 1,000,0001 times.
    The rod did not make a triangle 0 times."

    I have done the counter, called the function inside because I need it to run 1000000 times, the trouble seems to be getting it to count the number of true/false properly. Status can return either one so I had to use another variable.

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    I have also tried the loop this way with no avail...

    Code:
    for (count = 0; count <=1000000; count++)
    	{
    		if (status = true)
    			triangle++;
    		else
    			notTriangle++;
    
    
    		status = isTriangle (sideOne, sideTwo, sideThree);
    	}

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall that status = true assigns true to status. You probably wanted to write status == true, though I would prefer just writing status since it is already a bool.

    Note that if you write the assignment to status at the end of the loop body, then you need to give status a value before the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    In variable declaration I did change:
    bool status = false;


    Code:
    	for (count = 0; count <=1000000; count++)
    	{
    		status = isTriangle (sideOne, sideTwo, sideThree);
    		if (status)
    			triangle++;
    		else
    			notTriangle++;
    	}
    Still returns:
    "The rod dropped 1,000,000 times and made a triangle 1,000,0001 times.
    The rod did not make a triangle 0 times."

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to initialise status in isTriangle too. Actually, I would just write:
    Code:
    bool isTriangle(int sideOne, int sideTwo, int sideThree)
    {
        return sideOne + sideTwo > sideThree || sideTwo + sideThree > sideOne || sideOne + sideThree > sideTwo;
    }
    Likewise:
    Code:
    for (count = 0; count < 1000000; count++)
    {
        if (isTriangle(sideOne, sideTwo, sideThree))
        {
            triangle++;
        }
        else
        {
            notTriangle++;
        }
    }
    I changed your loop condition to count < 1000000 because you probably wanted that: generally, we start from 0 then use strictly less than, or start from 1 then use less than or equal to.
    Last edited by laserlight; 04-12-2015 at 10:18 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    It is still not counting correctly - 0 false answers, 1,000,000 true answers - results should be about 25%

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?

    EDIT:
    Why do you terminate the loop in your randomNumbers function when firstBreak == secondBreak?
    Last edited by laserlight; 04-12-2015 at 10:34 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    We are supposed to validate that we do not have any duplicate random numbers, so I thought that it would keep running if they were the same. Been playing with the loop and what I have in now causes the program to not run...

    Code:
    //void function2 random numbers>=1 <=999 with data validation, eliminate duplicates
    //void function 2 IN, return 3 out for lengths of pieces (5 paramaters)
    //bool function to accept 3 lenghts, return statement in bool value of false if no triangle made (3 paramaters)
    //main function, loop to run 1,000,000 times. display results. seed random on system time.
    
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    
    void randomNumbers(int & firstBreak, int & secondBreak);
    void lengthOfPieces(int & firstBreak, int & secondBreak, int & sideOne, int & sideTwo, int & sideThree);
    bool isTriangle(int sideOne, int sideTwo, int sideThree);
    
    
    int main ()
    {
    	int count= 0;
    	int triangle = 0;
    	bool status = false;
    	int notTriangle = 0;
    	int firstBreak, secondBreak, sideOne, sideTwo, sideThree;
    
    
    	unsigned seed = time(0);
    	srand(seed);
    
    
    	randomNumbers (firstBreak, secondBreak);
    	lengthOfPieces (firstBreak, secondBreak, sideOne, sideTwo, sideThree);
    	
    	
    	for (count = 0; count < 1000000; count++)
    	{
    		
    		while (count < 1000000)
    			(isTriangle(sideOne, sideTwo, sideThree));
    			triangle++;
    			notTriangle++;
    			count++;
    	}
    
    
    	cout << "The rod dropped 1,000,000 times and created a triangle " << triangle <<  " times." << endl;
    	cout << "The rod did not create a triangle " << notTriangle <<  " times." << endl;
    
    
    	return 0;
    }
    
    
    void randomNumbers (int & firstBreak, int & secondBreak)
    {
    	do 
    	{
    	firstBreak = rand() % 999 + 1;
    	secondBreak = rand() % 999 + 1;
    	}
    	while (firstBreak == secondBreak);
    }
    
    
    void lengthOfPieces (int & firstBreak, int & secondBreak, int & sideOne, int & sideTwo, int & sideThree)
    {
    	sideOne = 1000 - (firstBreak + secondBreak);
    	sideTwo = 1000 - sideOne - firstBreak;
    	sideThree = 1000 - secondBreak;
    }
    
    
    bool isTriangle (int sideOne, int sideTwo, int sideThree)
    {
    
    
    	return sideOne + sideTwo > sideThree || sideTwo + sideThree > sideOne || sideOne + sideThree > sideTwo;
    
    
    }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TessaG
    We are supposed to validate that we do not have any duplicate random numbers, so I thought that it would keep running if they were the same.
    Oh, my mistake, I misread it. That said, it would be better to write:
    Code:
    firstBreak = rand () % 999 + 1;
    do
    {
        secondBreak = rand () % 999 + 1;
    }
    while (firstBreak == secondBreak);
    Anyway, it looks like you are not calling randomNumbers and lengthOfPieces from within the loop, so the loop keeps working with the same values.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    Code:
    for (count = 0; count < 1000000; count++)
    	{
    		randomNumbers (firstBreak, secondBreak);
    		lengthOfPieces (firstBreak, secondBreak, sideOne, sideTwo, sideThree);
    		isTriangle(sideOne, sideTwo, sideThree);
    			if (status)
    			triangle++;
    			else
    				notTriangle++;
    	}
    returns true 0 times, false 1000000 times.. thoughts?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to assign the return value of the isTriangle call to status.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    noted - changed to:
    Code:
    for (count = 0; count < 1000000; count++)
    	{
    		randomNumbers (firstBreak, secondBreak);
    		lengthOfPieces (firstBreak, secondBreak, sideOne, sideTwo, sideThree);
    		status = isTriangle(sideOne, sideTwo, sideThree);
    			if (status == true)
    			triangle++;
    			else
    				notTriangle++;
    	}
    all true results.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Glass Rod dilemma.
    By les in forum C++ Programming
    Replies: 9
    Last Post: 11-19-2012, 12:15 PM
  2. Glass Rod Problem
    By CoryMore in forum C++ Programming
    Replies: 18
    Last Post: 06-26-2012, 02:20 AM
  3. For loop problem
    By CPlus in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2010, 06:40 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