none
How do I search through an array and store results in this way? RRS feed

  • Question

  • That is an example of an array I want to search through, but I don't know how to do it. The columns I refer to by a letter skipping the first, so it's A, B, C, and D, those are the columns with numbers in them. The result of the search would be to find matching numbers so if I searched the above array the results would be displayed like this:

    4:

    A(A)

    A(D)

    D(A)

    9:

    A(B)

    E(D)

    5:

    C(A)

    D(B)

    As you can see, the results are shown with the actual number at the top, and below that the array coordinates of the array elements' that match are shown below the number. Could someone please show me how this is done, if so you don't need to show an example exactly like what I've shown. You may have noticed two bold statements, I just want to know how to do those two things so feel free to show me in any way that is easiest for you.

    Thanks

    Thursday, March 26, 2020 1:48 PM

Answers


  • That is an example of an array I want to search through, but I don't know how to do it. The columns I refer to by a letter skipping the first, so it's A, B, C, and D, those are the columns with numbers in them. The result of the search would be to find matching numbers so if I searched the above array the results would be displayed like this:

    4:

    A(A)

    A(D)

    D(A)


    Study this for one way:

    static void Main(string[] args)
    {
        int[,] arry = 
            {{4,9,5,4},{2,6,7,8},{5,1,2,3 },{4,5,3,5},{8,6,1,9}};        
        int target = 4;
        Console.WriteLine("{0}:", target);
        for (int x = 0; x < 5; ++x)
        {
            for (int y = 0; y < 4; ++y)
            {
                if(arry[x,y] == target)
                {
                    Console.WriteLine("{0}({1})",(char)(x+'A'), (char)(y+'A'));
                }
            }
        }
    }

    Output:

    4:
    A(A)
    A(D)
    D(A)

    - Wayne


    Thursday, March 26, 2020 3:10 PM

All replies


  • That is an example of an array I want to search through, but I don't know how to do it. The columns I refer to by a letter skipping the first, so it's A, B, C, and D, those are the columns with numbers in them. The result of the search would be to find matching numbers so if I searched the above array the results would be displayed like this:

    4:

    A(A)

    A(D)

    D(A)


    Study this for one way:

    static void Main(string[] args)
    {
        int[,] arry = 
            {{4,9,5,4},{2,6,7,8},{5,1,2,3 },{4,5,3,5},{8,6,1,9}};        
        int target = 4;
        Console.WriteLine("{0}:", target);
        for (int x = 0; x < 5; ++x)
        {
            for (int y = 0; y < 4; ++y)
            {
                if(arry[x,y] == target)
                {
                    Console.WriteLine("{0}({1})",(char)(x+'A'), (char)(y+'A'));
                }
            }
        }
    }

    Output:

    4:
    A(A)
    A(D)
    D(A)

    - Wayne


    Thursday, March 26, 2020 3:10 PM
  • Or you can use an array of co-ordinate letters:

    static void Main(string[] args)
    {
        char[] letters = {'A', 'B', 'C', 'D', 'E'};
        int[,] arry =
            {{4,9,5,4},{2,6,7,8},{5,1,2,3 },{4,5,3,5},{8,6,1,9}};
    
        int target = 4;
        Console.WriteLine("{0}:", target);
        for (int x= 0; x < 5; ++x)
        {
            for (int y = 0; y < 4; ++y)
            {
                if(arry[x,y] == target)
                {
                    Console.WriteLine("{0}({1})", letters[x], letters[y]);
                }
            }
        }
    }
    

    - Wayne

    Thursday, March 26, 2020 3:45 PM
  • Hi Integrater,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 31, 2020 9:29 AM