Search code examples
c#arraysfor-loopwhile-loopnested-loops

How to assign a variable using 3 arrays in C#


I am trying to write an add-in for a finite element geology piece of software.

I have three arrays which are essentially a coordinate system. I want to assign a value to a variable depending on its position in the grid system. Basically I want to say if my node is within an x range and a y range, then my aquifer thickness at this node is this value. So far I have this.

//create an array of xcoords of data points:
double[] xcoord = new double[11] {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50};

//create an array of ycoords of data points:
double[] ycoord = new double[11] {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50};

//create an array of aquifer thickness

double[] aquiferThicknessPoints = new double[121]
        {
        10, 10, 12, 13, 12, 15, 14, 15, 14, 13, 13,
        10, 10, 13, 15, 16, 14, 13, 15, 16, 12, 13,
        12, 14, 15, 18, 19, 17, 14, 15, 18, 14, 14,
        13, 14, 15, 18, 20, 17, 15, 17, 18, 15, 15,
        14, 15, 17, 18, 21, 17, 18, 18, 19, 17, 16,
        15, 15, 17, 17, 20, 21, 21, 19, 19, 18, 18,
        15, 15, 17, 20, 20, 21, 22, 21, 19, 19, 19,
        16, 17, 19, 20, 22, 23, 22, 21, 20, 20, 20,
        17, 18, 20, 22, 23, 24, 24, 23, 22, 20, 21,
        18, 19, 21, 22, 24, 25, 24, 23, 22, 22, 22,
        19, 19, 22, 22, 24, 25, 25, 23, 23, 22, 23,
        }; 

dataPointSpacingHalf = dataPointSpacing / 2;



for (int i = 0; i < xcoord.Length; i++)
{
    for (int j = 0; j < ycoord.Length; j++)
    {
        if (nodeX >= (xcoord[i] - dataPointSpacingHalf) && (nodeX < (xcoord[i] + dataPointSpacingHalf)) && (nodeY >= (ycoord[j] - dataPointSpacingHalf) && (nodeY < (ycoord[j] + dataPointSpacingHalf))))
        {
            aquiferThickness = aquiferThicknessPoints[?];
        }
    }
}

I can see how the nested for loops will loop through 110 times, but i don't know how to assign my aquifer thickness from my array to each loop.

I open to any way of solving this problem as I'm very new new to programming and am still not sure which is the best way to achieve things.


Solution

  • Just use i * xcoord.Length + j insted of ?

    Here is the code:

    for (int i = 0; i < xcoord.Length; i++)
    {
        for (int j = 0; j < ycoord.Length; j++)
        {
                                                          //Here is the magic!
            //without considering coordinates
            //aquiferThickness[i, j] = aquiferThicknessPoints[i * xcoord.Length + j];
    
            //considering coordinates
            aquiferThickness[i, j] = 
                aquiferThicknessPoints[
                    CoordToIndex(xNode,indexedCoords) * xcoord.Length + 
                    CoordToIndex(yNode,indexedCoords)];
    
        }
    }
    

    Also to consider the xNode, yNode coordinate, you can take this approach

    Dictionary<int, double> indexedCoords = new Dictionary<int, double> { { 0, 0 }, { 1, 5 }, { 2, 10 }, .... };
    
    int CoordToIndex(double node, Dictionary<int, double> indexedCoords)
    {
        return indexedCoords.First(i => i.Value > node).Key;
    }