Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads from a data file named triangles.dat in the same dire

ID: 3715888 • Letter: W

Question

Write a program that reads from a data file named triangles.dat in the same directory as your program’s current working directory (i.e. no need to specify path to the file) that contains unknown pairs of base and height values of triangles. For each pair read from the input file, display the base and height values and the computed area. Use floating-point data type to hold the numbers. You may use the following values in your data file or you can create your own.

(b,h) of this data (15.0 4.0), (6.0 9.0)( 3.0 4.0)( 5.23 8.45)( 52.36 84.78 )

The area of triangle is calculated with this formula: Note: I will use a different input data file to test your program and the file will contain different number of triangles, so do not assume there will always be 5 triangles in the file. This is for C++

Explanation / Answer

I have created a simple and object oriented solution for your problem, everything is explained using comments. So go through it, understand how the code works and let me know if you have any doubts. Thanks.

//code.cpp

#include<iostream>

#include<fstream>

#include<vector>

//defining a simple structure to hold data of a single triangle

struct Triangle{

                float base;

                float height;

                float area;

               

};

using namespace std;

int main(){

                //name of the input file

                char inputFileName[]="triangles.dat";

                //opening the file

                ifstream inFile(inputFileName);

                if(!inFile){

                                //couldnt open the file

                                cout<<"Input file not found!"<<endl;

                                return 1;

                }

                //creating a vector of triangles, so that we dont need to worry about the

                //number of pairs in the file

                vector<Triangle> triangles;

                char ch;

                float num1,num2;

                //looping through the file, assuming that the file is valid and each and every

                //pair is enclosed within ()

                while(ch!=EOF){

                                //checking the next character without removing it

                                if(inFile.peek()=='('){

                                                //it is an open paranthesis, so start of a pair, getting the character

                                                //from the file into the ch variable

                                                ch=inFile.get();

                                                //getting the two floating point numbers

                                                inFile>>num1;

                                                inFile>>num2;

                                                //defining a triangle and setting base and height

                                                Triangle t;

                                                t.base=num1;

                                                t.height=num2;

                                                //finding the area and setting the area field

                                                t.area=t.base*t.height*0.5;

                                                //pushing to the vector

                                                triangles.push_back(t);

                                }

                                //getting the next character

                                ch=inFile.get();

                }

                //looping through the vector and printing the details of each triangles

                for(int i=0;i<triangles.size();i++){

                                cout<<"Triangle "<<(i+1)<<endl;

                                cout<<"Base: "<<triangles[i].base<<endl;

                                cout<<"Height: "<<triangles[i].height<<endl;

                                cout<<"Area: "<<triangles[i].area<<endl;

                                cout<<endl;

                }

               

                return 0;

}

/*OUTPUT*/

Triangle 1

Base: 15

Height: 4

Area: 30

Triangle 2

Base: 6

Height: 9

Area: 27

Triangle 3

Base: 3

Height: 4

Area: 6

Triangle 4

Base: 5.23

Height: 8.45

Area: 22.0968

Triangle 5

Base: 52.36

Height: 84.78

Area: 2219.54

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote