Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Int search( int *p, int n, int m, int k, int *r, int *c){ Int i; Int index = -1;

ID: 3700773 • Letter: I

Question

Int search( int *p, int n, int m, int k, int *r, int *c){ Int i; Int index = -1; For( i = 0; i < m*n; i++){ If ( p[i] ==k) { *r = *c = Index = } } Return } 1. Complete the implementation of the following C function that searches a 2D array for a key. The parameter p points to a 2D int array of dimensions m by n:int a[m][n]. The parameter k is the key value to search. The pointers r and c will point to the result of a successful search: the row and column indices of the element in the 2D array whose value is equal to the key. The function returns the linear index of the found element if the search is successful. It returns -1 if the key is not found in the array. Do not create additional variables and do not remove any existing code.

Explanation / Answer

// include <math.h>

// this function is returning exact index

// it is working perfectly

// but some problem with row and column

int search(int *p,int n,int m,int k,int *r,int *c)

{

int i;

int index = -1;

for( i = 0; i < m*n; i++){

if ( p[i] == k) {

// I tried a lot here eventhough we store row and column address in r and c

// it can't be accessble in main function or parent function which called it.

// the *r,*c in the parent function and *r,*c in this search functions are reffering to different address locations

// and are local

//*r = &p[(int)ceil((i+1)/n)];

//*c = &p[(int)(i+1)%n];

index =i;

return index;

}

}

// if no match then index value will as it is in the begining i.e, -1

// so -1 will be returned

return index;

}

//Thank you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote