Thread: Right sided triangle

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005

    Right sided triangle

    Hey guys

    I am trying an exercise in a book I found at the library and it asks me to write a program that reads three values and determines if they could make a right sided triangle.

    I have made a fairly good attempt but I think my algorithm is a bit muddled. Can
    you help me out.

    The code below compiles ok.

    #include <iostream>
    #include <cmath>
    // main function - driver //////////////////////////////////////////////////////
    int main ( void )
    	double hypotenuse = 0,
    		   sideA = 0,
    		   sideB = 0,
    		   squSideA = 0,
    		   squSideB = 0,
    		   totalSqu = 0;
    	std::cout << "Enter the hypotenuse: ";
    	std::cin >> hypotenuse;
    	std::cout << "Enter the sizes of the other two sides: ";
    	std::cin >> sideA >> sideB;
    	// find the square of each side
    	squSideA = sqrt ( sideA );
    	squSideB = sqrt ( sideB );
    	// get the total square of both sides
    	totalSqu = squSideA + squSideB;
    	if ( totalSqu == hypotenuse )
    		std::cout << "\nThis could represent a right-sided triangle\n";
    		std::cout << "\nThis could not represent a right-sided triangle\n";
        std::cin.get(); // freeze console output window
        return 0; // return value from int main
    Double Helix STL

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    London, UK
    sqrt is the square root, the exact opposite of the square You want something like:
    if ((hypotenuse * hypotenuse) == ((sideA * sideA) + (sideB * sideB))) {
       // Woohoo
    edit: This is the Pythagorean Theorem where a^2 + b^2 = c^2, where c is the hypotenuse.

    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Thank you QuantumPete - great help. Thanks so much
    Double Helix STL

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    It may also be a good idea to do your double comparison using a delta value or some such, to avoid problem where someone enters 0.3, 0.4, 0.5, and gets the answer that it isn't a triangle, when it in reality is, just that the 0.3 value turned into 0.2999999999 or some such.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 01:31 PM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-26-2008, 11:24 PM
  3. Resizing a triangle. Why is my code not working?
    By gozu in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2007, 05:40 PM
  4. Stupid Logic Problem Need Outside Viewpoint
    By RP319 in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2005, 09:59 PM
  5. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
Website Security Test