In: Computer Science
You have to write a program that computes the area of a triangle. Input consists of the three points that represent the vertices of the triangle. Points are represented as Cartesian units i.e. X,Y coordinates as in (3,5). Output must be the three points, the three distances between vertices, and the area of the triangle formed by these points. The program must read the coordinates of each point, compute the distances between each pair of them and print these values. Next, it must calculate the area and print its value. All input are read from keyboard. All output must be printed out on the terminal. Solution requirements: 1) Your program must solve the problem using the following functions: 10 points. getPoint(): Receives the input data. It reads from the keyboard the coordinates (x, y) corresponding to a single vertex of the triangle and returns both of them rounded to a single decimal place to the caller. 15 points. calcLength(): Receives the coordinates of two points and returns the distance between them (calculated using the equation provided below) rounded to ten decimal places. ________________ Length between points (x1,y1) and (x2,y2) = √(x1-x2)2+(y1-y2)2 That is, the length is the square root of the square of the distance between the x coordinates plus the square of the distance between the y coordinates. 15 points. semiPerimeter(): Receives three lengths and returns the value of the semi perimeter (calculated using the equation provided below) rounded to ten decimal places. Semi perimeter = ½ * (ab + bc + ca) Where ab, bc and ca are the lengths between points a and b, b and c, and c and a respectively. 15 points. calcArea(): Receives three lengths and returns the area of the triangle (calculated using the equation provided below) rounded to two decimal places. This function must call semiPerimeter() to calculate the value for s before calculating the area of the triangle. ________________________ Area = √(s*(s-ab)*(s-bc)*(s-ca)) Where s is the semi perimeter and ab, bc, and ca are the three distances between the three points. 10 points. printDistance(): Receives the output file, the coordinates of two points, and the distance between them and prints a message similar to the following one: The distance between (1.0,1.2) and (6.0,6.0) is 6.93 20 points. square(): Receives a value and returns its square rounded to two decimal places. You must use it to calculate the squares of the distances of the x and y coordinates in calcLength(). Do NOT use pow() to implement this function. 15 points. round_off(value, places): Receives a value (double precision real number) and a number indicating a quantity of decimal places (whole number) and returns the value rounded to the specified number of places. Your main() function must: • Call getPoint(), calcLength(), and printDistance() as many times as needed • Call calcArea() • Format the output so real numbers are printed in fixed format • Print the area of the triangle to the terminal. Note: The coordinates of vertices must be printed with a single decimal digit while the distances and the area must be printed with 2 decimal digits. Unless specified otherwise all the values are double precision real numbers. You can declare all the variables that you need. All the functions must be declared below main().
2) You can use ONLY the material learned and used in the lectures/Pas/lab assignments.
3) Your program must pass all my tests.
4) You must choose the most appropriate type of function and type of parameters for each of the functions described above
Must be in C++ please insert into code below
#include <iostream> // to use cin and cout
#include <typeinfo> // to be able to use operator typeid
// Include here the libraries that your program needs to compile
using namespace std;
// Ignore this; it's a little function used for making tests
inline void _test(const char* expression, const char* file, int line)
{
cerr << "test(" << expression << ") failed in file " << file;
cerr << ", line " << line << "." << endl << endl;
}
// This goes along with the above function...don't worry about it
#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))
// Insert here the prototypes of the functions
int main()
{
system("pause");
// Do NOT remove or modify the following statements
cout << endl << "Testing your solution" << endl << endl;
test(fabs(round_off(calcLength(1.0, 1.2, 6.0, 6.1), 2) - 7.00) < .001); // Incorrect calculation of length
test(fabs(round_off(calcLength(6.0, 6.1, 3.2, 6.5), 2) - 2.83) < .001); // Incorrect calculation of length
test(fabs(round_off(calcLength(1.0, 1.2, 3.2, 6.5), 2) - 5.74) < .001); // Incorrect calculation of length
test(fabs(calcArea(calcLength(1.0, 1.2, 6.0, 6.1), calcLength(6.0, 6.1, 3.2, 6.5), calcLength(1.0, 1.2, 3.2, 6.5)) - 7.86) < .001); // Incorrect calculation of area
test(fabs(round_off(calcLength(1.2, 1.2, 7.6, 4.3), 2) - 7.11) < .001); // Incorrect calculation of length
test(fabs(round_off(calcLength(7.6, 4.3, 9.2, 3.4), 2) - 1.84) < .001); // Incorrect calculation of length
test(fabs(round_off(calcLength(1.2, 1.2, 9.2, 3.4), 2) - 8.30) < .001); // Incorrect calculation of length
test(fabs(calcArea(calcLength(1.2, 1.2, 7.6, 4.3), calcLength(7.6, 4.3, 9.2, 3.4), calcLength(1.2, 1.2, 9.2, 3.4)) - 5.36) < .001); // Incorrect calculation of area
test(fabs(round_off(calcLength(1.0, 1.0, 5.0, 5.0), 2) - 5.66) < .001); // Incorrect calculation of length
test(fabs(round_off(calcLength(5.0, 5.0, 9.0, 9.0), 2) - 5.66) < .001); // Incorrect calculation of length
test(fabs(round_off(calcLength(1.0, 1.0, 9.0, 9.0), 2) - 11.31) < .001); // Incorrect calculation of length
test(fabs(calcArea(calcLength(1.0, 1.0, 5.0, 5.0), calcLength(5.0, 5.0, 9.0, 9.0), calcLength(1.0, 1.0, 9.0, 9.0)) - 0.00) < .001); // Incorrect calculation of area
system("pause");
return 0;
}
//************************ Function definition *************************
// Read the handout carefully for detailed description of the functions that you have to implement
I implemented all of the above given functions. I hope you can do it after this....and just arrange the code as said. The functions will work as said, you only need to arrange them. Please reach out to me in the comments if you need any help
#include <iostream> // to use cin and cout
#include <typeinfo> // to be able to use operator typeid
#include<cmath>
#include <fstream>
// Include here the libraries that your program needs to compile
using namespace std;
// Ignore this; it's a little function used for making tests
typedef struct coord{
double x;
double y;
} Coord;
float power(float x, int y)
{
float temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
{
if(y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
double round_off(double var,int r)
{
double value = (int)(var * power(10,r) + .5);
return (double)value / power(10,r);
}
Coord getPoint(){
cout<<"Enter 2 coordinates for the point"<<endl;
double x,y;
cin>>x>>y;
Coord c;
c.x=round_off(x,1);
c.y=round_off(y,1);
return c;
}
double square(double x){
return round_off(x*x,2);
}
double calcLength(double x1,double y1,double x2,double y2){
return round_off(sqrt(square(x1-x2)-square(x2-y2)),10);
}
double semiPerimeter(double a,double b,double c){
return round_off(0.5*(a*b+b*c+c*a),10);
}
double calcArea(double a,double b,double c){
double s=semiPerimeter(a,b,c);
return round_off(sqrt(s*(s-a*b)*(s-b*c)*(s-a*c)),2);
}
void printDistance(string filename,double x1,double y1,double x2,double y2){
double dist=calcLength(x1,y1,x2,y2);
ofstream myfile;
myfile.open (filename);
myfile << "The distance between ("<<x1<<","<<y1<<") and ("<<x2<<","<<y2<<") is "<<dist<<endl;
myfile.close();
}
/*
inline void _test(const char* expression, const char* file, int line)
{
cerr << "test(" << expression << ") failed in file " << file;
cerr << ", line " << line << "." << endl << endl;
}
// This goes along with the above function...don't worry about it
#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))
// Insert here the prototypes of the functions
*/
int main()
{
system("pause");
Coord n=getPoint();
cout<<n.x<<" "<<n.y<<endl;
// Do NOT remove or modify the following statements
/*
cout << endl << "Testing your solution" << endl << endl;
test(fabs(round_off(calcLength(1.0, 1.2, 6.0, 6.1), 2) - 7.00) < .001); // Incorrect calculation of length
test(fabs(round_off(calcLength(6.0, 6.1, 3.2, 6.5), 2) - 2.83) < .001); // Incorrect calculation of length
test(fabs(round_off(calcLength(1.0, 1.2, 3.2, 6.5), 2) - 5.74) < .001); // Incorrect calculation of length
test(fabs(calcArea(calcLength(1.0, 1.2, 6.0, 6.1), calcLength(6.0, 6.1, 3.2, 6.5), calcLength(1.0, 1.2, 3.2, 6.5)) - 7.86) < .001); // Incorrect calculation of area
test(fabs(round_off(calcLength(1.2, 1.2, 7.6, 4.3), 2) - 7.11) < .001); // Incorrect calculation of length
test(fabs(round_off(calcLength(7.6, 4.3, 9.2, 3.4), 2) - 1.84) < .001); // Incorrect calculation of length
test(fabs(round_off(calcLength(1.2, 1.2, 9.2, 3.4), 2) - 8.30) < .001); // Incorrect calculation of length
test(fabs(calcArea(calcLength(1.2, 1.2, 7.6, 4.3), calcLength(7.6, 4.3, 9.2, 3.4), calcLength(1.2, 1.2, 9.2, 3.4)) - 5.36) < .001); // Incorrect calculation of area
test(fabs(round_off(calcLength(1.0, 1.0, 5.0, 5.0), 2) - 5.66) < .001); // Incorrect calculation of length
test(fabs(round_off(calcLength(5.0, 5.0, 9.0, 9.0), 2) - 5.66) < .001); // Incorrect calculation of length
test(fabs(round_off(calcLength(1.0, 1.0, 9.0, 9.0), 2) - 11.31) < .001); // Incorrect calculation of length
test(fabs(calcArea(calcLength(1.0, 1.0, 5.0, 5.0), calcLength(5.0, 5.0, 9.0, 9.0), calcLength(1.0, 1.0, 9.0, 9.0)) - 0.00) < .001); // Incorrect calculation of area
system("pause");
*/
return 0;
}
//************************ Function definition *************************
// Read the handout carefully for detailed description of the functions that you have to implement