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 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...
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)
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:...
Write a Java method that takes an input string and computes the income minus the expenses....
Write a Java method that takes an input string and computes the income minus the expenses. The income components are indicated by numbers; while the expenses from your spending are numbers starting with a minus sign '-'. The input string may contain lowercase and uppercase letters, as well as other characters. Note that Character.isDigit(char) tests if a char is one of the chars '0', '1', ..., '9'. Also recall that Integer.parseInt(string) converts a string to an int. Test cases :...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Write a program that reads in the radius and length of a cylinder and computes volume...
Write a program that reads in the radius and length of a cylinder and computes volume using the following formulas: area = radius * radius * PI volume = area * length
Write a JavaScript program that computes the average of a collection of numbers and then outputs...
Write a JavaScript program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average. An A grade is any score that is at least 20% greater than the average. The B grade is any score that is not an A, but is at least 10% greater than the average. An F grade is any score that is at least 20% less than the average. The D grade...
Write a defining table and a computer program that computes and outputs the volume of a...
Write a defining table and a computer program that computes and outputs the volume of a torus with inner radius a and outer radius b. A doughnut is an example of a torus. Your program must read the inner radius and outer radius from two text fields and display the volume in a div. The formula for the volume of a torus is v =  π2(a + b)(a - b)2 4 where v is the volume, π is the constant pi,...
Write a program that computes the tax and tip on a restaurant bill for a patron...
Write a program that computes the tax and tip on a restaurant bill for a patron with $44.50 meal charge. The tax should be 6.75% of the meal cost. The tip should be 15% of the total after adding tax. Display the meal cost, tax amount, tip amount, and total bill on the screen. (I need this to be in OOP using C++)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT