the lengths of edges are grouped and stored in a file named

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.

Solution


/*
C ++ program that reads a text file called edges.txt which contains
sides of the triangles . The program finds area of the triangle which
satisfies the triangle sides property otherwise prints a star in the
area.txt file
*/
#include<iostream>
#include <iomanip>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

//function prototypes
bool isValid (int tri1, int tri2, int tri3);
double area(int a, int b, int c);
void calc (int a, int b, int c, float &area);

int main ()
{
      
   float refArea;

   ifstream fin;
   fin.open(\"edges.txt\");


   if(!fin)
   {
       cout<<\"File doesnot exist ...\"<<endl;
       system(\"pause\");
       return -1;
   }

   ofstream fout;
   fout.open(\"area.txt\");

   int a, b, c;
   //read until end of file
   while(fin >> a >> b >> c)
   {
       /*Calling isValid function */

       bool result = isValid (a, b, c);

       if(result)
       {
           calc (a, b, c, refArea);
           fout <<left<<setw(5)<<a<<setw(5)<<b<<setw(5)<<setw(5)<<c<<fixed << setprecision (2)<< refArea << endl;
       }
       else
           fout <<left<<setw(5)<<a<<setw(5)<<b<<setw(5)<<setw(5)<<c<<setw(5)<<\"*\"<<endl;

   } // end loop.


   fin.close();
   fout.close();

   system(\"pause\");
   return 0;

}//end main
// isValid Function
bool isValid (int tri1, int tri2, int tri3)
{
   bool result;
   if ((tri1 + tri2 > tri3) && (tri1 + tri3 > tri2) && (tri2 + tri3 > tri1))
       result = true;
   else
       result = false;

   return result;

} // end isValid


/* The function calc that takes three sides and finds the area of the triangle
using herons\' forumula*/
void calc(int a, int b, int c, float &area)
{
   float s = ((a + b + c)/2.0);
   area = sqrt(s*(s-a)*(s-b)*(s-c));
}

------------------------------------------------------------------------------------------------

Input text file

edges.txt

12 15 7
8 9 14
11 2 15
6 5 4
18 7 10
16 11 17

------------------------------------------------------------------------------------------------

Output text file

area.txt

12   15   7    41.23
8    9    14   33.67
11   2    15   *  
6    5    4    9.92
18   7    10   *  
16   11   17   85.21

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 t
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 t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site