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
#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
Given a matrix of size NxM sorted in descending order and value V, write a function...
(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...
Write a program in C++ that, given a seven-digit number, writes to a file every possible seven-letter corresponding to that number. There are 2187 (3 to the seventh power) such words. Include the numbers 0 and 1; just embed them in the words as a 0 and a 1. Example: one possibility for 5701679 would be: LR01OPW. Assume the user correctly enters only 7 digits (no ‘-‘). Read the user input into a char array. Use a recursive function to...
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...
How can I make this into a recursive function (C++)?? //Write a function that, given positive integers 1 <= n <= 10^4 and 1 <= s <= n as input, returns the sum of the last s elements in the sequence from 1 to n (inclusive). unsigned long int suffix_sum(unsigned int n, unsigned int s){ unsigned long int sum = 0,r=(n-s)+1; while (r<=n){ sum = sum + r; r++; } return sum; return 0; }
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....
Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...
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, 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...
CONVERT CODE TO JAVA
// A C++ program to Print all elements in sorted order from row
and
// column wise sorted matrix
#include<iostream>
#include<climits>
using namespace std;
#define INF INT_MAX
#define N 4
// A utility function to youngify a Young Tableau. This is
different
// from standard youngify. It assumes that the value at
mat[0][0] is
// infinite.
void youngify(int mat[][N], int i, int j)
{
// Find the values at down and right
sides of...
Language is C++ Basic principles and theory of structured programming in C++ by implementing the class: Matrix Use these principles and theory to help build and manipulate instances of the class (objects), call member functions Matrix class implementation from the main function file and, in addition, the Matrix class must have its interface(Matrix.h) in a separate file from its implementation (Matrix.cpp). The code in the following files: matrix.h matrix.cpp matrixDriver.h Please use these file names exactly. Summary Create three files...