By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

Totally lost help Triangle program

P: 5
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

"equilateral" 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<iostream>
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<<"Equilateral Traingle\n";
if(a==b || c==a || b==c)
cout<<"Isosceles 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
Oct 25 '06 #1
Share this Question
Share on Google+
5 Replies


100+
P: 180
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

Expand|Select|Wrap|Line Numbers
  1. void getsides(int& a, int& b, int& c)
  2. {    
  3.     cout<<"Enter side a: ";
  4.     cin>>a;
  5.     cout<<"Enter side b: ";
  6.     cin>>b;
  7.     cout<<"Enter side c: ";
  8.     cin>>c;
  9. }
  10.  
  11. void swap (int& a , int& b) 
  12.     int temp;
  13.     temp = a; 
  14.     a = b; 
  15.     b = temp; 
  16.  
  17. void triangle (int a, int b, int c)
  18. {
  19.     if(a+b<c)
  20.       cout<<"Cannot form a triangle!";
  21.     if(b+c<a)
  22.      cout<<"Cannot form a triangle!";
  23.     if(a+c<b)
  24.      cout<<"Cannot form a triangle!";
  25. }
  26.  
  27. void angletype(int a, int b, int c)
  28. {    
  29.     if((c*c) == ((a*a) + (b*b)))
  30.           cout<<"Right";
  31.     if((c*c) > ((a*a) + (b*b)))
  32.           cout<<"Obtuse";
  33.     if((c*c) < ((a*a) + (b*b)))
  34.           cout<<"Acute";
  35. }
  36.  
  37. void sidetype(int a, int b, int c)
  38. {    
  39.      if(a==b==c)
  40.            cout<<"Equilateral Traingle\n";
  41.      if(a==b || c==a || b==c)
  42.            cout<<"Isosceles Traingle\n";
  43.      else 
  44.            cout<<"Scalene Triangle\n";
  45. }
  46.  
  47. void sortsides(int& a, int& b, int& c)
  48. {
  49.      if (a>b) 
  50.     swap(a, b); 
  51.     if (b>c) 
  52.                 swap(b, c); 
  53.     if (a>b) 
  54.     swap(a, b);         
  55. }
  56.  
  57. int main() 
  58.        int a, b, c;
  59.        getsides(a, b, c);
  60.        while(1)
  61.        {
  62.     if(a==0 && b==0 && c==0)
  63.      break;
  64.     sortsides(a, b, c);
  65.     cout<<a<<b<<c<<endl;
  66.     getsides(a, b, c);
  67.       }    
  68. return 0;
  69. }
Thankx
Oct 25 '06 #2

P: 5
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<iostream>
using namespace std;

void sidetype(int a, int b, int c)
{
if(a==b && a==c && b==c)
cout<<"Equilateral Traingle\n";
else 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);
}



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
Oct 25 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
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.
Oct 26 '06 #4

100+
P: 180
Try out something like this

Expand|Select|Wrap|Line Numbers
  1. bool triangle (int a, int b, int c)
  2. {
  3.     if( (a+b<c) || (b+c<a) ||  (a+c<b) )
  4.      {
  5.            return false;
  6.      }
  7.    return true;
  8. }
Then in main you can test like this

Expand|Select|Wrap|Line Numbers
  1. if( triangle(a,b,c) )
  2.    cout<<"This forms a triangle"<<endl;
  3. else
  4.    cout<<"Cannot form a triangle"<<endl;
Thankx
Oct 26 '06 #5

P: 5
No thank you!
Oct 26 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.