Question

In: Computer Science

Write  the following functions using the following structure for a point. struct Point { double x, y;...

Write  the following functions using the following structure for a point.

struct Point {

double x, y;

};

(a) A function that passes a point and returns an integer (1, 2, 3, or 4) to indicate in which quadrant the point is located

int findQuadrant(struct Point testpoint)

{

}

(b) A function that passes two points and returns the distance between them.

float distance(struct Point point1, struct Point point2)

{

}

(c) A function that passes two points and generate the line equation that passes these two points. The function should return the slope and y-intercept.

void line(struct Point point1, struct Point point2, float *slope, float *intercept)

{

}

Solutions

Expert Solution

Thanks for the question.


Here is the completed code for this problem. Comments are included so that you understand whats going on. Let me know if you have any doubts or if you need anything to change.


Thank You !!


========================================================================================

#include <iostream>
#include<cmath>
using namespace std;

struct Point {
double x, y;
};

int findQuadrant(struct Point testpoint){
  
   if(testpoint.x>=0){
       if(testpoint.y>=0){
           return 1 // when both x and y are positive
       }else{
           return 3; // when x is positive and y is negative
       }
      
   }else{4
       if(testpoint.y>=0){
           return 2 // when x is negative and y is positive
       }else{
           return 3; // when x is positive and y is negative
       }
   }
}

float distance(struct Point point1, struct Point point2){
  
   float squareX = (point1.x-point2.x)*(point1.x-point2.x);
   float squareY = (point1.y-point2.y)*(point1.y-point2.y);
   return pow(squareX+squareY,0.5); // square root of x^2 + y^2

}

void line(struct Point point1, struct Point point2, float *slope, float *intercept){
   if(point1.x==point2.x){
       *intercept=0; // there will be no y intercept
       *slope=0; // slope will be infinity
   }else if(point1.y==point2.y){
       *intercept=point1.y; // intercept will be that of point1.y or point2.y
       *slope=0; // slope will be zero
   }else{
       *slope=(point1.y-point2.y)/(point1.x-point2.x); // find slope
       *intercept=*slope*point1.x +point1.y; // use slope and any other point to find the intercept
   }

}

========================================================================================


Related Solutions

write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
QUESTION 60 Given the following Product structure: struct Product {     string name;     double price;...
QUESTION 60 Given the following Product structure: struct Product {     string name;     double price;     int quantity;     bool equals(const Product&); }; how would you define the equals function so two products are equal if their names and prices are equal? a. bool equals(const Product& to_compare) {     return (name == to_compare.name && price == to_compare.price); } b. bool Product::equals(const Product& to_compare) {     return (name == to_compare.name || price == to_compare.price); } c. bool equals(const Product& to_compare)...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
Write the demand functions for the following utility function: U = ln(x) + ln(y)
Write the demand functions for the following utility function: U = ln(x) + ln(y)
in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y)...
in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y) in 2-D space. The private data members are the coordinates X and Y of type ‘double’. The public member functions are: A default constructor (no parameters) to initialize X and Y to zero. An explicit value constructor with two parameters to initialize the values of X and Y. Two functions, one to set the X value and the other to set the Y value...
using / for integral Evaluate the double integral //R cos( (y-x)/(y+x) )dA where R is the...
using / for integral Evaluate the double integral //R cos( (y-x)/(y+x) )dA where R is the trapezoidal region with vertices (1,0), (2,0), (0,2), and (0,1)
Consider the following five utility functions. G(x,y) = (x)1/2 + y H(x,y) = (x+1)1/2 + y...
Consider the following five utility functions. G(x,y) = (x)1/2 + y H(x,y) = (x+1)1/2 + y + 1 L(x,y) = ln(x) + y U(x,y) = x ey W(x,y) = 100 x - 4x2 + 3 y Z(x,y) = -(100 - x)2 -3 (y-200)2 Which function or functions represent the same preferences as U?   a.G b.U c.None. d.W e.Z f.L g.H
Consider the following utility functions: (i) u(x,y) = x2y (ii) u(x,y) = max{x,y} (iii) u(x,y) =...
Consider the following utility functions: (i) u(x,y) = x2y (ii) u(x,y) = max{x,y} (iii) u(x,y) = √x + y (a) For each example, with prices px = 2 and py = 4 find the expenditure minimising bundle to achieve utility level of 10. (b) Verify, in each case, that if you use the expenditure minimizing amount as income and face the same prices, then the expenditure minimizing bundle will maximize your utility.
Consider the homogeneous second order equation y′′+p(x)y′+q(x)y=0. Using the Wronskian, find functions p(x) and q(x) such...
Consider the homogeneous second order equation y′′+p(x)y′+q(x)y=0. Using the Wronskian, find functions p(x) and q(x) such that the differential equation has solutions sinx and 1+cosx. Finally, find a homogeneous third order differential equation with constant coefficients where sinx and 1+cosx are solutions.
x' = x − y y' = 4y − x^2*y Linearize the system about the point...
x' = x − y y' = 4y − x^2*y Linearize the system about the point (2, 2). Classify the type and stability of the critical point at (2, 2) by examining the linearized system.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT