Pythagorean Programming

This is a discussion on Pythagorean Programming within the C++ Programming forums, part of the General Programming Boards category; Hello, Everyone Tonight, I was writing a pythagorean program which ask for the two sides and gives the hypothenuse of ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    30

    Pythagorean Programming

    Hello, Everyone

    Tonight, I was writing a pythagorean program which ask for the two sides and gives the hypothenuse of three different triangles. Here is the program:

    Code:
    #include<iostream>
    using namespace std;
    
    #include<iomanip>
    using std::setw;
    using std::setprecision;
    using std::setiosflags;
    
    #include<cmath>
    
    double hypotenuse(double, double);
    
    int main()
    {
         double side1, side2, theHypotenuse;
         int first = 1;
    
         cout << "Enter the lengths of the two sides: ";
    
         for( int i = 1; i <= 3; i++)
         {
         cin  >> side1 >> side2;
    
        if( first )
           {
            cout << setw( 5 ) << "Triangle" << setw( 12 ) << "Side 1"
                 << setw( 14 ) << "Side 2" << setw( 20 ) << "Hypotenuse"
                 << "\n";
            first = 0; // prevents this from printing again
           }
        cout << setiosflags( ios::fixed | ios::showpoint ) << setw( 3 )
             << i << setw( 15 ) << setprecision( 1 ) << side1 << setw( 15 )
             << setprecision( 1 ) << side2 << setw( 15 ) << setprecision ( 1 )
             << hypotenuse << "\n";
         }
         theHypotenuse = hypotenuse(side1, side2); // The function call
         
         return 0;
    }
    
    double hypotenuse(double side1, double side2)
    {
         double hypot, A;
         A = side1 * side1 + side2 * side2;
         hypot = sqrt( A );
            
         return(hypot);
    }
    The program compiles, however, I get the following result:
    Code:
    <pegasus> g++ hypotenuse.cpp
    <pegasus> a.out
    Enter the lengths of the two sides: 3.0 4.0 5.0 12.0 8.0 15.0
    Triangle      Side 1        Side 2          Hypotenuse
      1            3.0            4.0              1
      2            5.0           12.0              1
      3            8.0           15.0              1
    <pegasus>
    Unfortunately, the answers for the hypotenuse I get are 1.
    How can I input into the program to give me the real answer for the hypotenuse as the following:
    Code:
    Triangle      Side 1        Side 2          Hypotenuse
      1            3.0            4.0             5.0
      2            5.0           12.0            13.0
      3            8.0           15.0            17.0
    Please someone help me to figure out how to put the real answer as part of the hypotenuse table.

    Thank you in advance for all your help.!
    Last edited by mikeprogram; 10-30-2005 at 01:29 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    You were for some reason using the name of the function as output and calling it afterward. That doesn't work. I should also mention that you don't need to define std::setw or any iomanip functions.

    It should be like this:
    Code:
    #include<iostream>
    #include<iomanip>
    #include<cmath>
    
    using namespace std;
    
    double hypotenuse(double, double);
    
    int main()
    {
         double side1, side2;
         int first = 1;
    
         cout << "Enter the lengths of the two sides: ";
    
         for( int i = 1; i <= 3; i++)
         {
         cin  >> side1 >> side2;
    
        if( first )
           {
            cout << setw( 5 ) << "Triangle" << setw( 12 ) << "Side 1"
                 << setw( 14 ) << "Side 2" << setw( 20 ) << "Hypotenuse"
                 << "\n";
            first = 0; // prevents this from printing again
           }
        cout << setiosflags( ios::fixed | ios::showpoint ) << setw( 3 )
             << i << setw( 15 ) << setprecision( 1 ) << side1 << setw( 15 )
             << setprecision( 1 ) << side2 << setw( 15 ) << setprecision ( 1 )
             << hypotenuse(side1, side2) << "\n";
         }
         
         return 0;
    }
    
    double hypotenuse(double side1, double side2)
    {
         double hypot, A;
         A = side1 * side1 + side2 * side2;
         hypot = sqrt( A );
            
         return(hypot);
    }
    Output:
    Code:
    Enter the lengths of the two sides: 3.0 4.0 5.0 12.0
    Triangle      Side 1        Side 2          Hypotenuse
      1            3.0            4.0            5.0
      2            5.0           12.0           13.0
    Last edited by SlyMaelstrom; 10-30-2005 at 02:01 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    30
    SlyMaelstrom:

    I got your message. Once again, I could solve the problem with inputting hypotenuse answer as a table. Thank you for your tips.
    I did not know you don't need to define std::setw or any iomanip functions in these types of program.

    Sincerely, thank you for your help and your precious time. !

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    when doing
    "using namespace std;" there is no need for
    "using std::<blah>" since its already taken care of

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pythagorean Theorem Program Comment, criticize, or whatever
    By Sshakey6791 in forum C++ Programming
    Replies: 7
    Last Post: 02-13-2009, 01:48 AM
  2. Problem with Pythagorean triple etc.
    By -Prime- in forum C Programming
    Replies: 10
    Last Post: 10-14-2006, 01:50 AM

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