Question

In: Computer Science

In C++, 
 Define a structure Triangle that contains three Point members. Write a function that computes...

In C++, 
 Define a structure Triangle that contains three Point members. Write a function that computes the perimeter() of a Triangle. Write a program that reads the coordinates of the points, calls your function, and displays the result.

C++

Solutions

Expert Solution

#include<iostream>
#include<cmath>
using namespace std;
struct Point
{
   int x,y;
};
struct Triangle
{
   Point p1,p2,p3;//3 point members   
};

double perimeter(Triangle t)
{
   double side1 = sqrt(pow(t.p2.x-t.p1.x,2)+pow(t.p2.y-t.p1.y,2));
   double side2 = sqrt(pow(t.p3.x-t.p2.x,2)+pow(t.p3.y-t.p2.y,2));
   double side3 = sqrt(pow(t.p1.x-t.p3.x,2)+pow(t.p1.y-t.p3.y,2));
   //now returning perimeter
   return side1+side2+side3;
}
int main()
{
   Triangle t;
   //reading 3 points
   cout<<"Enter point1 x:";
   cin>>t.p1.x;
   cout<<"Enter point1 y:";
   cin>>t.p1.y;
  
   cout<<"Enter point2 x:";
   cin>>t.p2.x;
   cout<<"Enter point2 y:";
   cin>>t.p2.y;

   cout<<"Enter point3 x:";
   cin>>t.p3.x;
   cout<<"Enter point3 y:";
   cin>>t.p3.y;
  
   //calling method to find perimeter
   double p = perimeter(t);
   cout<<"Perimeter is: "<<p<<endl;
   return 0;
}


Related Solutions

Write C code that contains a function called triangle_generator that outputs a triangle wave at a...
Write C code that contains a function called triangle_generator that outputs a triangle wave at a specified frequency (in hertz) using a specified sample rate (in hertz), and for a specified time duration (in seconds). These parameters are float type. The output you generate are floating point numbers between -1 and 1, one number per output line. The math trig functions in C use radians while all the specifications here are in hertz (cycles per second). One hertz is 2*Pi...
Define a structure Point. A point has an x- and a y-coordinate. Write a function double...
Define a structure Point. A point has an x- and a y-coordinate. Write a function double distance(Point a, Point b) that computes the distance from a to b. Write a program that reads the coordinates of the points, calls your function, and displays the result. IN C++ if possible please give //comments
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X,...
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X, if X is greater than 1 and X is less than 100. Put the inline function in a main program that reads X from the keyboard, calls the function, and then outputs the result. 2. Show an empty statement and detail what it does? 3. A collection of predefined functions is called a Database                    C) Subroutine                       E) None of these Library                       D) Directive 4....
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....
Write a module that contains a function sqrt(y, tol=1e-6), which computes a square root of a...
Write a module that contains a function sqrt(y, tol=1e-6), which computes a square root of a number using Heron’s algorithm with guaranteed relative error less then tol. The module should run as a program that asks for user input and prints output when executed using run sqrt.py. Heron’s algorithms for finding x such that y = x^2 works as follows. First, you come up with an initial guess for x; think what it should be. Then, you update x using...
[ Write in C not C++] 1. Define a C structure Covid19Info to store the information...
[ Write in C not C++] 1. Define a C structure Covid19Info to store the information of COVID 19 cases of countries around the world. It keeps the name of the country, number of COVID 19 cases, number of deaths, current test positive rate, etc. After defining the structure, declare a variable of Covid19Info type with some initial values for Bangladesh. [8]
C++ program: Create a Date class that contains three members: the month, the day of the...
C++ program: Create a Date class that contains three members: the month, the day of the month, and the year, all of type int. The user should enter a date in the format 12/31/2001, store it in an object of type Date, then retrieve the object and print it out in the same format. Next create an employee class. The member data should comprise an employee number (type int) and the employee’s compensation (in dollars; type float). Extend the employee...
C++ C++ Write a definition for a structure type for records for CDs. The record contains...
C++ C++ Write a definition for a structure type for records for CDs. The record contains price and total number of songs, . Part of the problem is appropriate choices of type and member names.Then declare two variables of this structure. Write a function called GetData use the structure as function argument to ask the user to input the value from the keyboard for structure variables.
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines...
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines whether they are the sides of a right-angled triangle. The function should take three integer arguments and return 1 (true) if the arguments comprise a right-angled triangle, and 0 (false) otherwise. Use this function in a program that inputs a series of sets of integers. Hint: a^2+b^2=C^2 Codes in C please.s
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT