Using C Write a program that computes two measurements using

Using C++

Write a program that computes two measurements using only pointers to a structure and two
functions to calculate:
(a) The distance between two points and
(b) The slope of the line passing through these two points.

Note: 1. The program should NOT to compute the slope of a vertical line.
2. The functions should return the appropriate values
3. The functions should receive a pointer to the structure.
4. Use the arrow operator to access the elements of the structure
• Fractional values need to be approximated by decimals.
• Recall that you should not declare a pointer without initializing it.

The output should look like the one of following:
The distance between (a, b) and (c, d) is # and the slope is #
The distance between (a, b) and (c, d) is # but there is no slope
Test your program with these sets of points:
(4, 7) and (8, 10), (4, -2) and (2, 2), (4, -2) and (3, -2), (5, 3) and
(5, -2), (1/3, -3) and (2, -1/4)

Solution

C++ code:

#include <bits/stdc++.h>
using namespace std;
struct point
{
   float x,y;
};

struct myfunc
{
   float slope(point p1, point p2)
   {
       if(((p2.x) - (p1.x)) == 0)
       {
           cout << \"slope is infinity as given line is verticle!\" << endl;
       }
       else
       {
       return (float(p2.y) - (p1.y))/float((p2.x = p1.x));
       }
   }
   float distance(point p1, point p2)
   {
   return sqrt( pow(((p2.y) - (p1.y)),2) + pow( ((p2.x = p1.x)) , 2) );  
   }
};
int main()
{
   point p1 = point();
   point p2 = point();  
   myfunc f = myfunc();

   p1.x = 4; p1.y = 7;
   p2.x = 8; p2.y = 10;
   cout << \"The distance between (\" << p1.x << \",\" << p1.y << \") and (\" << p2.x << \",\" << p2.y << \") is \" << f.distance(p1,p2) << \" and the slope is \" << f.slope(p1,p2) << endl << endl;

   p1.x = 4; p1.y = -2;
   p2.x = 2; p2.y = 2;
   cout << \"The distance between (\" << p1.x << \",\" << p1.y << \") and (\" << p2.x << \",\" << p2.y << \") is \" << f.distance(p1,p2) << \" and the slope is \" << f.slope(p1,p2) << endl<< endl;

   p1.x = 3; p1.y = -2;
   p2.x = 4; p2.y = -2;
   cout << \"The distance between (\" << p1.x << \",\" << p1.y << \") and (\" << p2.x << \",\" << p2.y << \") is \" << f.distance(p1,p2) << \" and the slope is \" << f.slope(p1,p2) << endl<< endl;

   p1.x = 5; p1.y = 3;
   p2.x = 5; p2.y =-2 ;
   cout << \"The distance between (\" << p1.x << \",\" << p1.y << \") and (\" << p2.x << \",\" << p2.y << \") is \" << f.distance(p1,p2) << \" and the slope is \" << f.slope(p1,p2) << endl<< endl;

   p1.x = 1.0/3.0; p1.y = -3;
   p2.x = 2; p2.y = -1.0/4.0;
   cout << \"The distance between (\" << p1.x << \",\" << p1.y << \") and (\" << p2.x << \",\" << p2.y << \") is \" << f.distance(p1,p2) << \" and the slope is \" << f.slope(p1,p2) << endl<< endl;


   return 0;
}

Sample output:

The distance between (4,7) and (8,10) is 5 and the slope is 0.75

The distance between (4,-2) and (2,2) is 5.65685 and the slope is 1

The distance between (3,-2) and (4,-2) is 3 and the slope is 0

slope is infinity as given line is verticle!
The distance between (5,3) and (5,-2) is 7.07107 and the slope is 0

The distance between (0.333333,-3) and (2,-0.25) is 2.77013 and the slope is 8.25

Using C++ Write a program that computes two measurements using only pointers to a structure and two functions to calculate: (a) The distance between two points
Using C++ Write a program that computes two measurements using only pointers to a structure and two functions to calculate: (a) The distance between two points

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site