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

C++ using Microsoft visual studio Write the function distance() that receives fo

ID: 3834861 • Letter: C

Question

C++ using Microsoft visual studio

Write the function distance() that receives four double type parameters representing the x and y coordinates of two points ( ordered pair) in the rectangular coordinates system. The function returns the distance between the two points. Use the distance formula in page 121 of your textbook.

To test this function, write a main function that prompts the user to enter the x and y components of the two points then calls the function distance() and displays the distance between the two points.

Explanation / Answer

#include <iostream>
#include<math.h>
using namespace std;

double distance(double x1, double y1, double x2, double y2){
   return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}

int main() {
   // your code goes here
   cout << "Enter the coordinates of two points"<<endl;
   double x1, y1, x2, y2;
   cin >> x1 >> y1 >> x2 >> y2;
   double ans = distance(x1, y1, x2, y2);
   cout << "Answer is "<<ans;
   return 0;
}

OUTPUT :

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