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
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...
DIRECTIONS: IMPLEMENT QuickSort and MergeSort. You have been provided: 1. Functions to perform Merge (for MergeSort)...
DIRECTIONS: IMPLEMENT QuickSort and MergeSort. You have been provided: 1. Functions to perform Merge (for MergeSort) and Partition (for QuickSort). 2. In class we discussed the body of the recursive functions, the "brakes" needed to stop the recursion. You are expected to develop 2 RECURSIVE programs: 1. Complete the bodies of the sort functions, MergeSort and QuickSort. 2. Complate the main that tests each function. - program should not read inputs - stock the program with multiple arrays (test cases)...
Write a C/C++ program that performs the tasks described below. If there is just 1 command-line...
Write a C/C++ program that performs the tasks described below. If there is just 1 command-line argument and it is -hw you should simply print hello world and then exit(0). Otherwise, the program should provide a function named mm_alloc. This is the function prototype: void *mm_alloc(int num_bytes_to_allocate) mm_alloc function should obtain space for the user from a block which you obtain via mmap. You should obtain the block on the first invocation and the block size should be some number...
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 =...
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...
Description: In this assignment, you need to implement a recursive descent parser in C++ for the...
Description: In this assignment, you need to implement a recursive descent parser in C++ for the following CFG: 1. exps --> exp | exp NEWLINE exps 2. exp --> term {addop term} 3. addop --> + | - 4. term --> factor {mulop factor} 5. mulop --> * | / 6. factor --> ( exp ) | INT The 1st production defines exps as an individual expression, or a sequence expressions separated by NEWLINE token. The 2nd production describes an...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing associated with the assignment it is failing one part of testing.   Below is the test that fails: Failed test 4: differences in output arguments: -c input data: a b c -c expected stdout: b observed stdout: a b expected stderr: observed stderr: ./test: invalid option -- 'c' Unsure where I have gone wrong. MUST BE WRITTEN IN C++ Task Level 1: Basic operation Complete...
// This program prints a table to convert numbers from one unit to another. // The...
// This program prints a table to convert numbers from one unit to another. // The program illustrates some implementation techniques. //Include the header file ostream for including all stream. ---------------------------------------------------------*/ #include <iostream> //Provides cout <v1.0> //Include iomanip that is used for set precision. #include <iomanip> //Provides setw function for setting output width <v1.1> //Header file for EXIT_SUCCESS. #include <stdlib.h>// Provides EXIT_SUCCESS <v1.2> //Header file for assert. #include <assert.h>// Provides assert function <1.3> using namespace std; // Allows all standard...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
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...