Pythagorean Theorem Program Comment, criticize, or whatever

This is a discussion on Pythagorean Theorem Program Comment, criticize, or whatever within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <cmath> #include <string> int main() { double SideA, SideB, SideC, Hyp; // Hyp is the Hypotenuse ...

  1. #1
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    Red face Pythagorean Theorem Program Comment, criticize, or whatever

    Code:
    #include <iostream>
    #include <cmath>
    #include <string>
    
    int main() 
    {
    	double SideA, SideB, SideC, Hyp; // Hyp is the Hypotenuse
    	std::string GetInfo;
    
    	std::cout << "The Pythagorean Theorem \n\n";
    	
    	std::cout << "Do you know the hypotenuse? \n>";
    	std::cin >> GetInfo;
    	std::cout << std::endl;
    
    	std::cin.sync();
    	
    	if ( GetInfo == "no" ) {
    
    		std::cout << "Enter Your Sides: \n";
    		std::cout << "1st: ";
    		std::cin >> SideA;
    		std::cout << "2nd: ";
    		std::cin >> SideB;
    		std::cin.ignore();
    
    		Hyp = sqrt( pow( SideA, 2) + pow( SideB, 2));
    
    		std::cout << "Hypotenuse = " << Hyp << std::endl;
    
    	} else if ( GetInfo == "yes" ) {
    
    		std::cout << "Enter Your Data: \n";
    		std::cout << "Hypotenuse: ";
    		std::cin >> SideC;
    		std::cout << "Side: ";
    		std::cin >> SideA;
    		std::cin.ignore();
    
    		if ( SideC < SideA ) 
    		{
    			std::cerr << "ERROR: The sides can't be bigger then the hypotenuse. \n";
    
    			return EXIT_FAILURE;
    		}
    
    		SideB = sqrt( pow( SideC, 2) - pow( SideA, 2) );
    
    		std::cout << "Missing side = " << SideB << std::endl;
    
    	} else if ( GetInfo != "no" || GetInfo != "yes" ) {
    
    		std::cout << "ERROR: Case Sensitive or Spelling \n";
    
    		return EXIT_FAILURE;
    	}
    
    	std::cin.get();
    
    	return(0);
    }
    Any advice would be appreciated and I thank you in advance.
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    2
    Code:
    else if ( GetInfo != "no" || GetInfo != "yes" )
    I don't think this condition matters, but it should be && instead of ||. The way you have it, it will always evaluate to true.

  3. #3
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    You could get by with SideA, SideB, and Hyp I would think, omitting SideC.
    I probably would have organized my 3 sides within a structure.
    Also I noticed you have included <string> you can swap all input to all uppercase or all lowercase to omit the caps problems

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,202
    pow(x, 2) would be more simply expressed as x*x.

    Also, one trouble with computing sqrt(x*x + y*y) is that it will overflow for large x and/or y, even if the result (after computing the square root) can be represented in a double.

    One common trick to get around this would be;
    Code:
    // your approach
    
    Hyp = sqrt( pow( SideA, 2) + pow( SideB, 2));    // blech!!!
    
    // slightly better
    
    Hyp = sqrt(SideA*SideA + SideB*SideB);     //  can overflow for big values of SideA or SideB
    
    // better approach if you want to reduce chances of overflow
    
    double temp;
    if (sideA > sideB)
    {
       temp = sideB/sideA;                            // guaranteed to produce a result < 1
        Hyp = sideA * sqrt(1.0 + temp*temp); //  1.0 + temp*temp is guaranteed < 2
    }
    else
    {
        temp = sideA/sideB;                            // guaranteed to produce a result <= 1
        Hyp = sideB * sqrt(1.0 + temp*temp); //  1.0 + temp*temp is guaranteed <= 2
    }

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    Quote Originally Posted by grumpy View Post
    Also, one trouble with computing sqrt(x*x + y*y) is that it will overflow for large x and/or y, even if the result (after computing the square root) can be represented in a double.
    The answer to that problem is _hypot. Of course that defeats the purpose of what the OP is trying to learn here though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    ?

    Quote Originally Posted by QuestionKing View Post
    Also I noticed you have included <string> you can swap all input to all uppercase or all lowercase to omit the caps problems
    Can you show me how to do that ? or send me a example? help would be appreciated.

    Thanks.
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  7. #7
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Here is a tutorial on this site for converting strings to upper case.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,202
    Quote Originally Posted by iMalc View Post
    The answer to that problem is _hypot. Of course that defeats the purpose of what the OP is trying to learn here though.
    Which is why I didn't bother to mention it .....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program drops ws and other tokens
    By slippy in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2007, 04:21 AM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Comment this Program
    By kishorepalle in forum C Programming
    Replies: 11
    Last Post: 10-05-2004, 06:41 AM
  4. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  5. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 09:49 PM

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