• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

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.
0
cuong5985
Asked:
cuong5985
  • 2
1 Solution
 
brettmjohnsonCommented:
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.
0
 
brettmjohnsonCommented:
Man, I really must proofread my posts before submitting.  Consider this
easier to read version:

You seem to be writing the code blindly, without any sense of
design or purpose.   Although you have generated twice as much
code as a reasonable solution to the problem requires, you have only
managed to input 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 (and the code you have
posted shows no indication of OO design), 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 the 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 find 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 (they 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

4.5) Keep in mide that C and C++ integer arithmetic truncates results
(rounds down) to the next lowest integer.  So multiplying two integers,
then dividing by 2 will not yield fractional results.  You will need to use
floating point computation in your program to achieve accurate results.
Consider using floats or doubles in your calculations or even your coordinates.

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.
0
 
novitiateCommented:
good points brettmjohnson.

_novi_
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now