Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get rid of fixed sentinel value (-32767)?
    text
    copied!<p>Below is the program I wrote to find sum of a subarray from given array, however somehow I am not getting how can I get rid of the sentinel value (-32767 in this case)? and how can I optimise it? and how can I keep track of range of max subarray?</p> <pre><code>#define EVALUE -32767 using namespace std; int findMaxSubArray(vector&lt;int&gt;,int low,int high); int findMaxSubArray_Mid(vector&lt;int&gt;,int low,int high); int main() { vector&lt;int&gt; v; int j=0; cout &lt;&lt; "Enter array values(-32767 to end): "; while(1) { cin &gt;&gt; j; if (EVALUE==j) break; v.push_back(j); } if(v.size()!=0) cout &lt;&lt; "Max sum is: " &lt;&lt; findMaxSubArray(v,0,v.size()-1) &lt;&lt; "\n"; else cout &lt;&lt; "No array elements entered, exiting...\n"; system("pause"); return 0; } int findMaxSubArray(vector&lt;int&gt; v, int low, int high) { if(low==high) return v[low]; int max_mid_sum=findMaxSubArray_Mid(v,low,high); int max_left_sum=findMaxSubArray(v,low,(low+high)/2); int max_right_sum=findMaxSubArray(v,(low+high)/2+1,high); if (max_mid_sum&gt;max_left_sum) return (max_mid_sum&gt;max_right_sum?max_mid_sum:max_right_sum); else return(max_left_sum&gt;max_right_sum?max_left_sum:max_right_sum); } int findMaxSubArray_Mid(vector&lt;int&gt; v,int low,int high) { int mid=high/2; int max_left_sum=0; int max_right_sum=0; int sum=0; for(int i=mid;i&gt;=low;--i) { sum+=v[i]; if(sum&gt;max_left_sum) { max_left_sum=sum; } } sum=0; for(int i=mid+1;i&lt;=high;++i) { sum+=v[i]; if(sum&gt;max_right_sum) { max_right_sum=sum; } } return (max_right_sum+max_left_sum); } </code></pre>
 

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