How to find certain target in jagged array with C#

35 Views Asked by At

So I set up jagged arrays and got the question from the user. Have finished setting up the target value based on the user's answer. But cannot show the boolean value whether the target value is in the jagged array when I used the loop. It only shows the "True" value whether the target is in the jagged array or not.

class HW03_04
{
    static void Main()
    {
        int[][] array =
        {
            new int[] {9, 6, 48, 15, 23},
            new int[] {7, 56, 23, 4, 62},
            new int[] {13, 25, 7, 56, 2},
            new int[] {84, 30, 5, 0, 17}
        };
        Console.Write("What is your target value? ");
        int targetValue = Convert.ToInt32(Console.ReadLine());
        bool answer = Convert.ToBoolean(targetValue);
        Console.WriteLine("{0}", answer);
    }
    static bool target(int[][] arrSearch, int targetValue)
    {
        int size = arrSearch.Length;
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < arrSearch[i].Length; j++)
            {
                if (arrSearch[i][j] == targetValue)
                {
                    return true;
                }
            }
        }
        return false;
    }
}

Please help fixing the loop to show proper boolean value.

1

There are 1 best solutions below

0
Ian Pilipski On

You need to call your function, change your line to:

bool answer = target(array, targetValue);