SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Parameters And Overloading
Exercise:
Programming Projects
Question:2 | ISBN:9780132846813 | Edition: 5

Question

The area of an arbitrary triangle can be computed using the formula

where a, b, and c are the lengths of the sides, and s is the semiperimeter.

Write a void function that uses five parameters: three value parameters that provide the lengths of the edges, and two reference parameters that compute the area and perimeter ( not the semiperimeter ). Make your function robust. Note that not all combinations of a, b, and c produce a triangle. Your function should produce correct results for legal data and reasonable results for illegal combinations.



TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#include<iostream>  
#include<cmath>  
using namespace std;  
  
int main()  
{  
double area,perimeter;  
int a,b,c;  
void compute(double&,double&,double,double,double);  
cout << "Enter the 3 side lengths of the triangle: \n";  
cout << " Side 1: "; cin >> a;  
cout << " Side 2: "; cin >> b;  
cout << " Side 3: "; cin >> c;  
compute(area,perimeter,a,b,c);  
  
return 0;  
}  
  
void compute (double& area, double& perimeter, double a, double b, double c) {  
   double s;  
      
   if( (a <= 0) || (b <= 0) || (c <= 0) )  
    {   
    cout<<"A triangle cannot have a side of zero or less!"<<endl;  
    return;  
    }  
    else if( (a > (b + c)) || (b > (a + c)) || (c > (b + a)) )  
    {   
    cout<<"This combination will not make a legal triangle!"<<endl;  
    return;  
    }  
   else  
   {  
   s = (a + b + c)/ 2.0;  
   area = sqrt (s*(s-a)*(s-b)*(s-c));  
   perimeter = (a + b + c);  
   cout << "The area of a triangle with side lengths of " << a << ", " << b << " and " << c << " is " << area << " and the perimeter is "<< perimeter << endl;  
  
   }  
   return;  
}  

 

0 0

Discussions

Post the discussion to improve the above solution.