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

Input the coordinates of two points in the Cartesian coordinate system. Calculat

ID: 3887891 • Letter: I

Question

Input the coordinates of two points in the Cartesian coordinate system. Calculate the distance between the points and the midpoint between them. The program will be submitted in Java and also in C++. Also, include an appropriate algorithm. Accept 4 real numbers from the user, representing coordinates x_1, y_2, x_2, and y_2. Note that the C++ cmath library includes two useful functions, pow and squareroot . Formulas: distance: d = squareroot (x_2 - x_1)^2 + (y_2 - y_1)^2 midpoint: (x_1 + x_2/2, y_1 + y_2/2 Print appropriate labels, the original points, the distance, and the midpoint. Print the original points and the midpoint with one decimal position: print the distance with two decimal positions. Sample output (you may change the layout):

Explanation / Answer

Below is your code in C++. Let me know if you have any issues: -

#include<iostream>

using namespace std;

#include<cmath>

#include<iomanip>

int main() {

cout<<"Enter 4 real numbers for x1,y1,x2,y2 : ";

double x1,y1,x2,y2;

cin>>x1;

cin>>y1;

cin>>x2;

cin>>y2;

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

double m1,m2;

m1 = (x1+x2) / 2;

m2 = (y1 + y2) / 2;

cout<<"Points: ("<<fixed << setprecision(1)<<x1<<","<<y1<<") ("<<x2<<","<<y2<<") ";

cout<<"Distance: "<<fixed << setprecision(2) <<dist<<endl;

cout<<"Midpoint: "<<"("<<fixed << setprecision(1) <<m1<<","<<m2<<")"<<endl;

}

Output: -

Enter 4 real numbers for x1,y1,x2,y2 : 3.0
2.5
-1.0
4.0
Points: (3.0,2.5) (-1.0,4.0)
Distance: 4.27
Midpoint: (1.0,3.2)

JAVA CODE

Distance.java

import java.util.Scanner;

public class Distance {

public static void main(String[] args) {

System.out.println("Enter 4 real numbers for x1,y1,x2,y2 : ");

double x1, y1, x2, y2;

Scanner sc = new Scanner(System.in);

x1 = Double.parseDouble(sc.next());

y1 = Double.parseDouble(sc.next());

x2 = Double.parseDouble(sc.next());

y2 = Double.parseDouble(sc.next());

double dist = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));

double m1, m2;

m1 = (x1 + x2) / 2.0;

m2 = (y1 + y2) / 2.0;

System.out.println("Points: (" + (Math.round(x1 * 10.0) / 10.0) + "," + (Math.round(y1 * 10.0) / 10.0)

+ ") (" + (Math.round(x2 * 10.0) / 10.0) + "," + (Math.round(y2 * 10.0) / 10.0) + ") ");

System.out.println("Distance: " + (Math.round(dist * 100.0) / 100.0));

System.out.println(

"Midpoint: " + "(" + (Math.round(m1 * 10.0) / 10.0) + "," + (Math.round(m2 * 10.0) / 10.0) + ")");

}

}

Sample Run: -

Enter 4 real numbers for x1,y1,x2,y2 :
3.0
2.5
-1.0
4.0
Points: (3.0,2.5) (-1.0,4.0)

Distance: 4.27
Midpoint: (1.0,3.3)