Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

static void Main(string[] args)

{
while (true)
{
Console.Write("Enter number of rows: ");
int rows = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number of columns: ");
int cols = Convert.ToInt32(Console.ReadLine());

int[,] array = generateTwoDimArray(rows, cols);


print2DArray(array);

Console.Write("Enter a value: ");


int val = Convert.ToInt32(Console.ReadLine());

string result = Search(val, array);


Console.WriteLine(result);

Console.Write("\nDo you want to try again?[Y/N]: ");


char input = Console.ReadKey().KeyChar;

if (input != 'Y' || input != 'y')


break;

Console.WriteLine();
Console.WriteLine();
}

public static int[,] generateTwoDimArray(int rows, int cols)


{
Random ran = new Random();
int[,] twodim = new int[rows, cols];

for (int r = 0; r < rows; r++)


{
for (int c = 0; c < cols; c++)
{
twodim[r, c] = ran.Next(10, 50);
}
}

return twodim;
}

public static void print2DArray(int[,] arr)


{
for (int r = 0; r < arr.GetLength(0); r++)
{
for (int c = 0; c < arr.GetLength(1); c++)
{
Console.Write("[{0}]", arr[r, c]);
}
Console.WriteLine();
}
}

public static string Search(int value, int[,] arr)


{
int num = 0;
for (int r = 0; r < arr.GetLength(0); r++)
{
for (int c = 0; c < arr.GetLength(1); c++)
{
if (value == arr[r, c])
{
num++;
}
}
}
if (num == 0)
{
return "Not Found";
}
else if (num == 1)
{
return "value occurred 1 time";
}
return value + " occurred " + num + " times";
}
}

You might also like