The distance D between two points with coordinates (x1,y1) and (x2,y2) on a plane is given byD = [(x2- x1)2 + (y2 – y1)2]1/2The distance between two points (x1,y1,z1) and (x2,y2,z2) in a three dimensional space is given byD = [(x2-x1)2 +(y2-y1)2 + (z2-z1)2]1/2Develop a program in C++ using function overloading by writing the same function ComputeDistance() with different signatures. Round off distance to two decimal pointsNote: Syntax to print 'x' decimal places of variable 'a'include <iomanip>usecout<<fixed<<setprecision(x)<<a;Input formatx coordinate of point 1y coordinate of point 1z coordinate of point 1x coordinate of point 2y coordinate of point 2z coordinate of point 2Output formatDistance in two dimensional planeDistance in three dimensional space
Question
The distance D between two points with coordinates (x1,y1) and (x2,y2) on a plane is given byD = [(x2- x1)2 + (y2 – y1)2]1/2The distance between two points (x1,y1,z1) and (x2,y2,z2) in a three dimensional space is given byD = [(x2-x1)2 +(y2-y1)2 + (z2-z1)2]1/2Develop a program in C++ using function overloading by writing the same function ComputeDistance() with different signatures. Round off distance to two decimal pointsNote: Syntax to print 'x' decimal places of variable 'a'include <iomanip>usecout<<fixed<<setprecision(x)<<a;Input formatx coordinate of point 1y coordinate of point 1z coordinate of point 1x coordinate of point 2y coordinate of point 2z coordinate of point 2Output formatDistance in two dimensional planeDistance in three dimensional space
Solution
Here is a simple C++ program that uses function overloading to compute the distance between two points in both two and three dimensions:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// Function to compute distance in 2D
double ComputeDistance(double x1, double y1, double x2, double y2) {
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
// Function to compute distance in 3D
double ComputeDistance(double x1, double y1, double z1, double x2, double y2, double z2) {
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2));
}
int main() {
double x1, y1, z1, x2, y2, z2;
// Input coordinates
cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
// Compute and print distances
cout << fixed << setprecision(2) << "Distance in two dimensional plane: " << ComputeDistance(x1, y1, x2, y2) << endl;
cout << fixed << setprecision(2) << "Distance in three dimensional space: " << ComputeDistance(x1, y1, z1, x2, y2, z2) << endl;
return 0;
}
This program first defines two overloaded versions of the ComputeDistance function, one for two dimensions and one for three. The main function then reads in the coordinates of two points from the user, computes the distances in both two and three dimensions using the appropriate ComputeDistance function, and prints the results. The setprecision function is used to round the distances to two decimal places.
Similar Questions
Single File Programming QuestionProblem StatementRohit wanted to teach his new batch of students to find the distance between two points.He wants your help in writing a program to accept two points and calculate the distance between them using pointers. The result should be a float value.Note: d = √((x2 – x1)² + (y2 – y1)²)Input format :The first line of input consists of two space-separated integers x1 and y1, representing the coordinates of Point 1.The second line of input consists of two space-separated integers x2 and y2, representing the coordinates of Point 2.Output format :The output prints a floating-point value - the distance between two points with two decimal places.Refer to the sample output for the formatting specificationsCode constraints :In this scenario, the test cases fall under the following constraints:1 ≤ x,y ≤ 1000Sample test cases :Input 1 :2 34 1Output 1 :2.83Input 2 :4 72 8Output 2 :2.24Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0WhitelistSet 1:*
Problem StatementEva is at (3, 4) on a 2D grid, and Angel is at an unknown point (x, y). They want to find the Euclidean distance between them. Write a program taking Angel's coordinates as input and using built-in mathematical functions to output the distance.Formula: distance = sqrt((x - 3)2 + (y-4)2) Note: The program should utilize built-in mathematical functions, pow and sqrt, for accurate calculation of the Euclidean distance.Input format :The input consists of two space-separated integers, x and y, representing the coordinates of Angel's location.Output format :The output displays a single integer representing the Euclidean distance between Eva and Angel.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases will fall under the following constraints:1 ≤ x, y ≤ 1000Sample test cases :Input 1 :5 10Output 1 :6Input 2 :15 3Output 2 :12Input 3 :887 999Output 3 :1330
The distance of the point P (2, 3) from the x-axis is(A) 2 (B) 3 (C) 1 (D) 5
Give the cartesian coordinates of the point C 4:4; 1158; z 2. (b)Give the cylindrical coordinates of the point D x 3:1; y 2:6; z 3. (c) Specifythe distance from C to D
Given the coordinates of two points in the plane, you can use either the distance formula or the Pythagorean theorem to find the distance between them.A.TrueB.False
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.