HI....
I'm doing this question...

Question

Using a non-void function with parameters, write a complete C++ program that prompts the user for the Cartesian coordinates of two points (x1, y1) and (x2, y2) and displays the distance between them computed using the formula:

distance = sqrt( (x1-x2)^2) + ((y1-y2)^2)...

and i came out with this code :

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;


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

int main () {

    cout<<"Enter The First Coordinate :";
    double x1,y1;
    cin>>x1>>y1;
    cout<<"Enter The Second Coordinate :";
    double x2,y2;
    cin>>x2>>y2;
    cout<<endl;

    cout<<"The Distance Between Two Coordinate Is :"<<distance(x1,x2,y1,y2);
}

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

this code run smoothly but not until it displayed the answer...
because when it display the distance,i always got the wrong answer and sometimes it display the NaN....

i dont know whats wrong with my code....
could somebody figure it out??

Thank You....

Dani AI

Generated

A few clarifications that make the cause and fixes explicit (building on question and help).

The bug is not an IO problem — it’s a math/operator mistake. Squaring a value is not the same as multiplying that value by 2, and in C/C++ the caret character ^ is a bitwise XOR, not an exponent operator. Using 2 * (x1 - x2) produces the wrong numeric result and can leave a negative value under sqrt, which yields NaN. That explains the unexpected outputs you saw.

A clearer, safer implementation approach:

  • compute differences first (dx = x2 - x1, dy = y2 - y1), then square them with dx*dx/dy*dy (fast and exact for doubles), or use std::hypot(dx, dy) to get sqrt(dx*dx + dy*dy) with better numerical stability. Also use a natural parameter order (x1, y1, x2, y2) so callers don’t mix coordinates.

Example (illustrative):

double distance(double x1, double y1, double x2, double y2) {
    double dx = x2 - x1;
    double dy = y2 - y1;
    return std::hypot(dx, dy);    // robust: sqrt(dx*dx + dy*dy)
}

Extra tips: validate input (check cin state) before computing, format the output with setprecision if you need a limited number of decimals, and prefer dx*dx over pow(dx,2) when performance and exactness matter. This keeps the math correct and avoids NaN from an unintended negative value inside sqrt.

Recommended Answers

All 6 Replies

You wrote that

distance = sqrt( (x1-x2)^2) + ((y1-y2)^2).. .

and you are multiplying it by 2, instead of doing a power to 2. That is why you are getting a wrong answer.

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

instead you should have

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

i think

sqrt((2*(x1-x2))+(2*(y1-y2))) and sqrt((pow((x1-x2),2))+(pow((y1-y2),2)))

are both same...

The simple math should be similar like this :

2^2 is just same like 2 * 2....both should be the same,so it should not cause any error...

:-O
yes, 2^2 is 2*2, but that is the only case :)

Note that 3^2 does not equal to 3*2 :)

Moreover, why would ^ be created if it's the same as *?? ;)

plus, when you are squaring something, you always get rid off the negative sign, however, if you multiply 2 by some negative number, you will get a negative number. And a negative number under a square root gives you a complex number. That is actually why you are getting the #IND as an answer.

haha...
i see that now...
i'm really careless did that assumption of 2^2 = 2*2..

Thank You afterall.. :)

haha...
i see that now...
i'm really careless did that assumption of 2^2 = 2*2..

Thank You afterall.. :)

You're welcome :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.