0

The correct answer for the value of x1=7, x2=3, y1=12, y2=9 is supposed to be 5. This code is giving me 5.9... I can't figure out what the problem is.

#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main()
{

    int x1, x2, y1, y2;
    double distance;

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

    cout << "Enter x1: ";
    cin >> x1;
    cout << "Enter x2: ";
    cin >> x2;
    cout << "Enter y1: ";
    cin >> y1;
    cout << "Enter y2: ";
    cin >> y2;

    cout << "The distance between two points is: " << distance << endl;

    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 8
    Your program exhibits undefined behavior, due to doing calculations on non-initialized variables (i.e. You do your calculation before reading the values). Consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of coding randomly. – Algirdas Preidžius Feb 08 '18 at 21:50
  • So I should put the formula under the read values? – Mussie Tesfay Feb 08 '18 at 21:51
  • @MussieTesfay yes. Otherwise the calculations are done before the input on uninitialized variables. Remember, evaluation is done from the top-down. – Arnav Borborah Feb 08 '18 at 21:52
  • @MussieTesfay What do you think, given the explanation, that I had provided? – Algirdas Preidžius Feb 08 '18 at 21:53
  • 2
    The title of this question needs to be rephrased. "Can't figure out the issue" says absolutely nothing – Morgan Feb 08 '18 at 21:53
  • @Morgan Someone has already edited it, but is in the review queue – Dijkgraaf Feb 08 '18 at 21:54
  • It just didn't feel right to do that. I am used to defining all variables before dealing with anything else. – Mussie Tesfay Feb 08 '18 at 21:55
  • 3
    When writing a program you do not write mathematical equations, but statements that are executed one by one, so you cannot change order any way you want. – Slava Feb 08 '18 at 21:57
  • Got it! Thank you so much! – Mussie Tesfay Feb 08 '18 at 22:02
  • _I am used to defining all variables before dealing with anything else_ What you did was you `declared` the variables. You `define` them when you assign values to them from console input. – Killzone Kid Feb 08 '18 at 22:02
  • 1
    Consider using `x * x` rather than `pow(x, 2)`. Often it is more accurate and faster. – Thomas Matthews Feb 08 '18 at 22:15

1 Answers1

3

Your expectation that:

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

will use the values of the variables that the user input is ill-founded. When that line is executed, the variables x1, etc are not initialized. Hence, your program has undefined behavior.

Move that line after the line where you read y2.

// Not good to be here.
// distance = sqrt(pow((x2-x1), 2) + pow((y2-y1), 2));

cout << "Enter x1: ";
cin >> x1;
cout << "Enter x2: ";
cin >> x2;
cout << "Enter y1: ";
cin >> y1;
cout << "Enter y2: ";
cin >> y2;

distance = sqrt(pow((x2-x1), 2) + pow((y2-y1), 2));
R Sahu
  • 204,454
  • 14
  • 159
  • 270