Hello,
I'm in a beginners c++ class and I need to write a program that calculates the area and perimeter of a triangle of sides: a, b, and c.
The lengths of each side (values for a, b, c) are written in an input file (.txt), so the program has to calculate area and perimeter only for those values.
The lengths of each side contained in the input file are:
5 5 10
5 4 10
5 6 10
Each line is a different case. For instance, case 1 is: a=5, b=5, c=10, case 2 is: a=5, b=4, and so on.
My code so far is:
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void calculations(ifstream& infile, ofstream& outfile, double& first, double& second);
int main()
{
ifstream in_stream;
ofstream out_stream;
in_stream.open("infile.txt");
if (in_stream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
out_stream.open("outfile.dat");
if (out_stream.fail())
{
cout << "Output file opening failed.\n";
exit(1);
}
double semiperimeter, area;
calculations(in_stream, out_stream, semiperimeter, area);
in_stream.close();
out_stream.close();
cout << "End of program.\n";
return 0;
}
void calculations(ifstream& infile, ofstream& outfile, double& first, double& second)
{
const int number_of_cases = 3, variables = 3;
int i, A[number_of_cases][variables];
double next, area, perimeter, semiperimeter;
cout << "This program calculates the area and perimeter of a triangle of sides\n"
<< "a, b, and c, for the baseline cases given in class.\n"
<< endl;
I'm not sure if what I have so far is right, but I'm stuck in the void function because I don't know how to read the values for each case (each line) as a 2 dimensional array, and plug them into the variables a, b, c, in order to perform the following calculations:
semiperimeter = (a+b+c)/2
area = sqrt(s*(s-a)*(s-b)*(s-c))
perimeter = 2 * semiperimeter
I appreciate any help.