Mohanad Al Nuaimi Mohanad Al Nuaimi - 2 months ago 18
C# Question

C# visual studio: Remove entire row or column from Array, and how to show two dimensional array in the output?

i have 8x6 array and I need to delete first two rows and the last two columns and change it to 6x4

45 778 0 0 0 0
1531 87 0 0 0 0
1391 619 115 115 1446 674
1278 330 150 150 1351 403
1037 460 178 178 1124 547
695 454 202 202 794 553
911 119 197 197 1007 215
534 91 195 195 629 186


i need to change the above matrix to :

1391 619 115 115
1278 330 150 150
1037 460 178 178
695 454 202 202
911 119 197 197
534 91 195 195


also how can I display this matrix in the output ?? I tried
Console.WriteLine( myArray.GetUpperBound(0));
but didn't work!!

Answer

Only thing I can think of is create a new array and then using a double for loop read out of the one and into the other?

Will be interesting to see if anyone knows a better way to do this

The exact function to do this would be

private static int[,] CopyArray(int[,] array, int fromX, int fromY, int lengthX, int lengthY)
{
    int[,] result = new int[lengthX, lengthY];
    for (int x = 0; x < result.GetLength(0); x++)
    {
        for (int y = 0; y < result.GetLength(1); y++)
        {
            result[x, y] = array[x + fromX, y + fromY];
        }
    }
    return result;
}

A full example of this is posted to my GitHub account (http://go.beeming.net/2dyzJY6).