Solved

# Structure

Posted on 2005-05-09
190 Views
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
Question by:cuong5985

LVL 23

Expert Comment

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);
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

LVL 23

Accepted Solution

Man, I really must proofread my posts before submitting.  Consider this

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);
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

LVL 8

Expert Comment

good points brettmjohnson.

_novi_
0

## Featured Post

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.