-7

When running this the var number 10 will always equal 0 after doing the math to find the distance formula. How can I fix this. Thanks in advance

        Int32 Number1;
        Int32 Number2;
        Int32 Number3;
        Int32 Number4;
        Int32 Number5;

        Console.WriteLine("Type in your 1st X coordinate");
        Number1 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Type in your 1st Y coordinate");
        Number2 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Type in your 2nd X coordinate");
        Number3 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Type in your 2nd Y coordinate");
        Number4 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Do you want to use the distance or midpoint formula.(Type 0 for distance and 1  for midpoint)");
        Number5 = Convert.ToInt32(Console.ReadLine());


        Int32 Number6 = Number1 - Number3;
        Int32 Number7 = Number6 * Number6;
        Int32 Number8 = Number2 - Number4;
        Int32 Number9 = Number8 * Number8;
        Int32 Number10 = Number6 + Number8;

        if (Number5 == 0)
        {


           Console.Out.Write("Square Root of");
           Console.Out.Write(Number10);



        }
9
  • 1
    Have you taken a look at what the values of Number6, Number8, Number1, Number3, Number2, and Number4 are?
    – maniak1982
    Jun 1, 2015 at 15:20
  • Yes you fill them out as shown when in the readlines.
    – Maximus
    Jun 1, 2015 at 15:23
  • Since you're calculating distance between two points, shouldn't you Math.Abs() when you subtract the two points
    – Shar1er80
    Jun 1, 2015 at 15:24
  • 2
    1. It's generally not best-practice to use Int32, use int instead. 2. Those variable names are horrid, it's going to make debugging things extremely unpleasant in the future (and even now). Meaningful variable names are extremely helpful. 3. You are doing Number10 = Number6 + Number8, which is wrong. It should be Number10 = Number7 + Number9. (Note what I said about debugging.) Then you should do your Math.Sqrt on it. Jun 1, 2015 at 15:25
  • Step through it, inspect the variables. You'll almost certainly see what the problem is. Jun 1, 2015 at 15:25

2 Answers 2

2

If you use more sensible variable names and declare the variables at the point of assignment, like so:

        Console.WriteLine("Type in your 1st X coordinate");
        int x1 = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Type in your 1st Y coordinate");
        int y1 = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Type in your 2nd X coordinate");
        int x2 = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Type in your 2nd Y coordinate");
        int y2 = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Do you want to use the distance or midpoint formula.(Type 0 for distance and 1  for midpoint)");
        int forumlaSelection = Convert.ToInt32(Console.ReadLine());

        int dx = x1 - x2;
        int dxSquared = dx * dx;
        int dy = y1 - y2;
        int dySquared = dy * dy;
        int dxPlusDy = dx + dy;

        if (forumlaSelection == 0)
        {
            Console.Out.Write("Square Root of");
            Console.Out.Write(dxPlusDy);
        }

Then you might be more likely to see the error.

See that dxPlusDy? Looks to me that it should be this instead:

int dxSquaredPlusDySquared = dxSquared + dySquared;

And your final output should be the square root of that:

Console.Out.Write(Math.Sqrt(dxSquaredPlusDySquared));

Also, I recommend using double rather than int:

        Console.WriteLine("Type in your 1st X coordinate");
        double x1 = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Type in your 1st Y coordinate");
        double y1 = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Type in your 2nd X coordinate");
        double x2 = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Type in your 2nd Y coordinate");
        double y2 = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Do you want to use the distance or midpodouble formula.(Type 0 for distance and 1  for midpoint)");
        int forumlaSelection = Convert.ToInt32(Console.ReadLine());

        double dx = x1 - x2;
        double dxSquared = dx * dx;
        double dy = y1 - y2;
        double dySquared = dy * dy;
        double dxSquaredPlusDySquared = dxSquared + dySquared;

        if (forumlaSelection == 0)
        {
            Console.Out.Write("Square Root of");
            Console.Out.Write(Math.Sqrt(dxSquaredPlusDySquared));
        }
0

Though I hate to answer questions like this, I feel you could do with a good understanding and explanation of some best-practices here.

  1. Use meaningful variable names. Number[N] is not meaningful. Making this change causes a discovery of multiple supreme flaws in your logic immediately.
  2. Don't use Int32, it's generally best-practice to use int in this case. (Though, for your situation, double is more appropriate. But that change is unnecessary.)
  3. Your formula is way off. (Renaming variables more meaningfully points this out readily.)
  4. You don't catch errors with invalid input. You should do some attempt at this. The five while loops handle this in very small overhead.

MSDN Int32.TryParse

The following modification fixes all of these issues:

int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
int option = 0;

Console.WriteLine("Type in your 1st X coordinate");
while (!int.TryParse(Console.ReadLine(), out x1))
    Console.WriteLine("That number is invalid, please try again:");

Console.WriteLine("Type in your 1st Y coordinate");
while (!int.TryParse(Console.ReadLine(), out y1))
    Console.WriteLine("That number is invalid, please try again:");

Console.WriteLine("Type in your 2nd X coordinate");
while (!int.TryParse(Console.ReadLine(), out x2))
    Console.WriteLine("That number is invalid, please try again:");

Console.WriteLine("Type in your 2nd Y coordinate");
while (!int.TryParse(Console.ReadLine(), out y2))
    Console.WriteLine("That number is invalid, please try again:");

Console.WriteLine("Do you want to use the distance or midpoint formula.(Type 0 for distance and 1  for midpoint)");
while (!int.TryParse(Console.ReadLine(), out option))
    Console.WriteLine("That number is invalid, please try again:");

int x2x1 = x2 - x1;
int x2x1Squared = x2x1 * x2x1;
int y2y1 = y2 - y1;
int y2y1Squared = y2y1 * y2y1;
int addedSquaredValues = x2x1Squared + y2y1Squared;

if (option == 0)
    Console.WriteLine("Distance between ({0},{1}) and ({2},{3}) is {4}", x1, y1, x2, y2, Math.Sqrt(addedSquaredValues));

Console.ReadLine();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.