1
#include <iostream>
#include <string>

using namespace std;

void ReadTriangleLegs(float& a, float& b)
{

    cout << "Please enter Triangle Leg 1 : ";
    cin >> a;
    cout << "Please enter Triangle Leg 2 : ";
    cin >> b;

}

float CalculateCircleArea(float a, float b)
{
    const float PI = 3.14159265359;
    float Area = PI * (pow(b, 2) / 4) * ((2 * a - b) / (2 * a + b));
    return Area;
}

void PrintResults(float Area)
{
    cout << "The Circle Area is : " << Area << endl;
}

int main()
{
    float a, b;
    ReadTriangleLegs(a, b);
    PrintResults(CalculateCircleArea(a, b));

    return 0;
}

This is a code to calculate a circle area Inscribed in an Isosceles Triangle and I want to know why when I change the variable type for (a & b) from float to int I get Area = 0 ?

Mariama
  • 11
  • 1
  • Then `((2 * a - b) / (2 * a + b)` uses _integer division_ (returning a truncated `int` too). Hence `3 / 5 == 0` and such. – Joop Eggen Sep 01 '22 at 10:32
  • 1
    Read about integer division in your favourite C++ book. – molbdnilo Sep 01 '22 at 10:32
  • If you are expecting them to be converted to `float` before (or "inside") the calculation of `Area` because the ultimate result is floating point, that's not how it works. Dividing two integers is always an integer division. – molbdnilo Sep 01 '22 at 10:41
  • So, you've posted code that works correctly, and you're asking people to rewrite it according to your instructions to produce code that doesn't work? Don't do that. Post the code that you're asking about. – Pete Becker Sep 01 '22 at 12:52
  • @PeteBecker No that is not my question I don't want to rewrite the code, Joop Eggen answered my question perfectly.. But thank you all for your time – Mariama Sep 02 '22 at 11:25
  • @Mariama -- "when I change the variable type ... from float to int I get Area = 0": you're asking about code that's different from what you posted. – Pete Becker Sep 02 '22 at 12:42

0 Answers0