Question

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

Homework Answers

Answer #1

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;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
currently the code contacts the server 1 time and exits. Modify the code so that the...
currently the code contacts the server 1 time and exits. Modify the code so that the program contacts the server five times before exiting. /* client.c - code for example client program that uses TCP */ #ifndef unix #define WIN32 #include <windows.h> #include <winsock.h> #else #define closesocket close #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #endif #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #define PROTOPORT 5193 /* default protocol port number */ extern int errno; char...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double secondQuizz; double midTerm; double finalTerm; string name; }; int main() { int n; cout<<"enter the number of students"<<endl; cin>>n; struct student students[n]; int i; struct student istudent; for(i=0;i<n;i++) {    cout<<"Student name?"; cin >> istudent.name; cout<<"enter marks in first quizz , second quizz , mid term , final term of student "<<i+1<<endl; cin>>students[i].firstQuizz>>students[i].secondQuizz>>students[i].midTerm>>students[i].finalTerm; } for(i=0;i<n;i++) { double marks=0; double score=students[i].firstQuizz+students[i].secondQuizz+students[i].midTerm+students[i].finalTerm; marks=(students[i].firstQuizz*0.25)+(students[i].secondQuizz*0.25)+(students[i].midTerm*0.25)+(students[i].finalTerm*0.50); double totalArrgegateMarks =...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
1.    Given the following segment of code: (If there is nothing output, write None.) int x;...
1.    Given the following segment of code: (If there is nothing output, write None.) int x; int y; cin >> x; cin >> y; while (x > y) {     x -= 3;     cout << x << " "; } cout << endl;        a.    What are the output and final values of x and y when the input is 10 for x and 0 for y? [2, 2, 2]               Output                                                                                                                                                                                                    x = ______________                                                                                                                                                                                                   ...
1.Select all C++ functions that must be defined for implementing deep copies in your class which...
1.Select all C++ functions that must be defined for implementing deep copies in your class which has instance variables pointing to dynamic memories. Group of answer choices a.Default Constructor b.Copy Constructor c.Overloading assignment operator d.Writing own friend functions to directly access dynamically allocated memories e.Writing own destructor to free the allocated memory f.Overloading new operator 2. Match the memory source (right side) of the variables (left side) of the following code snippet which is a part of 'myProg' program. Note...
IN C++ - most of this is done it's just missing the bolded part... Write a...
IN C++ - most of this is done it's just missing the bolded part... Write a program that creates a class hierarchy for simple geometry. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation). Create a pure abstract base class Shape, which will form the basis...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's length, width, and area Define an overloaded constructor and use it when creating the Rectangle object instead of using the setters. Change this program to calculate and display the rectangle's perimeter. Example: In feet, how wide is your house? 20 In feet, how long is your house? 25 The house is 20.00 feet wide. The house is 25.00 feet long. The house has 500.00...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT