Recursion, Searching, Sorting, and Pointers/assign_1.c
int searchMatrix (int V, int *P, unsigned int rowsize, unsigned int colsize, int A[][colsize]){
    
    int isFound = NOTFOUND;
    int minOffset = *P; //offset for the location of min num in A, the 0th element of array P
    int maxOffset = *(P + 1);   //offset for the location of max num in A, the 1st element of array P
    int middleOffset = (maxOffset + minOffset) / 2; //offset for the location of middle num in A
    
    int *ptr = &A[0][0];    //points to the beginning of the matrix (A)
            
    //computes the actual value for the min, max, and middle
    int minValue = *(ptr + minOffset);
    int maxValue = *(ptr + maxOffset);
    int middleValue = *(ptr + middleOffset);
    
    //base case, set the position of the num in the array P
    if (V == middleValue){
        P[0] = middleOffset / colsize;    //row of searched num
        P[1] = middleOffset % colsize;    //col of searched num
        return FOUND;
    }
        
    //if num is not found, set the default location in the array P
    if (((middleValue == minValue) && (middleValue == maxValue) && V != middleValue)){
        P[0] = NOTFOUND;
        P[1] = NOTFOUND;
        return NOTFOUND;
    }
        
    if (V > middleValue){   //if num is greater than the middle val of the array, search the upper half
        *P = middleOffset - 1;
        //no need to re-assign the 1st element of arr P as it still is the max
        isFound = searchMatrix(V, P, rowsize, colsize, A);
    }
    else if (V < middleValue){      //if num is lesser than the middle val of the array, search the lower half
        *P = minOffset;
        *(P + 1) = middleOffset + 1;
        isFound = searchMatrix(V, P, rowsize, colsize, A);
    }     
    
    return isFound;
}
/*
    searchMatrix: searches a sorted matrix for a given value using recursive binary search
                  it also computes the position of the value in the matrix
    input: the value to be searched (V)
           the size of the matrix (rowsize, colsize)
           the array (A[][])
           a pointer to the potential position of the value
    output: 1 if the value is found, otherwise -1
*/
searches a sorted matrix for a given value using recursive binary search
it also computes the position of the value in the matrix
input: the value to be searched (V)
the size of the matrix (rowsize, colsize)
the array (A[][])
a pointer to the potential position of the value
output: 1 if the value is found, otherwise -1
searches a sorted matrix for a given value using recursive binary search it also computes the position of the value in the matrix input : the value to be searched ( V ) the size of the matrix ( rowsize colsize ) the array ( A [] [] ) a pointer to the potential position of the value output : 1 if the value is found otherwise - 1
int searchMatrix (int V, int *P, unsigned int rowsize, unsigned int colsize, int A[][colsize]){
    int isFound = NOTFOUND;
    int minOffset = *P; 
    int maxOffset = *(P + 1);   
    int middleOffset = (maxOffset + minOffset) / 2; 
    int *ptr = &A[0][0];    
    int minValue = *(ptr + minOffset);
    int maxValue = *(ptr + maxOffset);
    int middleValue = *(ptr + middleOffset);
    if (V == middleValue){
        P[0] = middleOffset / colsize;    
        P[1] = middleOffset % colsize;    
        return FOUND;
    }
    if (((middleValue == minValue) && (middleValue == maxValue) && V != middleValue)){
        P[0] = NOTFOUND;
        P[1] = NOTFOUND;
        return NOTFOUND;
    }
    if (V > middleValue){   
        *P = middleOffset - 1;
        isFound = searchMatrix(V, P, rowsize, colsize, A);
    }
    else if (V < middleValue){      
        *P = minOffset;
        *(P + 1) = middleOffset + 1;
        isFound = searchMatrix(V, P, rowsize, colsize, A);
    }     
    return isFound;
}
int searchMatrix ( int V , int * P , unsigned int rowsize , unsigned int colsize , int A [ ] [ colsize ] ) { int isFound = NOTFOUND ; int minOffset = * P ; int maxOffset = * ( P + 1 ) ; int middleOffset = ( maxOffset + minOffset ) / 2 ; int * ptr = & A [ 0 ] [ 0 ] ; int minValue = * ( ptr + minOffset ) ; int maxValue = * ( ptr + maxOffset ) ; int middleValue = * ( ptr + middleOffset ) ; if ( V == middleValue ) { P [ 0 ] = middleOffset / colsize ; P [ 1 ] = middleOffset % colsize ; return FOUND ; } if ( ( ( middleValue == minValue ) && ( middleValue == maxValue ) && V != middleValue ) ) { P [ 0 ] = NOTFOUND ; P [ 1 ] = NOTFOUND ; return NOTFOUND ; } if ( V > middleValue ) { * P = middleOffset - 1 ; isFound = searchMatrix ( V , P , rowsize , colsize , A ) ; } else if ( V < middleValue ) { * P = minOffset ; * ( P + 1 ) = middleOffset + 1 ; isFound = searchMatrix ( V , P , rowsize , colsize , A ) ; } return isFound ; }
searchMatrix: searches a sorted matrix for a given value using recursive binary search
it also computes the position of the value in the matrix
input: the value to be searched (V)
the size of the matrix (rowsize, colsize)
the array (A[][])
a pointer to the potential position of the value
output: 1 if the value is found, otherwise -1
searchMatrix : searches a sorted matrix for a given value using recursive binary search it also computes the position of the value in the matrix input : the value to be searched ( V ) the size of the matrix ( rowsize colsize ) the array ( A [] [] ) a pointer to the potential position of the value output : 1 if the value is found otherwise - 1
//offset for the location of min num in A, the 0th element of array P //offset for the location of max num in A, the 1st element of array P //offset for the location of middle num in A //points to the beginning of the matrix (A) //computes the actual value for the min, max, and middle //base case, set the position of the num in the array P //row of searched num //col of searched num //if num is not found, set the default location in the array P //if num is greater than the middle val of the array, search the upper half //no need to re-assign the 1st element of arr P as it still is the max //if num is lesser than the middle val of the array, search the lower half