Glass Rod dilemma.

This is a discussion on Glass Rod dilemma. within the C++ Programming forums, part of the General Programming Boards category; Good Morning all: I am new to programming and I have a homework assignment that is giving me some difficulty. ...

  1. #1
    les
    les is offline
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    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));
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > triangle = triangle + attempts;
    You're not calling the function.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    les
    les is offline
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    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?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    No, you need to make it into a function call.

    attempts(0);
    is a call.

    attempts;
    is not
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    les
    les is offline
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    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?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Ah, you also have a local variable called attempts as well.
    Which is otherwise unused and useless.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    les
    les is offline
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    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;
    	}
    }

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > probability = (triangle / tests) * 100;
    This will be done in integer arithmetic, giving you zero no doubt.

    Try perhaps
    triangle * 100 / tests;
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    les
    les is offline
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    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));
    }

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > attempts();
    > triangle = triangle + attempts();
    Why are you calling attempts() twice now?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Glass Rod Problem
    By CoryMore in forum C++ Programming
    Replies: 18
    Last Post: 06-26-2012, 02:20 AM
  2. C; A Dilemma
    By Imanuel in forum C++ Programming
    Replies: 6
    Last Post: 10-05-2011, 09:18 PM
  3. dilemma
    By axon in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 12-03-2003, 12:28 PM
  4. The glass is half full
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-30-2002, 02:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21