Question

In: Computer Science

You have to write a program that computes the area of a triangle. Input consists of...

You have to write a program that computes the area of a triangle. Input consists of the three points that represent the vertices of the triangle. Points are represented as Cartesian units i.e. X,Y coordinates as in (3,5). Output must be the three points, the three distances between vertices, and the area of the triangle formed by these points. The program must read the coordinates of each point, compute the distances between each pair of them and print these values. Next, it must calculate the area and print its value. All input are read from keyboard. All output must be printed out on the terminal. Solution requirements: 1) Your program must solve the problem using the following functions: 10 points. getPoint(): Receives the input data. It reads from the keyboard the coordinates (x, y) corresponding to a single vertex of the triangle and returns both of them rounded to a single decimal place to the caller. 15 points. calcLength(): Receives the coordinates of two points and returns the distance between them (calculated using the equation provided below) rounded to ten decimal places. ________________ Length between points (x1,y1) and (x2,y2) = √(x1-x2)2+(y1-y2)2 That is, the length is the square root of the square of the distance between the x coordinates plus the square of the distance between the y coordinates. 15 points. semiPerimeter(): Receives three lengths and returns the value of the semi perimeter (calculated using the equation provided below) rounded to ten decimal places. Semi perimeter = ½ * (ab + bc + ca) Where ab, bc and ca are the lengths between points a and b, b and c, and c and a respectively. 15 points. calcArea(): Receives three lengths and returns the area of the triangle (calculated using the equation provided below) rounded to two decimal places. This function must call semiPerimeter() to calculate the value for s before calculating the area of the triangle. ________________________ Area = √(s*(s-ab)*(s-bc)*(s-ca)) Where s is the semi perimeter and ab, bc, and ca are the three distances between the three points. 10 points. printDistance(): Receives the output file, the coordinates of two points, and the distance between them and prints a message similar to the following one: The distance between (1.0,1.2) and (6.0,6.0) is 6.93 20 points. square(): Receives a value and returns its square rounded to two decimal places. You must use it to calculate the squares of the distances of the x and y coordinates in calcLength(). Do NOT use pow() to implement this function. 15 points. round_off(value, places): Receives a value (double precision real number) and a number indicating a quantity of decimal places (whole number) and returns the value rounded to the specified number of places. Your main() function must: • Call getPoint(), calcLength(), and printDistance() as many times as needed • Call calcArea() • Format the output so real numbers are printed in fixed format • Print the area of the triangle to the terminal. Note: The coordinates of vertices must be printed with a single decimal digit while the distances and the area must be printed with 2 decimal digits. Unless specified otherwise all the values are double precision real numbers. You can declare all the variables that you need. All the functions must be declared below main().

2) You can use ONLY the material learned and used in the lectures/Pas/lab assignments.

3) Your program must pass all my tests.

4) You must choose the most appropriate type of function and type of parameters for each of the functions described above

Must be in C++ please insert into code below

#include <iostream> // to use cin and cout

#include <typeinfo> // to be able to use operator typeid

// Include here the libraries that your program needs to compile

using namespace std;

// Ignore this; it's a little function used for making tests

inline void _test(const char* expression, const char* file, int line)

{

cerr << "test(" << expression << ") failed in file " << file;

cerr << ", line " << line << "." << endl << endl;

}

// This goes along with the above function...don't worry about it

#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))

// Insert here the prototypes of the functions

int main()

{

system("pause");

// Do NOT remove or modify the following statements

cout << endl << "Testing your solution" << endl << endl;

test(fabs(round_off(calcLength(1.0, 1.2, 6.0, 6.1), 2) - 7.00) < .001); // Incorrect calculation of length

test(fabs(round_off(calcLength(6.0, 6.1, 3.2, 6.5), 2) - 2.83) < .001); // Incorrect calculation of length

test(fabs(round_off(calcLength(1.0, 1.2, 3.2, 6.5), 2) - 5.74) < .001); // Incorrect calculation of length

test(fabs(calcArea(calcLength(1.0, 1.2, 6.0, 6.1), calcLength(6.0, 6.1, 3.2, 6.5), calcLength(1.0, 1.2, 3.2, 6.5)) - 7.86) < .001); // Incorrect calculation of area

test(fabs(round_off(calcLength(1.2, 1.2, 7.6, 4.3), 2) - 7.11) < .001); // Incorrect calculation of length

test(fabs(round_off(calcLength(7.6, 4.3, 9.2, 3.4), 2) - 1.84) < .001); // Incorrect calculation of length

test(fabs(round_off(calcLength(1.2, 1.2, 9.2, 3.4), 2) - 8.30) < .001); // Incorrect calculation of length

test(fabs(calcArea(calcLength(1.2, 1.2, 7.6, 4.3), calcLength(7.6, 4.3, 9.2, 3.4), calcLength(1.2, 1.2, 9.2, 3.4)) - 5.36) < .001); // Incorrect calculation of area

test(fabs(round_off(calcLength(1.0, 1.0, 5.0, 5.0), 2) - 5.66) < .001); // Incorrect calculation of length

test(fabs(round_off(calcLength(5.0, 5.0, 9.0, 9.0), 2) - 5.66) < .001); // Incorrect calculation of length

test(fabs(round_off(calcLength(1.0, 1.0, 9.0, 9.0), 2) - 11.31) < .001); // Incorrect calculation of length

test(fabs(calcArea(calcLength(1.0, 1.0, 5.0, 5.0), calcLength(5.0, 5.0, 9.0, 9.0), calcLength(1.0, 1.0, 9.0, 9.0)) - 0.00) < .001); // Incorrect calculation of area

system("pause");

return 0;

}

//************************ Function definition *************************

// Read the handout carefully for detailed description of the functions that you have to implement

Solutions

Expert Solution

I implemented all of the above given functions. I hope you can do it after this....and just arrange the code as said. The functions will work as said, you only need to arrange them. Please reach out to me in the comments if you need any help

#include <iostream> // to use cin and cout

#include <typeinfo> // to be able to use operator typeid

#include<cmath>
#include <fstream>

// Include here the libraries that your program needs to compile

using namespace std;

// Ignore this; it's a little function used for making tests

typedef struct coord{
    double x;
    double y;
} Coord;

float power(float x, int y) 
    { 
        float temp; 
        if( y == 0) 
            return 1; 
        temp = power(x, y/2);  
          
        if (y%2 == 0) 
            return temp*temp; 
        else
        { 
            if(y > 0) 
                return x * temp * temp; 
            else
                return (temp * temp) / x; 
        } 
    }  

double round_off(double var,int r) 
{ 
    double value = (int)(var * power(10,r) + .5); 
    return (double)value / power(10,r); 
} 
Coord getPoint(){
    cout<<"Enter 2 coordinates for the point"<<endl;
    double x,y;
    cin>>x>>y;

    Coord c;
    c.x=round_off(x,1);
    c.y=round_off(y,1);
    return c;
}

double square(double x){
    return round_off(x*x,2);
}

double calcLength(double x1,double y1,double x2,double y2){
    return round_off(sqrt(square(x1-x2)-square(x2-y2)),10);
}

double semiPerimeter(double a,double b,double c){
    return round_off(0.5*(a*b+b*c+c*a),10);
}

double calcArea(double a,double b,double c){
    double s=semiPerimeter(a,b,c);
    return round_off(sqrt(s*(s-a*b)*(s-b*c)*(s-a*c)),2);
}

void printDistance(string filename,double x1,double y1,double x2,double y2){
    double dist=calcLength(x1,y1,x2,y2);
    ofstream myfile;
    myfile.open (filename);
    myfile << "The distance between ("<<x1<<","<<y1<<") and ("<<x2<<","<<y2<<") is "<<dist<<endl;
    myfile.close();
}
/*
inline void _test(const char* expression, const char* file, int line)

{

cerr << "test(" << expression << ") failed in file " << file;

cerr << ", line " << line << "." << endl << endl;

}

// This goes along with the above function...don't worry about it

#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))

// Insert here the prototypes of the functions
*/
int main()

{

system("pause");

Coord n=getPoint();
cout<<n.x<<" "<<n.y<<endl;


// Do NOT remove or modify the following statements
/*
cout << endl << "Testing your solution" << endl << endl;

test(fabs(round_off(calcLength(1.0, 1.2, 6.0, 6.1), 2) - 7.00) < .001); // Incorrect calculation of length

test(fabs(round_off(calcLength(6.0, 6.1, 3.2, 6.5), 2) - 2.83) < .001); // Incorrect calculation of length

test(fabs(round_off(calcLength(1.0, 1.2, 3.2, 6.5), 2) - 5.74) < .001); // Incorrect calculation of length

test(fabs(calcArea(calcLength(1.0, 1.2, 6.0, 6.1), calcLength(6.0, 6.1, 3.2, 6.5), calcLength(1.0, 1.2, 3.2, 6.5)) - 7.86) < .001); // Incorrect calculation of area

test(fabs(round_off(calcLength(1.2, 1.2, 7.6, 4.3), 2) - 7.11) < .001); // Incorrect calculation of length

test(fabs(round_off(calcLength(7.6, 4.3, 9.2, 3.4), 2) - 1.84) < .001); // Incorrect calculation of length

test(fabs(round_off(calcLength(1.2, 1.2, 9.2, 3.4), 2) - 8.30) < .001); // Incorrect calculation of length

test(fabs(calcArea(calcLength(1.2, 1.2, 7.6, 4.3), calcLength(7.6, 4.3, 9.2, 3.4), calcLength(1.2, 1.2, 9.2, 3.4)) - 5.36) < .001); // Incorrect calculation of area

test(fabs(round_off(calcLength(1.0, 1.0, 5.0, 5.0), 2) - 5.66) < .001); // Incorrect calculation of length

test(fabs(round_off(calcLength(5.0, 5.0, 9.0, 9.0), 2) - 5.66) < .001); // Incorrect calculation of length

test(fabs(round_off(calcLength(1.0, 1.0, 9.0, 9.0), 2) - 11.31) < .001); // Incorrect calculation of length

test(fabs(calcArea(calcLength(1.0, 1.0, 5.0, 5.0), calcLength(5.0, 5.0, 9.0, 9.0), calcLength(1.0, 1.0, 9.0, 9.0)) - 0.00) < .001); // Incorrect calculation of area

system("pause");
*/

return 0;

}

//************************ Function definition *************************

// Read the handout carefully for detailed description of the functions that you have to implement

Related Solutions

Write a program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
write a java program for area and perimeter of right triangle
write a java program for area and perimeter of right triangle
Write a program that computes the product of two fractions. The user provides 4 input which...
Write a program that computes the product of two fractions. The user provides 4 input which represent the numerator and the denominator of two fractions and prints the result of the operation. The program uses two functions. One called MultiplyNumerator(…) which calculates the product of the numerators of the fractions, and a second one called MultiplyDenom(…) which calculates the product of the denominator of the fractions. Call your program MultiplyFrac.cpp
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The program to present the user with a menu where one of the shapes can be selected. Based on the selection made, the user enters the proper input, the program validates the input (i.e all entries must be greater than zero). Once the input is entered and validated, the intended area is calculated and the entered information along with the area are displayed. Area of...
JAVA Program: Computes The Molar Mass of a chemical Formula. You shall write a program that:...
JAVA Program: Computes The Molar Mass of a chemical Formula. You shall write a program that: -Loads chemical-element data by scanning through the file (accessed via file path or URL) exactly once. FILENAME: Elements.txt -Expects one or more command-line arguments that constitute a chemical formula, as described below. *A chemical formula, for the sake of this program, shall be defined as: One or more whitespace-delimited tokens Each token consists of either: An element symbol, implying one atom of that element...
write a program that computes and displays the number of days that have elapsed between January...
write a program that computes and displays the number of days that have elapsed between January 1, 1900 and today (the day the program is run). The program must be designed as follows: Within a class named Elapsed_Days, provide the following functions: • bool is_year_valid(long year) returns true if year is >= 1900; otherwise false • bool is_month_valid(long month) returns true if month is between 1 and 12 (inclusive); otherwise false • bool is_leap_year(long year) returns true if year is...
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
C++ language Write a program that accepts as input the base price and the finished area...
C++ language Write a program that accepts as input the base price and the finished area in square feet of the three models. The program outputs the model(s) with the least price per square foot in the following format: If the colonial model is the least price, output: The price per square foot of the colonial model is the least. If the split-entry model is the least price, output: The price per square foot of the split-entry model is the...
Create a program that asks the user to input three numbers and computes their sum. This...
Create a program that asks the user to input three numbers and computes their sum. This sounds simple, but there's a constraint. You should only use two variables and use combined statements. You can use the output below as a guide. Please enter the first number: 4 Please enter the second number: 2 Please enter the third number: 9 The sum of the three numbers is: 15.
You have been asked to write program that allows the user to input a first name,...
You have been asked to write program that allows the user to input a first name, middle initial (without the period), and last name of a user and then display that person’s name with the first, middle initial followed by a period, and last name last. BEFORE creating the program, use structured programming principles to document how you are going to develop the program. Use Microsoft Word to complete this part of the assessment. Answer each of the following areas:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT