In mathematics, the distance of a point with coordinates (x,y) from the origin is the square root of the sum of x-squared and y-squared. Assume that Point has already been defined as a structured type with two double fields, x and y, define a function getR that takes as an argument a value of type Point and returns the distance of that point from the origin (as described above).

Respuesta :

Answer:

  1. #include <iostream>
  2. #include<cmath>
  3. using namespace std;
  4. struct Point{
  5.    double x;
  6.    double y;
  7. };
  8. double getR(Point p){
  9.    double d = sqrt(pow(p.x,2) + pow(p.y,2));
  10.    return d;
  11. }
  12. int main()
  13. {
  14.    Point p1;
  15.    p1.x = 3;
  16.    p1.y = 4;
  17.    
  18.    cout<<getR(p1);
  19.    return 0;
  20. }

Explanation:

The solution is written in c++.

Firstly, create a struct type Point with two double fields, x and y (Line 5 - 8).

Create a function getR that accept a Point type object (Line 10). This function will calculate the distance from origin by applying the formula and then return distance (Line 11 - 12).

In main program, we create a Point object, p1, (Line 17 -19) and test the function by passing the p1 object as argument  (Line 21). We shall get the sample output 5.