Answer to Question #168481 in C# for norelyn

Question #168481

Write a program using two-dimensional array that searches a number and display the number of time it appears on the list of 10 input values.


1
Expert's answer
2021-03-03T08:02:45-0500
// C# program to search an element in row-wise
// and column-wise sorted matrix
using System;
 
class GFG{
 
/* Searches the element x in mat[][]. If the 
element is found, then prints its position 
and returns true, otherwise prints "not found"
and returns false */  
static int search(int[,] mat, int n, int x)
{
    if (n == 0)
        return -1;
     
    // Traverse through the matrix
    for(int i = 0; i < n; i++) 
    {
        for(int j = 0; j < n; j++)
         
        // If the element is found
        if (mat[i,j] == x) 
        {
            Console.Write("Element found at (" + i +
                          ", " + j + ")\n");
            return 1;
        }
    }
    Console.Write(" Element not found");
    return 0;
}
 
// Driver code
static public void Main()
{
    int[,] mat = { { 10, 20, 30, 40 },
                   { 15, 25, 35, 45 },
                   { 27, 29, 37, 48 },
                   { 32, 33, 39, 50 } };
                    
    search(mat, 4, 29);
}
}
 
// This code is contributed by avanitrachhadiya2155

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS