The area of an arbitrary triangle can be computed using the formula:
area = sqrt s(s-a)(s-b)s(-c)
where a, b, and c are the lengths of the sides, and s is the semiperimeter.
s= a+b+c/2
However, not all three integers a, b, and c will produce a triangle. This is because for any triangle the sum of the lengths of any two edges shall be greater than the length of the remaining edge.
In this assignment, the lengths of edges are grouped and stored in a file named “edges.txt”. Each line of the file contains a group of three edges, which may or may not produce a triangle as discussed above. Your program reads each group of three edges from the file and determines whether or not they produce a valid triangle. If the three edges produce a valid triangle, compute its area and store the area the file “area.txt” with the precision of 2. Otherwise, write the letter “*” in the corresponding line. Each line of the file “area.txt” contains an area or a “*” symbol.
For example, if the file “edges.txt” contains the following data, your program is supposed to produce the file “areas.txt” as follows.
edges.txt areas.txt
For example, in line 1 the three numbers 12, 15, and 7 produces a valid triangle, the area of which is 41.23. But in line 4 the three numbers 11, 2, and 15 cannot produce a valid triangle.
In your program, you shall apply the procedure abstract methodology, i.e., breaking large problems into smaller sub-tasks. For example, you can define the following three functions in your program:
void ProcessEdges(ifstream& in_stream, ofstream& out_stream);
// Precondition: groups of three edges are stored in in_stream
// Postcondition: for each group of three edges in in_stream, compute the area
and store it in a separate line in the out_stream if they are valid edges; otherwise, store a “*” in out_stream.
bool isValidTriangle(int a, int b, int c);
// Precondition: integers a, b, and c store lengths of three edges
// Postcondition: If a, b, and c produce a valid triangle, return true; otherwise, return false.
double area(int a, int b, int c);
// Precondition: integers a, b, and c are three edges of a valid triangle; // Postcondition: return the area of the triangle.
//Define prototype for isValid Function
void ProcessEdges();
bool isValid (int tri1, int tri2, int tri3);
bool side;
double area(int a, int b, int c);
//Define prototype for calc Function
void calc (int a, int b, int c, float &s, float &area);
int main ()
{
int side_a, side_b, side_c;
float refS;
float refArea;
while (side != 1) //Establish loop here. If side not equal to 1, error encountered, try again.
{
cout << "Enter 3 lengths of the triangle sides: \n";
cout << " Side 1: "; cin >> side_a;
cout << " Side 2: "; cin >> side_b;
cout << " Side 3: "; cin >> side_c;
cout << endl;
cout << endl;
/*Call isValid Function to determine if the user inputs are valid.
isValid Function needs to loop until the correct values are entered */
bool side = isValid (side_a, side_b, side_c);
{
if (side == 0)
{
cout << "*" << endl;
side = 0; // Populate side with 0. Error encountered. Give it another try.
}
else //
{
side = 1; // If sides = triangle, end loop and continue.
}
}
} // end loop.
/*Call calc Function - this function will return the value of semiperimeter and Area.
(these values were stored in the reference parameters S and Area. Output should be perimeter
only (side a, b, c) and area. Set precision to 4 */
calc (side_a, side_b, side_c, refS, refArea);
cout << "Your triangle has a area of " << fixed << setprecision (2)<< refArea << endl;
bool isValid (int tri1, int tri2, int tri3)
{
if ((tri1 + tri2 > tri3) && (tri1 + tri3 > tri2) && (tri2 + tri3 > tri1))
side = true;
else
side = false;
return side;
} // end isValid
/* Write a Function 'calc'. Return type = void, 5 parameters (a, b, c, &s, &area.
Calculate s = semiperimeter and area. Results of s and area are to be stored in the
reference parameters s and area */
void calc(int a, int b, int c, float &s, float &area)
{
s = ((a + b + c)/2.0);
area = sqrt(s*(s-a)*(s-b)*(s-c));
}
I don't know what I am doing wrong. If I take out the:
void ProcessEdges(ifstream& in_stream, ofstream& out_stream)
{ in_stream.open("edges.txt");
out_stream.open("area.txt");
int a, b, c;
cin >> a >> b >> c;
in_stream.close();
out_stream.close();
}
then I get what I need but according (or so I assume) my instructions I need this function. Any help would be appreciated.
The way I understand the assignment is we have to input 3 different sides....the examples above are from the professor. i.e. 12, 15, 7 will produce 41.23 (if correct), or if we input 5,4,3 then the output should be *.
this code here will run the program correctly, but I think I am missing the part with the edges.txt.
this is the part where I am stumped.
""In this assignment, the lengths of edges are grouped and stored in a file named “edges.txt”. Each line of the file contains a group of three edges, which may or may not produce a triangle as discussed above. Your program reads each group of three edges from the file and determines whether or not they produce a valid triangle. If the three edges produce a valid triangle, compute its area and store the area the file “area.txt” with the precision of 2. Otherwise, write the letter “*” in the corresponding line. Each line of the file “area.txt” contains an area or a “*” symbol.""
For example, if the file “edges.txt” contains the following data, your program is supposed to produce the file “areas.txt” as follows.
edges.txt areas.txt
Admittedly, I think I suck at this programming, so forgive me for not being like you all. I am just trying to get through this. I truly wanted to learn to program but I am beginning to rethink my intelligence :( ugh....
Allow me to reiterate: your posts will be much easier to read, and more likely to attract people who want to help you, if you use the code tags to format the code portions of your posts.