Link to home
Start Free TrialLog in
Avatar of cuong5985
cuong5985

asked on

Structure

program to calculate the area of the triangle
this is my program.
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>



struct Coords
{
      int x;
   int y;
};

struct Line
{
      Coords A;
   Coords B;
};

void DisplayCoords(Coords Point);
void SetCoords(Coords &Point, int x, int y);
void GetCoords(Coords &Point);
void SetLine(Line &AB, Coords A, Coords B);
void DisplayLine(Line AB);


main()
{
   Coords A, B;
   Line AB;
   
   cout<<"A: "<<endl;
   GetCoords(A);
   
   cout<<"B: "<<endl;
   GetCoords(B);

   SetLine(AB, A, B);

   DisplayLine(AB);

   getch();
}

void GetCoords(Coords &Point)
{
      int x, y;
       cout<<">>";
    cin>>x;
       cout<<">>";
    cin>>y;
    SetCoords(Point, x, y);

}
void SetCoords(Coords &Point, int x, int y)
{
       Point.x = x;
    Point.y = y;


}

void SetLine(Line &AB, Coords A, Coords B)
{
       AB.A = A;
    AB.B = B;
}

void DisplayLine(Line AB)
{
   cout<<endl<<"{";
   DisplayCoords(AB.A);
   cout<<", ";
   DisplayCoords(AB.B);
   cout<<"}"<<endl;

}
void DisplayCoords(Coords Point)
{
   cout<<"("<<Point.x<<", "<<Point.y<<")";
}

But what should i do next.
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

You seem to be writing the code blindly, without any sense of
design or purpose.   Although you have generated twice as much
code as reasonable solution to the problem requires, you have only
managed to input only 1 of the 3 sides of the triangle and haven't
even touched on the core problem (finding the area of the triangle).

1)  This introductory programming problem doesn't require Object-Oriented
techniques, although an OO approach does present itself.  If you do not
wish to use OO techniques to solve the problem, don't choose an object-oriented
implementation language.  Stick to ANSI C rather than C++.  If you
do want to use an OO approach, see item 5 below.

2) Try to isolate your UI.  You prompt for the coordinates outside of
the getCoords() function, and again prompt inside.  Try to keep output
to the user and input from the user confined to routine, so you could
possibly replace it (for instance reading input from a dialog box, or from
a file).  In your case the prompt differs slightly, so pass the prompt string
into the function:
void getPoint(struct coords *aPoint, char * name)
{
    int x, y;
    printf("Please enter the coordinates of point %s: ", name);
    ....  // read the input
    aPoint->x = x;      // set the coordinates with the data
    aPoint->y = y;
    printf("You entered the point (%d, %d)\n", x, y);
}


3) You might consider it easier to consider the triangle as 3 points rather
than 3 line segments.  3 [distinct] points define a triangle, whereas 3 line
segments might not (the must share endpoints, create a cycle, etc).
Define your triangle by establishing its 3 points.  Knowing the coordinates
of the points, you can calculate the lengths of the sides, included angles, etc.

4) Know how to solve the problem.  The core problem here is finding the
area of a triangle.  You can't futz around inputting coordinates all day,
you need to know the underlying math involved.  This page discusses a
couple of different formulae for calculating the area of a triangle:
http://regentsprep.org/Regents/mathb/5E1/areatriglesson.htm
It doesn't matter which method you choose, but either one will also require
you to calculate the hypotenuse of a right triangle:
http://mathworld.wolfram.com/RightTriangle.html

5) If you are really going to use OO techniques, you should define at least
these three classes:  Point, Triangle, and RightTriangle (derives from Triangle).
Hint, the class Triangle should implement constructor method Triangle(Point, Point, Point)
as well as a Triangle::area() method.
ASKER CERTIFIED SOLUTION
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of novitiate
novitiate

good points brettmjohnson.

_novi_