Computer Science, asked by jadhavsahil280, 5 months ago

Write a C++ Program that find the distance between two points in 2D and 3D space using function overloading.​

Answers

Answered by nimmikhardiya
0

Explanation:

  • hope it is helpful to you
Attachments:
Answered by surajnegi0600
0

Answer:

#include <iostream>

#include <cmath>

// 2D distance function

double distance(double x1, double y1, double x2, double y2)

{

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

}

// 3D distance function

double distance(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;

   // Take input for 2D points

   std::cout << "Enter x1: ";

   std::cin >> x1;

   std::cout << "Enter y1: ";

   std::cin >> y1;

   std::cout << "Enter x2: ";

   std::cin >> x2;

   std::cout << "Enter y2: ";

   std::cin >> y2;

   // Call 2D distance function

   std::cout << "Distance between points (2D): " << distance(x1, y1, x2, y2) << std::endl;

   // Take input for 3D points

   std::cout << "Enter z1: ";

   std::cin >> z1;

   std::cout << "Enter z2: ";

   std::cin >> z2;

   // Call 3D distance function

   std::cout << "Distance between points (3D): " << distance(x1, y1, z1, x2, y2, z2) << std::endl;

   return 0;

}

Explanation:

This program uses function overloading to calculate the distance between two points in 2D and 3D space. The first distance function takes four parameters (x1, y1, x2, y2) and calculates the distance between the two points using the distance formula. The second distance function takes six parameters (x1, y1, z1, x2, y2, z2) and calculates the distance between the two points in 3D space using the distance formula. In main function, it take inputs of 2D points and calls 2D distance function and then take input of 3D points and call 3D distance function.

More question and answers:

https://brainly.in/question/54784401

https://brainly.in/question/54784296

#SPJ3

Similar questions