Question

Given a matrix of size NxM sorted in descending order and value V, write a function...

Given a matrix of size NxM sorted in descending order and value V, write a function which returns -1 if value V is not in the matrix, else it returns 1 if V is in the matrix. The function also computes the position of V in the matrix, that is P= [R, C], where R is the row number of V and C is the column number of V in the matrix, if V is in the matrix (if V is not in the matrix then P=[-1,-1]); P is a 1D array of size 2. You must use the recursive Binary Search method.

int SearchMatrix(int A[ ] [ ], int V, int *P, unsigned int rowsize, unsigned int colsize);

NOTE:

#define COL 20

#define ROW 20

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>


int binarySearch(int Mat[4][4],int row,int first,int last,int target)
{
int middle= (first+last)/2;
  
if(first>last)
return -1;

if(Mat[row][middle]==target)
return middle;
  
if(Mat[row][middle]>target)
return binarySearch(Mat,row,first,middle-1,target);
  
return binarySearch(Mat,row,middle+1,last,target);
}
int binarySearchRow(int Mat[4][4],int first,int last,int size,int target)
{
if(first>last)
return -1;

int middle= (first+last)/2;
if(Mat[middle][0]<= target && Mat[middle][size-1]>=target)
return middle;
  
if(Mat[middle][0]>target)
return binarySearchRow(Mat,first,middle-1,size,target);
  
return binarySearchRow(Mat,middle+1,last,size,target);
}
int search(int Mat[4][4],int target,int *p,int x,int y)
{
x= binarySearchRow(Mat,0,4,4,target);

if(x==-1)

return 0;
  
y = binarySearch(Mat,x,0,4,target);
if(y==-1)
return 0;
  
*p=x;
p++;
*p=y;
return 1;
}

int main()
{
int mat[4][4] = { {10, 4, 4, 40},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50},
};
int x,y;
int p[2]={-1,-1};
printf("Element 37 is found at\n");

search(mat,37,p ,x,y);
printf("%d %d\n",p[0],p[1]);
return 0;
}

================================================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ gcc bstre.c
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Element 37 is found at
2 2

Add a comment
Know the answer?
Add Answer to:
Given a matrix of size NxM sorted in descending order and value V, write a function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std;...

    (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std; // sorted array in descending order int list[SIZE] (23, 19, 17, 13, 11, 7, 5, 3, 1, 0); // recursively binary search the array list for key // return the index to list if key is found. else return -1 int recBinarySearch (int key) // Please implement the recursive function.. Please implement the C++ function recBinarySearch that recursively binary searches the value key in...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • Write a function that gets an array as a parameter, sorts it in a descending fashion...

    Write a function that gets an array as a parameter, sorts it in a descending fashion and returns the number of elements that did not change position in the sort (they were originally in the right position). int sortDESC(int* myArray, int size) if array = { 2, 3, 1, 0, -1} after calling the function the array should become {-1, 0, 1, 2 , 3 } and the function should return 1 (only one element has not changed place in...

  • 1. Write a recursive function that computes the sum of all numbers from 1 to n,...

    1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Here is the method header: public static int sum (int n){...} 2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters. Here is the method header: public static int minValue (int [] data, int size){...} 3. Write a recursive function that reverses the...

  • using c++ Write a function that returns true if a given integer array is sorted(in ascending...

    using c++ Write a function that returns true if a given integer array is sorted(in ascending order) and false if it is not. The function should perform no more than "size" comparisons. bool isSorted(int A[], int size);

  • 3. Write the function find sorted elt whose input is a vector sorted in increasing order...

    3. Write the function find sorted elt whose input is a vector sorted in increasing order and an element x. The output is 0 if the element x does not occur in v, 1 if the element x does occur in v. Below is the algorithm you should implement, known as the binary search algorithm. Note that the code below returns the index, but we want to return true or false, not the index, so adjust your code accordingly. Algorithm....

  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

  • Write a C function with signature btbstree (int' post, int size) that gets the post-order traversal...

    Write a C function with signature btbstree (int' post, int size) that gets the post-order traversal of a binary search tree in the form of an int array, reconstructs the tree and returns a pointer to the tree root as output (The second input parameter size specifies the number of elements in the tree). Note: Type bt is defined in the following way: typedef struct btreenode { struct btreenode *left; int data; struct btreenode *right; |}bt;

  • Given the contents of a sorted array of size 6, shown below, show which indices of...

    Given the contents of a sorted array of size 6, shown below, show which indices of the array will be visited, and in what order, if the algorithm BinarySearch was to be performed on this array for the number 4. int theArray[] = {1, 2, 3, 6, 8, 9}; Following are example question & answer: Given the contents of a sorted array of size 7, shown below, show which indices of the array will be visited, and in what order,...

  • Given a sorted (in ascending order) integer array nums of n elements and a target value,...

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a method to perform a binary search for a target value in nums. If target exists, then return its index, otherwise return -1. Analyze the running time, T(n). public int search(int[] nums, int target) { // YOUR CODE GOES HERE } // end search

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT