C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that c is at least as large as the other two sides. 4. Please run your program with all of your test cases from HW2. The results should be identical to that from HW2. You must supply a listing of your program and sample output CODE right now #include <cstdlib> //for exit, atoi, atof #include <iostream> //for cout and cerr #include <iomanip> //for i/o manipulation #include <cstring> //for strcmp #include <cmath> //for fabs using namespace std; //triangle function void triangle(int a, int b, int c) { if (a + b <= c || a + c <= b || b + c <= a) //conditions for an invalid triangle cout << a << " " << b << " " << c << " " << "Not a triangle"; else //if above returned false, then it is a valid triangle { int temp; cout << a << " " << b << " " << c << " "; //print the side values //logic to find out the largest value and store it to c. if (a > b && a > c) { temp = a; a = c; c = temp; } else if (b > a && b > c) { temp = b; b = c; c = temp; } if (a == b && b == c) //condition for equilateral triangle { cout << "Equilateral triangle"; return; } else if (a * a == b * b + c * c || b * b == c * c + a * a || c * c == a * a + b * b) //condition for right triangle i.e. pythagoras theorem cout << "Right "; if (a == b || b == c || a == c) //condition for Isosceles triangle cout << "Isosceles triangle"; else //if both the above ifs failed, then it is a scalene triangle cout << "Scalene triangle"; } } //overloaded triangle function void triangle(double a, double b, double c) { //equality threshold value for absolute difference procedure const double EPSILON = 0.001; if (a + b <= c || a + c <= b || b + c <= a) //conditions for an invalid triangle cout << fixed << setprecision(5) << a << " " << b << " " << c << " " << "Not a triangle"; else //if above returned false, then it is a valid triangle { double temp; cout << fixed << setprecision(5) << a << " " << b << " " << c << " "; //print the side values //logic to find out the largest value and store it to c. if (a > b && a > c) { temp = a; a = c; c = temp; } else if (b > a && b > c) { temp = b; b = c; c = temp; } if (fabs(a - b) < EPSILON && fabs(b - c) < EPSILON) //condition for equilateral triangle { cout << "Equilateral triangle"; return; } else if (fabs((a * a) - (b * b + c * c)) < EPSILON || fabs((b * b) - (c * c + a * a)) < EPSILON || fabs((c * c) - (a * a + b * b)) < EPSILON) //condition for right triangle i.e. pythagoras theorem cout << "Right "; if (fabs(a - b) < EPSILON || fabs(b - c) < EPSILON || fabs(a - c) < EPSILON) //condition for Isosceles triangle cout << "Isosceles triangle"; else //if both the above ifs failed, then it is a scalene triangle cout << "Scalene triangle"; } } //main function int main(int argc, char **argv) { if (argc == 3 || argc > 5) //check argc for argument count { cerr << "Incorrect number of arguments. " << endl; exit(1); } else if (argc == 1) //if no command arguments found then do the following { int a, b, c; //declare three int cin >> a >> b >> c; //read the values if (a <= 0 || b <= 0 || c <= 0) //if values are negative then exit { cerr << "Data must be a positive integer. " << endl; exit(1); } triangle(a, b, c); //call the function if inputs are valid } else //if command arguments were found and valid { if (strcmp(argv[1], "-i") == 0) { int a, b, c, temp; //declare required variables if (argc == 5) { //convert char to int using atoi() a = atoi(argv[2]); b = atoi(argv[3]); c = atoi(argv[4]); } else { cin >> a >> b >> c; // read the values } if (a <= 0 || b <= 0 || c <= 0) //if values are negative then exit { cerr << "Data must be a positive integer. " << endl; exit(1); } //call the function triangle(a, b, c); } else if (strcmp(argv[1], "-d") == 0) { double a, b, c, temp; //declare required variables if (argc == 5) { //convert char to int using atoi() a = atof(argv[2]); b = atof(argv[3]); c = atof(argv[4]); } else { cin >> a >> b >> c; // read the values } if (a <= 0 || b <= 0 || c <= 0) //if values are negative then exit { cerr << "Data must be a positive double. " << endl; exit(1); } //call the overloaded function triangle(a, b, c); } } cout << endl; return 0; }
I have modified your code so that the triangle function only
returns the type of the triangle and print the message in main
method.
PLEASE FIND THE FOLLOWING CODE SCREENSHOT, OUTPUT, AND CODE.
ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT
1.CODE SCREENSHOT:
2.OUTPUT:
3.CODE:
#include <cstdlib> //for exit, atoi, atof
#include <iostream> //for cout and cerr
#include <iomanip> //for i/o manipulation
#include <cstring> //for strcmp
#include <cmath> //for fabs
using namespace std;
//triangle function
string triangle(int a, int b, int c)
{
string s="";
if (a + b <= c || a + c <= b || b + c <= a) //conditions for an invalid triangle
return "Not a triangle";
else //if above returned false, then it is a valid triangle
{
int temp;
//logic to find out the largest value and store it to c.
if (a > b && a > c)
{
temp = a;
a = c;
c = temp;
}
else if (b > a && b > c)
{
temp = b;
b = c;
c = temp;
}
if (a == b && b == c) //condition for equilateral triangle
{
return "Equilateral triangle";
}
if (c * c == a * a + b * b) //condition for right triangle i.e. pythagoras theorem
s+="Right ";
if (a == b || b == c || a == c) //condition for Isosceles triangle
return s+"Isosceles triangle";
else //if both the above ifs failed, then it is a scalene triangle
return s+"Scalene triangle";
}
}
//overloaded triangle function
string triangle(double a, double b, double c)
{
//equality threshold value for absolute difference procedure
const double EPSILON = 0.001;
string s="";
if (a + b <= c || a + c <= b || b + c <= a) //conditions for an invalid triangle
return "Not a triangle";
else //if above returned false, then it is a valid triangle
{
double temp;
//logic to find out the largest value and store it to c.
if (a > b && a > c)
{
temp = a;
a = c;
c = temp;
}
else if (b > a && b > c)
{
temp = b;
b = c;
c = temp;
}
if (fabs(a - b) < EPSILON &&
fabs(b - c) < EPSILON) //condition for equilateral triangle
{
return "Equilateral triangle";
}
else if (fabs((a * a) - (b * b + c * c)) < EPSILON ||
fabs((b * b) - (c * c + a * a)) < EPSILON ||
fabs((c * c) - (a * a + b * b)) < EPSILON) //condition for right triangle i.e. pythagoras theorem
s+="Right ";
if (fabs(a - b) < EPSILON ||
fabs(b - c) < EPSILON ||
fabs(a - c) < EPSILON) //condition for Isosceles triangle
return s+"Isosceles triangle";
else //if both the above ifs failed, then it is a scalene triangle
return s+"Scalene triangle";
}
}
//main function
int main(int argc, char **argv)
{
if (argc == 3 || argc > 5) //check argc for argument count
{
cerr << "Incorrect number of arguments. " << endl;
exit(1);
}
else if (argc == 1) //if no command arguments found then do the following
{
int a, b, c; //declare three int
cin >> a >> b >> c; //read the values
if (a <= 0 || b <= 0 || c <= 0) //if values are negative then exit
{
cerr << "Data must be a positive integer. " << endl;
exit(1);
}
cout << a << " " << b << " " << c << " "; //print the side values
cout<<triangle(a, b, c); //call the function if inputs are valid
}
else //if command arguments were found and valid
{
if (strcmp(argv[1], "-i") == 0)
{
int a, b, c, temp; //declare required variables
if (argc == 5)
{
//convert char to int using atoi()
a = atoi(argv[2]);
b = atoi(argv[3]);
c = atoi(argv[4]);
}
else
{
cin >> a >> b >> c; // read the values
}
if (a <= 0 || b <= 0 || c <= 0) //if values are negative then exit
{
cerr << "Data must be a positive integer. " << endl;
exit(1);
}
cout << a << " " << b << " " << c << " "; //print the side values
//call the function
cout<<triangle(a, b, c);
}
else if (strcmp(argv[1], "-d") == 0)
{
double a, b, c, temp; //declare required variables
if (argc == 5)
{
//convert char to int using atoi()
a = atof(argv[2]);
b = atof(argv[3]);
c = atof(argv[4]);
}
else
{
cin >> a >> b >> c; // read the values
}
if (a <= 0 || b <= 0 || c <= 0) //if values are negative then exit
{
cerr << "Data must be a positive double. " << endl;
exit(1);
}
cout << a << " " << b << " " << c << " "; //print the side values
//call the overloaded function
cout<<triangle(a, b, c);
}
}
cout << endl;
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.