Knowee
Questions
Features
Study Tools

#include <iostream>#include <string>#include <cmath>using namespace std;class Triangle{public: void print_area(int s1, int s2, int s3) { double s = (s1+s2+s3)/2.0; cout << s << endl; cout << "Perimeter is " << (s1+s2+s3) << endl; }};int main(){ Triangle t; t.print_area(3,4,5); return 0;}

Question

#include <iostream>#include <string>#include <cmath>using namespace std;class Triangle{public: void print_area(int s1, int s2, int s3) { double s = (s1+s2+s3)/2.0; cout << s << endl; cout << "Perimeter is " << (s1+s2+s3) << endl; }};int main(){ Triangle t; t.print_area(3,4,5); return 0;}

🧐 Not the exact question you are looking for?Go ask a question

Solution

The code you've provided is written in C++. It defines a class Triangle with a method print_area that calculates the semi-perimeter of a triangle given its three sides. However, the method name is misleading because it doesn't actually calculate the area of the triangle, but rather its semi-perimeter and perimeter.

Here's how you can modify the code to calculate the area using Heron's formula:

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

class Triangle{
public:
    void print_area(int s1, int s2, int s3)
    {
        double s = (s1+s2+s3)/2.0;
        double area = sqrt(s*(s-s1)*(s-s2)*(s-s3));
        cout << "Area is " << area << endl;
        cout << "Perimeter is " << (s1+s2+s3) << endl;
    }
};

int main(){
    Triangle t;
    t.print_area(3,4,5);
    return 0;
}

In this modified code, the print_area method now calculates the area of the triangle using Heron's formula, which is sqrt(s*(s-a)*(s-b)*(s-c)) where s is the semi-perimeter and a, b, and c are the sides of the triangle. The area is then printed to the console.

This problem has been solved

Similar Questions

0/0

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.