Totally lost help Triangle program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • singhm
    New Member
    • Oct 2006
    • 5

    Totally lost help Triangle program

    Hi guys so I have a trianlge program having hard time finishing this though,
    I have to develop a program which is the following:

    Write a program that will allow the user to enter the 3 lengths and then tell what kind of triangle the 3 lengths form.
    Function main should consist mainly of a loop and calls to functions. Let the functions do the work.
    (Think of main as the boss who delegates all of the work to others.)

    main: loops until 0,0,0 is entered as the sides. For each set of sides:

    * Sort the 3 sides A, B, and C such that A<=B<=C If you sort them first everything else gets easier!
    * If they do form a triangle print the side type and the angle type.
    * If they do not form a triangle, print an appropriate message.

    The lengths are supposed to be the sides of a triangle, but it is possible to enter 3 lengths that do not form a triangle: for example 3, 5, and 12. (Why can't you make a triangle with those sides? Think in terms of if statements.

    A function sortSides that will assign the lengths of the sides to A, B, and C such that A<=B<=C
    A function swap that will swap 2 values - called by sortSides
    A function sideType that will print

    "equilatera l" if the lengths of all 3 sides are equal
    "isosceles" if any two sides (but not all 3) are equal
    "scalene" if no sides are equal

    A function angleType that will print

    "right" if C*C = A*A + B*B
    "obtuse" if C*C > A*A + B*B
    "acute" if C*C < A*A + B*B

    THIS IS WHAT I GOT ANY SUGGESTIONS WOULD BE GREATLY APPRECIATED SO I DONT FAIL. THANKS
    #include<iostre am>
    using namespace std;

    void getsides()
    {int a,b,c;

    cout<<"Enter side a: ";
    cin>>a;
    cout<<"Enter side b: ";
    cin>>b;
    cout<<"Enter side c: ";
    cin>>c;

    }

    void swap (int& a , int& b);
    { int temp;
    temp = a;
    a = b;
    b = temp;
    }

    void triangle (int a, int b, intc);
    {

    if(a+b<c)
    cout<<"Cannot form a triangle!";
    if (b+c<a)
    cout<<"Cannot form a triangle!";
    if (a+c<b)
    cout<<"Cannot form a triangle!";
    }

    void angletype()
    {int a,b,c;
    if(c*c=a*a + b*b)
    cout<<"Right";
    if(c*c>a*a + b*b)
    cout<<"Obtuse";
    if(c*c<a*a + b*b)
    cout<<"Acute";
    }

    void sidetype()
    { int a,b,c;
    if(a==b==c)
    cout<<"Equilate ral Traingle\n";
    if(a==b || c==a || b==c)
    cout<<"Isoscele s Traingle\n";
    else cout<<"Scalene Triangle\n";
    }



    void main()
    { int a, b, c;
    cout<<"Enter the first side:";
    cin>>a;
    cout<<"Enter the second side:";
    cin>>b;
    cout<<"Enter the third side:";
    cin>>c;
    if (a>b)
    swap(a, b);
    if (b>c)
    swap(b, c);
    if (a>b)
    swap(a, b);

    while(getsides( a,b,c,);!=0,0,0 )
    {sortsides (a,b,c):
    trainlge(a,b,c) ;
    {sidetype(a,b,c );
    angletype(a,b,c );
    }
    else cout<<"Not a traingle "<<a<<" "<<b<<" "<<c<<endl;

    } getsides ();
    return 0;
    } //main
  • vermarajeev
    New Member
    • Aug 2006
    • 180

    #2
    This is not the complete program, I have solved almost 80%, remaining try to do by yourself...If I give you the entire answer you will not learn anything...So try out after this

    Code:
    void getsides(int& a, int& b, int& c)
    {	
        cout<<"Enter side a: ";
        cin>>a;
        cout<<"Enter side b: ";
        cin>>b;
        cout<<"Enter side c: ";
        cin>>c;
    }
    
    void swap (int& a , int& b) 
    { 
        int temp;
        temp = a; 
        a = b; 
        b = temp; 
    } 
    
    void triangle (int a, int b, int c)
    {
        if(a+b<c)
      	cout<<"Cannot form a triangle!";
        if(b+c<a)
     	cout<<"Cannot form a triangle!";
        if(a+c<b)
     	cout<<"Cannot form a triangle!";
    }
    
    void angletype(int a, int b, int c)
    {	
        if((c*c) == ((a*a) + (b*b)))
              cout<<"Right";
        if((c*c) > ((a*a) + (b*b)))
              cout<<"Obtuse";
        if((c*c) < ((a*a) + (b*b)))
              cout<<"Acute";
    }
    
    void sidetype(int a, int b, int c)
    {	
         if(a==b==c)
               cout<<"Equilateral Traingle\n";
         if(a==b || c==a || b==c)
               cout<<"Isosceles Traingle\n";
         else 
               cout<<"Scalene Triangle\n";
    }
    
    void sortsides(int& a, int& b, int& c)
    {
         if (a>b) 
    	swap(a, b); 
        if (b>c) 
                    swap(b, c); 
        if (a>b) 
    	swap(a, b); 		
    }
    
    int main() 
    { 
           int a, b, c;
           getsides(a, b, c);
           while(1)
           {
    	if(a==0 && b==0 && c==0)
     	break;
    	sortsides(a, b, c);
    	cout<<a<<b<<c<<endl;
    	getsides(a, b, c);
          }	
    return 0;
    }
    Thankx

    Comment

    • singhm
      New Member
      • Oct 2006
      • 5

      #3
      Hey- Can you help me out on this?
      Everything wokrs for me now I jsut need the program to end after the user enters in sides such as; 1-1-4 it outputs cannot form a triangle which is correct but it goes on to say side type & angle type functions, i need it to stop after cannot form a triangle the simplest way , any thoughts ??






      #include<iostre am>
      using namespace std;

      void sidetype(int a, int b, int c)
      {
      if(a==b && a==c && b==c)
      cout<<"Equilate ral Traingle\n";
      else if(a==b || c==a || b==c)
      cout<<"Isoscele s Traingle\n";
      else cout<<"Scalene Triangle\n";
      }


      void sortsides(int&a , int&b, int&c)
      { if (a>b)
      swap(a, b);
      if (b>c)
      swap(b, c);
      if (a>b)
      swap(a, b);
      }



      void getsides(int&a, int&b, int&c)
      {

      cout<<"Enter side a: ";
      cin>>a;
      cout<<"Enter side b: ";
      cin>>b;
      cout<<"Enter side c: ";
      cin>>c;

      }

      void swap (int&a, int&b)
      {
      int temp;
      temp=a;
      a=b;
      b=temp;
      }


      void triangle (int a, int b, int c)
      {
      if(a+b<c)
      cout<<"Cannot form a triangle!";
      if (b+c<a)
      cout<<"Cannot form a triangle!";
      if (a+c<b)
      cout<<"Cannot form a triangle!";
      }

      void angletype (int a,int b,int c)
      {
      if((c*c)==((a*a ) + (b*b)))
      cout<<"Right Triangle \n";
      if((c*c)>((a*a) + (b*b)))
      cout<<"Obtuse Triangle \n";
      if((c*c)<((a*a) + (b*b)))
      cout<<"Acute Triangle \n";
      }

      int main()
      {
      int a, b, c;
      getsides(a,b,c) ;
      while(1)
      {
      if(a==0 && b==0 && c==0){
      cout<<"Not a triangle "<<a<<" "<<b<<" "<<c<<endl;
      break;
      }
      sortsides (a,b,c);
      swap (a,b);
      triangle(a,b,c) ;
      {sidetype(a,b,c );
      angletype(a,b,c );
      }
      getsides(a,b,c) ;
      }
      return 0;
      }


      thanks

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        perhaps you should return a value from the function triangle(...) indicating if it actually is or isn't a triangle and then only call the other functions if it is a triangle.

        Comment

        • vermarajeev
          New Member
          • Aug 2006
          • 180

          #5
          Try out something like this

          Code:
          bool triangle (int a, int b, int c)
          {
              if( (a+b<c) || (b+c<a) ||  (a+c<b) )
               {
                     return false;
               }
             return true;
          }
          Then in main you can test like this

          Code:
          if( triangle(a,b,c) )
             cout<<"This forms a triangle"<<endl;
          else
             cout<<"Cannot form a triangle"<<endl;
          Thankx

          Comment

          • singhm
            New Member
            • Oct 2006
            • 5

            #6
            No thank you!

            Comment

            Working...