Question
/* write a program that computes the area of a triangle. Input consists of the three points that represent the vertices of the triangle. Points
/*
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 must be read from input file inputhw2.txt. All output must be sent to the output file outputhw2.txt
*/
#include
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 double square(double& a, double& b); double calcLength(double& a, double& b); double semiPerimeter(double& a, double& b, double& c); double calcArea(double& a, double& b, double& c); double round_off(double a, int b); void getPoint(istream inFile, double& a, double& b); void printDistance(ostream outFile, double& a, double& b);
int main() { //Declare input and output file variables ifstream inFile; ofstream outFile;
//Open files inFile.open("inputhw2.txt"); outFile.open("outputhw2.txt");
// get data getPoint(inFile,a,b);
//double a = 6; //double b = 7; //double c = 8;
square(a, b); calcLength(a, b); semiPerimeter(a, b, c); calcArea(a, b, c);
//cout << square << endl; //cout << calcLength << endl; //cout << semiPerimeter << endl; //cout << calcArea << endl; // Send output to the file printDistance(outFile, a, b);
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
// (5) Close files inFile.close(); outFile.close(); system("pause"); return 0; }
//************************ Function definition ************************* // Read the handout carefully for detailed description of the functions that you have to implement // getPoint gets the point for the corrdinates in ("inputhw2.txt") void getPoint(istream inFile, double& a, double& b) { inFile >> a >> b; //returns both of them rounded to a single decimal place to the caller
} // square of a^2 +b^2 double square(double& a, double& b) { return a * a + b * b; } //calcLength: calculates the length between each corrdinates
//Length between points (x1,y1) and (x2,y2) = (x1-x2)2+(y1-y2)2 double calcLength(double& a, double& b) { return sqrt(square(a, b) + square(a, b)); } // 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) double semiPerimeter(double& a, double& b, double& c) { return (1.0/2.0)*(a*b + b * c + c * a); } // calculate the Area of triangle using the 3 point cordinates
//Area = (s*(s-ab)*(s-bc)*(s-ca)) double calcArea(double& a, double& b, double& c) { return sqrt(semiPerimeter(a, b, c)*(semiPerimeter(a, b, c) - a * b)*(semiPerimeter(a, b, c) - b * c)*(semiPerimeter(a, b, c) - c * a)); } // round_off(): 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 double round_off(double value, int digits) { return floor(value * pow(10.0, digits) + 0.5 / (pow(10.0, digits))); }
//printDistance out to text file ("outputhw2.txt")
//the outFile should show example: "The distance between (1.0,1.2) and (6.0,6.0) is 6.93" void printDistance(ostream outFile, double& a, double& b) { outFile << "The distance between (" << a << "," << b << ")and (" << a << ", " << b << ") is " << calcLength; //cout << "The distance between (" << a << "," << b << ") and (" << a << ", " << b << ") is " << calcLength; }
the question is that I'm having problems grabbing data from file and out putting the data to a file.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started