Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is in C++ program. I\'m getting 3.31662, but the right answer is 3.74166 #i

ID: 3607094 • Letter: T

Question

This is in C++ program. I'm getting 3.31662, but the right answer is 3.74166

#include <iostream>
#include <math.h> // for sqrt()
using namespace std;

class TwoD
{
   protected:
      double x, y; // x and y coordinates
   public:
      // inline implementation of constructor
TwoD(){x = y = 0.0; cout << "TwoD default constructor" << endl;} // default constructor
TwoD(double i, double j):x(i), y(j){cout << "TwoD consctructor with two arguments" << endl;}
  
      // inline implementation of member functions
      void setX(double NewX){x = NewX;}
      void setY(double NewY){y = NewY;}
      double getX() const {return x;}
      double getY() const {return y;}
      // get distance of 2D points
      double getDistance (const TwoD& point) const;
};

// calculate the distance of two 2D points
double TwoD::getDistance(const TwoD& point) const
{
double point1[2];
double point2[2];
double dx, dy;
double distance;

point1[0] = x;
point1[1] = y;

point2[0] = point.getX();
point2[1] = point.getY();

dx = point2[0] - point1[0];
dy = point2[1] - point1[1];

distance = sqrt(dx * dx + dy * dy);
return distance;
}
class ThreeD:public TwoD
{
   private:
      double z;

   public:

// --->ADD CODE HERE<---:
// Create a default inline constructor that reuses the constructor of
// the TwoD class.
// YOUR CODE GOES HERE

ThreeD()/*:TwoD()*/{z=0;}
// --->ADD CODE HERE<---:
// Create an inline constructor that initializes the 3D point
// and reuses the TwoD class.
// YOUR CODE GOES HERE
ThreeD(double i, double j, double k)/*:TwoD(i,j)*/{z = k;}
      void setZ(double NewZ){z = NewZ;}
      double getZ() const {return z;}
      // get distance for two 3D points
      double getDistance(const ThreeD& point) const;
};

// --->ADD CODE HERE<---:
// Overload the definition of getDistance() of TwoD class so that it
// can calculate the distance between two 3D points
double ThreeD::getDistance(const ThreeD& point) const
{
   // YOUR CODE GOES HERE:
double point1[3];
double point2[3];
double dx,dy,dz;
double distance;

   point1[0] = x;
   point1[1] = y;
   point1[2] = z;

   point2[0] = point.getX();
   point2[1] = point.getY();
   point2[2] = point.getZ();

   dx = point2[0] - point1[0];
   dy = point2[1] - point1[1];
   dz = point2[2] - point1[2];

   distance = sqrt(dx * dx + dy * dy + dz * dz);
   return distance;
}

// --->ADD CODE HERE<---:
// Implement a main() function.
// You should ask the user for the xyz coordinates of two 3D points,
// and then calculate and print out the distance between these two points.
int main()
{
   // YOUR CODE GOES HERE
int x,y,z;
ThreeD obj1;

cout << "Enter the xyz coordinates of the first point:";
cin >> x >> y >> z;
obj1.setX(x);
obj1.setY(y);
obj1.setZ(z);

cout << "Enter the xyz coordinates of the second point:";
cin >> x >> y >> z;
ThreeD obj2(x,y,z);

cout << "The distance between the two points is:" << obj1.getDistance(obj2) << endl;
}

Explanation / Answer

Hi..Please check the below code for the inpu of obj1 is 1 2 3 and obj2 is 4 5 6 and output gives as 3.7416. Please check the below code once and let me know any queries.


#include <iostream>
#include <math.h> // for sqrt()
using namespace std;
class TwoD
{
protected:
double x, y; // x and y coordinates
public:
// inline implementation of constructor
TwoD(){x = y = 0.0; cout << "TwoD default constructor" << endl;} // default constructor
TwoD(double i, double j):x(i), y(j){cout << "TwoD consctructor with two arguments" << endl;}
  
// inline implementation of member functions
void setX(double NewX){x = NewX;}
void setY(double NewY){y = NewY;}
double getX() const {return x;}
double getY() const {return y;}
// get distance of 2D points
double getDistance (const TwoD& point) const;
};
// calculate the distance of two 2D points
double TwoD::getDistance(const TwoD& point) const
{
double point1[2];
double point2[2];
double dx, dy;
double distance;
point1[0] = x;
point1[1] = y;
point2[0] = point.getX();
point2[1] = point.getY();
dx = point2[0] - point1[0];
dy = point2[1] - point1[1];
distance = sqrt(dx * dx + dy * dy);
return distance;
}
class ThreeD:public TwoD
{
private:
double z;
public:

// --->ADD CODE HERE<---:
// Create a default inline constructor that reuses the constructor of
// the TwoD class.
// YOUR CODE GOES HERE
ThreeD()/*:TwoD()*/{z=0;}
// --->ADD CODE HERE<---:
// Create an inline constructor that initializes the 3D point
// and reuses the TwoD class.
// YOUR CODE GOES HERE
ThreeD(double i, double j, double k)/*:TwoD(i,j)*/{z = k;}
void setZ(double NewZ){z = NewZ;}
double getZ() const {return z;}
// get distance for two 3D points
double getDistance(const ThreeD& point) const;
};
// --->ADD CODE HERE<---:
// Overload the definition of getDistance() of TwoD class so that it
// can calculate the distance between two 3D points
double ThreeD::getDistance(const ThreeD& point) const
{
// YOUR CODE GOES HERE:
double point1[3];
double point2[3];
double dx,dy,dz;
double distance;
point1[0] = x;
point1[1] = y;
point1[2] = z;
point2[0] = point.getX();
point2[1] = point.getY();
point2[2] = point.getZ();
dx = point2[0] - point1[0];
dy = point2[1] - point1[1];
dz = point2[2] - point1[2];
cout<<dx<<" "<<dy<<" "<<dz;
distance = sqrt(dx * dx + dy * dy + dz * dz);
return distance;
}

// --->ADD CODE HERE<---:
// Implement a main() function.
// You should ask the user for the xyz coordinates of two 3D points,
// and then calculate and print out the distance between these two points.
int main()
{
// YOUR CODE GOES HERE
int x,y,z;
ThreeD obj1;

cout << "Enter the xyz coordinates of the first point:";
cin >> x >> y >> z;
obj1.setX(x);
obj1.setY(y);
obj1.setZ(z);
cout << "Enter the xyz coordinates of the second point:";
cin >> x >> y >> z;
ThreeD obj2(x,y,z);
cout << "The distance between the two points is:" << obj1.getDistance(obj2) << endl;
}

Output:

TwoD default constructor                                                                                              

Enter the xyz coordinates of the first point:1                                                                        

2                                                                                                                     

3                                                                                                                     

Enter the xyz coordinates of the second point:4                                                                       

5                                                                                                                     

6   

TwoD default constructor The distance between the two points is:3.74166

Calculation like obj1-obj2

(1-3)*(1-3) + (2-4)*(2-4) + (3-6) = 14

square root of 14 is 3.74166

Any queries comment here. Thank you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote