-1

I recently finished this code. It will ask the user what type of formula it wants to use for calculating the area of the triangle. What I want to know is how I can improve this code and to make it more efficient.

The 3 ways are:

  1. Heron's Formula (uses sqrt)
  2. The usage of angle
  3. The normal way
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int formula;
    cout<<"Pick a Formula for computing the area for triangles"<<endl;
    cout<<"1. Heron's Formula"<<endl;
    cout<<"2. 2 sides and an angle"<<endl;
    cout<<"3. Given a base and a Height"<<endl;
    cout<<"Formula";
    cin>>formula;

    switch (formula){

        case 1:
            int a=0, b=0, c=0, sa=0, s=0, rt=0;
            cout<<"Formula is sqrt s(s-a)(s-b)(s-c) where s is (a+b+c)/2"<<endl;
            cout<<"Input Parameters"<<endl;
            cout<<"a";
            cin>>a;
            cout<<"b";
            cin>>b;
            cout<<"c";
            cin>>c;
            sa=(a+b+c)/2;
            s=sa*(sa-a)*(sa-b)*(sa-c);(s);
            rt=sqrt(s);
            cout<<"The area of the triangle is "<<rt<< "square units";
            break;
    }
    switch (formula){
        case 2:
            int angle=0, a=0, b=0, zi=0, za=0;
            cout<<"Formula is 1/2ab sin (angle)"<<endl;
            cout<<"Input Parameters"<<endl;
            cout<<"Angle";
            cin>>angle;
            cout<<"a";
            cin>>a;
            cout<<"b";
            cin>>b;
            za=(a*b)/2;
            zi=za*(sin(angle));             
            cout<<"The area of the triangle is "<<zi<<" square units";
            break;
    }
    switch (formula){
        case 3:
            int ba=0, h=0, area=0;
            cout<<"Formula is 1/2bh"<<endl;
            cout<<"Input Parameters"<<endl;
            cout<<"b";
            cin>>ba;
            cout<<"h";
            cin>>h;
            area=ba*h/2;    
            cout<<"The area of the triangle is "<<area<<" square units";
            break;
    }
}

There's no problem with it. It works perfectly fine. All I want to know is if it could be made in a way to occupy less space and for it to be more efficient. Anyways, feel free to use this code if you come pass this question.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 5
    Welcome to Stack Overflow! If you want help improving working code, you should post this on [CodeReview.SE](https://codereview.stackexchange.com/help/how-to-ask). If you do decide to do so, please delete the question here. – NathanOliver Jan 08 '21 at 20:07
  • 4
    You should be using 1 `switch` statement with 3 `case` blocks, not 3 `switch` statements with 1 `case` block each. Personally, I would get rid of the `switch` entirely, move the 3 formulas into separate functions/lambdas, put pointers to them into a 3-element array, and use the user's input to index into that array to call the chosen function directly. – Remy Lebeau Jan 08 '21 at 20:13

1 Answers1

1

The question is not clear. But what you can do here is to use a single switch statement like this :

switch(formula) {
case 1 : {
         // code
         break;
         }
case 2 : {
         // code
         break;
         }
}

Note: according to this question -> Why can't variables be declared in a switch statement? and because you declare variables inside, you should define a scope using curly braces

  • 1
    You could also write 3 small functions for each of the formulae, to make the body of the `switch` shorter. – cigien Jan 09 '21 at 00:40