How do i make my code display only one set of numbers out of a list of 70 that d
ID: 3572122 • Letter: H
Question
How do i make my code display only one set of numbers out of a list of 70 that display the triangle whose area is .5 with the maximum perimeter?
static int triangles() {
ifstream ifs;
string path = "C:\Users\ken\Desktop\triangle.txt";
int x1, x2, y1, y2, z1, z2;
int count = 1;
float A, B, C, S;
double perimeter, area;
ifs.open(path);
if (!ifs)
{
cout << "file not found:" << path << endl;
return -1;
}
ifs >> x1;
while (!ifs.eof())
{
ifs >> x2 >> y1 >> y2 >> z1 >> z2;
float A = sqrt((double)(y1 - x1) * (y1 - x1) + (y2 - x2) * (y2 - x2));
float B = sqrt((double)(y1 - z1) * (y1 - z1) + (y2 - z2) * (y2 - z2));
float C = sqrt((double)(x1 - z1) * (x1 - z1) + (x2 - z2) * (x2 - z2));
float s = (A + B + C) / 2;
perimeter = A + B + C;
area = sqrt(s * (s - A) * (s - B) * (s - C));
cout << "Triangle " << count << " with points(" << x1 << "," << x2 << "), (" << y1 << "," << y2 << "), (" << z1 << "," << z2 << ") has an area of " << area << ", and a maximum perimeter of" << perimeter << endl;
++count;
ifs >> x1;
}
ifs.close();
return 0;
}
Explanation / Answer
static int triangles() {
ifstream ifs;
string path = "C:\Users\ken\Desktop\triangle.txt";
int x1, x2, y1, y2, z1, z2,mx1, mx2, my1, my2, mz1, mz2;
int count = 1,mcount;
float A, B, C, S;
double perimeter, area,marea,mperimeter=0;
ifs.open(path);
if (!ifs)
{
cout << "file not found:" << path << endl;
return -1;
}
ifs >> x1;
while (!ifs.eof())
{
ifs >> x2 >> y1 >> y2 >> z1 >> z2;
float A = sqrt((double)(y1 - x1) * (y1 - x1) + (y2 - x2) * (y2 - x2));
float B = sqrt((double)(y1 - z1) * (y1 - z1) + (y2 - z2) * (y2 - z2));
float C = sqrt((double)(x1 - z1) * (x1 - z1) + (x2 - z2) * (x2 - z2));
float s = (A + B + C) / 2;
perimeter = A + B + C;
area = sqrt(s * (s - A) * (s - B) * (s - C));
if(area==.5)//if area == 5
{
if(perimeter>mperimeter)//if max perimerter
{
mx1 = x1;mx2 = x2;my1 = y1;my2 = y2; mz1 = z1; mz2 = z2;
marea = area;
mperimeter = perimeter;
mcount=count;
}
}
// cout << "Triangle " << count << " with points(" << x1 << "," << x2 << "), (" << y1 << "," << y2 << "), (" << z1 << "," << z2 << ") has an area of " << area << ", and a maximum perimeter of" << perimeter << endl;
++count;
ifs >> x1;
}
ifs.close();
//printing once
cout << "Triangle " << count << " with points(" << mx1 << "," << mx2 << "), (" << my1 << "," << my2 << "), (" << mz1 << "," << mz2 << ") has an area of " << marea << ", and a maximum perimeter of" << mperimeter << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.