Algorithm for my codes

Please can somebody make an algorithm for the code below, so i can compare with what i have done. thank you!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* A program that calculates
the area of a triangle using
three distinct formulas*/

#include <iostream>
#include <cmath>

using namespace std;

int areaOfTriangle(); // user-defined function prototype

int main()
{
    areaOfTriangle();
    return 0;
}

int areaOfTriangle() //user-defined function
{
   float A; // variable declaration for side A
   float C; // variable declaration for side C
   float B; // variable declaration for breathe
   float H; // variable declaration for Height
   float X; // variable declaration for included angle in the triangle
   int Area;
   float S = (A + B + C)/2;
   int ch;

   cout << " MENU:\n please select the following options below\n "; // output stream object for interactive usage of program
   cout << " n1. Option 1 [Hint]: A= 1/2 * B * H\n";
   cout << " n2. Option 2 [Hint]: A = ABcosX\n";
   cout << " n3. Option 3 [Hint]: A = squareroot of S(S-A)(S-B)(S-C)\n";

   cin >> ch; // input stream object to enable user choose options

   switch(ch)
   {
      case 1:
      {
          cout << "plese enter values for sides B and H: ";
          cin >> B;
          cin >> H;
          Area = 0.5 * B * H;
          break;

      }

      case 2:
        {
            cout << "Please enter values for sides A and B and the included angle: ";
            cin >> A;
            cin >> B;
            cin >> X;
            Area = A * B *cos(X);
            break;
        }
      case 3:
        {
            cout << "Please enter values for A, B and C: ";
            cin >> A;
            cin >> B;
            cin >> C;
            S = (A + B + C)/2;
            Area = sqrt(S*(S-A)*(S-B)*(S-C));
            break;
        }
      default: cout << "The option chosen is a wrong one. please read the instructions and follow suit ";
      break;
   }

   cout << "The area of the triangle is: " << Area << endl;

}
Last edited on
I told you in your last post 15 minutes ago to use CODE TAGS for your code, Edit your post and use them - http://www.cplusplus.com/articles/jEywvCM9/

The code is hard to read otherwise.
okay
Thanks for fixing your code at the top
Last edited on
that has been done
A few points you should address:
1. Why Area is an int? It does not make sense to multiply floats and the answer to be int
2. On line 26, you define S in terms of A,B,C, but those values are not defined at that point.
3. Is the X angle in degrees? Then you should convert the input to radians. The definition of the trigonometric functions in <cmath> use radians. http://www.cplusplus.com/reference/cmath/cos/
4. The formula for case 2 is wrong. If I have a right angle triangle, with A=B=1, the area should be A*B/2=0.5. If you insert X=90 degrees, cos(90 degrees)=0, so you get 0.
Topic archived. No new replies allowed.