Question

In: Computer Science

C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...

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;
}

Solutions

Expert Solution

The below code has been modified to convert the functions return type from void to string. comment as //added has been included in the modified parts of the 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
#include <string>

using namespace std;

//triangle function
string triangle(int a, int b, int c)
{
    string res;
    
  if (a + b <= c || a + c <= b || b + c <= a) {//conditions for an invalid triangle
  
    res = to_string(a) + " " +to_string(b)+  " "+to_string(c) + " "+ "Not a triangle";  //added
    
    return res;
  }
  else //if above returned false, then it is a valid triangle
  {
    int temp;
    res = to_string(a) + " " +to_string(b)+  " "+to_string(c) + " "; //added

    //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
    {
      res = res +  "Equilateral triangle"; //added
      return res;
    }

    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
      res = res +  "Right "; //added

    if (a == b || b == c || a == c) //condition for Isosceles triangle
      res = res +  "Isosceles triangle";//added 
      
    else //if both the above ifs failed, then it is a scalene triangle
      res = res +  "Scalene triangle"; //added
  }
  return res;
}

//overloaded triangle function
string triangle(double a, double b, double c)
{
  //equality threshold value for absolute difference procedure
  const double EPSILON = 0.001;
  string res;
  if (a + b <= c || a + c <= b || b + c <= a) {//conditions for an invalid triangle
    res =  to_string(a) + " " +to_string(b)+  " "+to_string(c) + " "+ "Not a triangle";  //added
    return res;
  }
  else //if above returned false, then it is a valid triangle
  {
    double temp;

    
    res =  to_string(a) + " " +to_string(b)+  " "+to_string(c) + " ";  //added

    //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
    {
      res = res + "Equilateral triangle"; //added
      //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 ";
     res = res + "Right ";

    if (fabs(a - b) < EPSILON ||
        fabs(b - c) < EPSILON ||
        fabs(a - c) < EPSILON) //condition for Isosceles triangle
        
      res = res + "Isosceles triangle";  //added
    else //if both the above ifs failed, then it is a scalene triangle 
    
      res = res + "Scalene triangle";  //added
  }
  return res;
}

//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
    string res;   //added
    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 if inputs are valid
    res = triangle(a, b, c);
    cout<< res ;  //added
  }

  else //if command arguments were found and valid
  {
    if (strcmp(argv[1], "-i") == 0)
    {
      int a, b, c, temp; //declare required variables
      string res;

      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
      res = triangle(a, b, c);
      cout<< res ;  //added
    }

    else if (strcmp(argv[1], "-d") == 0)
    {
      double a, b, c, temp; //declare required variables
      string res;

      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
      res = triangle(a, b, c);
      cout << fixed << setprecision(5) << res;  //added
      
    }
  }

  cout << endl;

  return 0;
}

Output screenshot:


Related Solutions

C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
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...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to call these functions instead of the inline code in your main(). In addition, add error checking for the user input. If the user inputs an incorrect value prompt the user to re-enter the number. The flow of the code should look something like: /** Calculate the mean of a vector of floating point numbers **/ float mean(vector& values) /** Calculate the standard deviation from...
C++ CODE Change your code from Part A to now present a menu to the user...
C++ CODE Change your code from Part A to now present a menu to the user asking them for an operation to perform on the text that was input. You must include the following functions in your code exactly as provided. Additionally, you must have function prototypes and place them above your main function, and the function definitions should be placed below the main function. Your program should gracefully exit if the user inputs the character q or sends the...
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A...
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A function to get principal, rate, and term. A function to calculate monthly payment. Test your program by getting the data from the user by using the first function and calculate monthly payment by calling the other function. Add version control and write proper comments with a few blank lines & indentations in order to improve your programming style.
// If you modify any of the given code, the return types, or the parameters, you...
// If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // You are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of employees struct employeeList {    struct employee* employee;    struct employeeList* next; } *list = NULL;              ...
C code required /* * isGreater - if x > y then return 1, else return...
C code required /* * isGreater - if x > y then return 1, else return 0 * Example: isGreater(4,5) = 0, isGreater(5,4) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 24 * Rating: 3 */
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT