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

Write a program in C++ that helps the user calculate both the distance between t

ID: 3850995 • Letter: W

Question

Write a program in C++ that helps the user calculate both the distance between two points in the 2D Cartesian plane and the midpoint of the line segment those two points define. Try to make your results as readable as possible.

Oh, btw, the exact level of the user may be a bit variable. The client said they want to use this program anywhere in their K-12 school system. So, you'll need to account for the possibility that the user may use appropriate notation during entry or might not. They may leave off one or both parentheses or leave out the comma between the two coordinates. If they do, don't worry about it, but print a little reminder message to tell them what notation they messed up on.

So, to enter the point (3,-4), the user should be able to enter any of: (3,-4), 3,-4), (3,-4, (3 -4), 3,-4, 3 -4), (3 -4, or even 3 -4. Only the first would be quiet, however, the others would generate 1, 2, or even 3 warning/reminder messages about missing notation.

As an example, you might have the program interaction look something like (the parts in this color are typed by the user) /pointcalcinp. out Welcome to the 2D Point Program! Where is your first point? 3.4 12.2) You were missing the open parenthesis before the x coordinate You were missing the comma to separate coordinates Please use proper notation... Where is your second point? (13.4, 12.2 You were missing the close parenthesis after the y coordinate Please use proper notation... Thank you! Calculating Done. (3. 4, 12.2) is 10 units away from (13.4 12.2) The midpoint of the line segment from (3.4, 12.2) to (13.4 12.2) is (8.4 12.2) Thank you for using the 2PP!! Endeavor to have a day.

Explanation / Answer

Here is the code along with output. In case of any issues, please post a comment and I shall respond. Please don't forget to rate the answer if it helped. Thank you.

#include <iostream>

#include <cmath>

#include <cstring>

using namespace std;

void parse(string point, double &x, double &y);

void calculate(double x1, double y1, double x2, double y2);

int main()

{

string p1, p2;

double x1, y1, x2, y2;

  

cout << " Welcome to the 2D Point Program !!! ";

  

cout << " Where is your first point? ";

getline(cin, p1);

parse(p1, x1, y1);

  

cout << " Where is your second point? ";

getline(cin, p2);

parse(p2, x2, y2);

  

cout << " Thank you!! ";

  

calculate(x1, y1, x2, y2);

  

cout << " Thank you for using the 2PP!!" ;

  

cout << " Endeavor to have a day... ";

  

  

return 0;

}

//parse the input string and check for errors and then extract the x and y coordinates

//and update the reference variables x and y

void parse(string point, double &x, double &y)

{

int idx;

bool error = false;

//look for (

idx = point.find("(");

if(idx == string::npos)

{

cout << " You were missing the open parenthesis before the x coordinate!";

error = true;

}

else //leave out the ( and get rest of the string

point = point.substr(idx+1);

  

//look for comma

idx = point.find(",");

if(idx == string::npos)

{

cout << " You were missing the comma to separate coordinates!";

error = true;

}

else //replace the comma by a space

point = point.replace(idx, 1, " ");

  

//look for )

idx = point.find(")");

if(idx == string::npos)

{

cout << " You were missing the closing parenthesis after the y coordinate!";

error = true;

}

else //get all the string leaving out the )

point = point.substr(0, idx);

  

if(error)

cout <<" Please use proper notation... ";

  

//now point will be in the format x <space> y

idx = point.find(" ");

  

  

//separate out x and y based on index of the space

string strx = point.substr(0, idx);

string stry = point.substr(idx+1);

  

//convert them to double

x = atof(strx.c_str());

y = atof(stry.c_str());

}

//calculate distance and mid point

void calculate(double x1, double y1, double x2, double y2)

{

  

double distance, midx, midy;

  

cout << " Calculating... " ;

  

distance = sqrt(pow(x2-x1, 2) + pow(y2-y1, 2));

midx = (x1 + x2)/2;

midy = (y1 + y2)/2;

  

cout << " Done. ";

  

cout << " (" << x1 << ", " << y1 << ") is " << distance << " units away from ";

cout << "(" << x2 << ", " << y2 << ").";

  

cout << " The midpoint of the line segment from (" << x1 << ", " << y1 << ")";

cout << " to (" << x2 << ", " << y2 << ") is (" << midx << ", " << midy << ").";

  

}

output

           Welcome to the 2D Point Program !!!

Where is your first point? 3.4 12.2 )

   You were missing the open parenthesis before the x coordinate!
   You were missing the comma to separate coordinates!

   Please use proper notation...

Where is your second point? (13.4 , 12.2

   You were missing the closing parenthesis after the y coordinate!

   Please use proper notation...

Thank you!! Calculating... Done.

(3.4, 12.2) is 10 units away from (13.4, 12.2).

The midpoint of the line segment from (3.4, 12.2) to (13.4, 12.2) is (8.4, 12.2).

Thank you for using the 2PP!!

Endeavor to have a day...

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