Home > Mobile >  for loop to run through a list of X Y inputs
for loop to run through a list of X Y inputs

Time:03-27

I am Day one new to coding with C# and need to learn it as part of my graduate Job, they have set me a challenge of running a calculation through a list of two different inputs ( X and Y ) for now I just want the calculation to go down through the list and take two different numbers and apply the calculation. Ill paste the code in but this is the order Id like this to run in.

xInput - 100    // first number to be taken
xInput - 200    //second number to be taken
xInput - 300    // Id like the code to cycle through going from first to second agian at this point!
xInput - 400

I realise that this is probably very obvious but I am so new to coding that I have no idea where to begin!

List<double> xInputs = new List<double>()
{
    0, 2194.233, 758.7178, 2381.054, 661.3527,
};

List<double> yInputs = new List<double>()
{
    4000, 3310.551, 2875.47, 2095.711, 1287.857,
};

for (int i = 0; i < xInputs.Count; i  )
{
    double xTranslation = Math.Pow(xInputs[i] - xInputs[i], 2)   Math.Pow(yInputs[i] - yInputs[i], 2);
    double xySqrt = Math.Sqrt(xTranslation);
    Console.WriteLine(xInputs);
}

CodePudding user response:

Not clear what the issue is here, but if you want this output

       X            Y         Dist
       0         4000            0
2194.233     3310.551         2300
758.7178      2875.47     1356.547
2381.054     2095.711     3048.891
661.3527     1287.857     2791.614

then run this code

static class Program
{
    static void Main(string[] args)
    {
        double[] xInputs = new double[]
        {
            0, 2194.233, 758.7178, 2381.054, 661.3527,
        };

        double[] yInputs = new double[]
        {
            4000, 3310.551, 2875.47, 2095.711, 1287.857,
        };

        double[] xySqrt = new double[xInputs.Length];
        for (int i = 0; i < xySqrt.Length; i  )
        {
            xySqrt[i] = Math.Sqrt(Math.Pow(xInputs[i]-xInputs[0], 2)   Math.Pow(yInputs[i]-yInputs[0], 2));
        }
        Console.WriteLine($"{"X",12} {"Y",12} {"Dist",12}");
        for (int i = 0; i < xySqrt.Length; i  )
        {
            Console.WriteLine($"{xInputs[i],12:g7} {yInputs[i],12:g7} {xySqrt[i],12:g7}");
        }
    }
}

Of course since you are learning to code, you should recognize that 'x' and 'y' values always go in parts, and so it is better to use a built-in type System.Numerics.Vector2 to represent these values. As an added bonus the distance function already exists as Vector2.Distance().

using System.Numerics;

static class Program
{
    static void Main(string[] args)
    {
        Vector2[] inputs = new Vector2[] 
        {
            new Vector2(0,           4000f),
            new Vector2(2194.233f,   3310.551f),
            new Vector2(758.7178f,   2875.47f), 
            new Vector2(2381.054f,   2095.711f),
            new Vector2(661.3527f,   1287.857f),
        };

        double[] xySqrt = new double[inputs.Length];
        for (int i = 0; i < xySqrt.Length; i  )
        {
            xySqrt[i] = Vector2.Distance(inputs[i], inputs[0]);
        }
        Console.WriteLine($"{"Point",24} {"Dist",12}");
        for (int i = 0; i < xySqrt.Length; i  )
        {
            Console.WriteLine($"{inputs[i],24:g7} {xySqrt[i],12:g7}");
        }
    }
}

In the above code the Vector2.Distance(inputs[i], inputs[0]); is much clearer to the reader as what is going on, compared to Math.Sqrt(Math.Pow(xInputs[i]-xInputs[0], 2) Math.Pow(yInputs[i]-yInputs[0], 2));

CodePudding user response:

It sounds like you want to traverse the list using pairs of inputs. That's easy with just a couple of minor changes to your code.

for (int i = 0; i < xInputs.Count - 1; i  )
{
    double xTranslation = Math.Pow(xInputs[i   1] - xInputs[i], 2)   Math.Pow(yInputs[i   1] - yInputs[i], 2);
    double xySqrt = Math.Sqrt(xTranslation);
    Console.WriteLine(xySqrt);
}

Note that the list now stops after i < xInputs.Count - 1 and the first inputs are indexed with i 1.

I also changed your WriteLine to output result.

That produces:

2299.9996482369297
1499.9997886639987
1799.9996788642604
1900.0001701414897

An alternative approach you might want to consider is this:

List<(double x, double y)> inputs = new List<(double x, double y)>()
{
    (0, 4000), (2194.233, 3310.551), (758.7178, 2875.47),
    (2381.054, 2095.711), (661.3527, 1287.857), 
};

IEnumerable<double> distances =
    from i in Enumerable.Range(0, inputs.Count - 1)
    let x0 = inputs[i].x
    let x1 = inputs[i   1].x
    let y0 = inputs[i].y
    let y1 = inputs[i   1].y
    select Math.Sqrt(Math.Pow(x1 - x0, 2.0)   Math.Pow(y1 - y0, 2.0));

foreach (double distance in distances)
{
    Console.WriteLine(distance);
}

Or even this that I think reads very clearly:

IEnumerable<double> distances =
    from ps in inputs.Buffer(2, 1)
    where ps.Count == 2
    let dx = ps[1].x - ps[0].x
    let dy = ps[1].y - ps[0].y
    select Math.Sqrt(Math.Pow(dx, 2.0)   Math.Pow(dy, 2.0));

Note: that uses Microsoft's Interactive Framework (NuGet "System.Interactive") to get the Buffer operator.

  • Related