Note that there are some explanatory texts on larger screens.

plurals
  1. POBinary searching a 2D array to find all the indexes of the key
    primarykey
    data
    text
    <p>I am trying to binary search a 2D array which will give me the position of every key in the array, my code is</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; using namespace std; vector&lt;pair&lt;int,int&gt; &gt; up; vector&lt;pair&lt;int,int&gt; &gt; down; void binsearchd(int **A,int row,int low,int high,int key) { int mid=(low+high)/2; if(low==high&amp;&amp;A[row][mid]!=key)//!(A[row][mid]&lt;=key&amp;&amp;A[row][mid-1]&lt;=key&amp;&amp;A[row-1][mid] &lt;=key&amp;&amp;A[row+1][mid]&gt;key&amp;&amp;A[row][mid+1]&gt;key)) return; if(A[row][mid]==key)//&lt;=key&amp;&amp;A[row][mid-1]&lt;=key&amp;&amp;A[row-1][mid]&lt;=key&amp;&amp;A[row+1][mid]&gt;key&amp;&amp;A[row][mid+1]&gt;key) { down.push_back({row,mid}); return; } if(A[row][mid]&gt;key) binsearchd(A,row,low,mid,key); else if(A[row][mid]&lt;key) binsearchd(A,row,mid+1,high,key); else { binsearchd(A,row,low,mid,key); binsearchd(A,row,mid+1,high,key); } return; } void searchd(int **A,int lowi,int lowj,int highi,int highj,int key) { if(lowi==highi) { binsearchd(A,lowi,lowj,highj,key); return; } int midi=(lowi+highi)/2,midj=(lowj+highj)/2; if(A[midi][midj]==key)//&lt;=key&amp;&amp;A[midi][midj-1]&lt;=key&amp;&amp;A[midi-1][midj]&lt;=key&amp;&amp;A[midi+1][midj]&gt;key&amp;&amp;A[midi][midj+1]&gt;key) down.push_back({midi,midj}); if(A[midi][midj]&gt;key) { searchd(A,lowi,lowj,midi,highj,key); searchd(A,midi+1,lowj,highi,midj,key); } else if(A[midi][midj]&lt;key) { searchd(A,midi+1,lowj,highi,highj,key); searchd(A,lowi,midj+1,midi+1,highj,key); } else { searchd(A,lowi,lowj,midi,highj,key); searchd(A,midi+1,lowj,highi,highj,key); binsearchd(A,midi,lowj,highj,key); } return; } int main() { int row,col; while(cin&gt;&gt;row&gt;&gt;col&amp;&amp;row){ int **A=new int*[row]; for(int i=0;i&lt;row;++i) { A[i]=new int[col]; for(int j=0;j&lt;col;++j) cin&gt;&gt;A[i][j]; } int query; cin&gt;&gt;query; while(query--) { int d; cin&gt;&gt;d; //searchu(A,1,1,row+1,col+1,u); searchd(A,0,0,row,col,d); int max=-999999999; //for(vector&lt;pair&lt;int,int&gt; &gt;::iterator itr=up.begin();itr!=up.end();++itr) //{ for(vector&lt;pair&lt;int,int&gt; &gt;::iterator itr2=down.begin();itr2!=down.end();++itr2) { cout&lt;&lt;itr2-&gt;first&lt;&lt;" "&lt;&lt;itr2-&gt;second&lt;&lt;endl; } up.resize(0); down.resize(0); } for(int i=0;i&lt;row+1;++i) delete A[i]; delete A; } } </code></pre> <p>the problem is it gives segmentation fault on array</p> <pre><code>4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 </code></pre> <p>key=4 and also in most of the time, I tried debugging found the reason of seg fault was binary searching a row 4, which is greater than the no. of row.. but I cant find out how that row can appear because my code divides the row in exactly 2 halves each time and thats how it shoudnt be able to cross the boundary.. Any hint??</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload