Note that there are some explanatory texts on larger screens.

plurals
  1. POAssuming an array of 'bool', can 'a[n] == (-1)' ever be true?
    text
    copied!<p>I have some code where I have two bool arrays <code>a[]</code> and <code>b[]</code>, same size <code>N</code>.</p> <p><code>it</code> is an iterator pointing to an index of <code>a</code>, possibly between <code>0</code> to <code>N-1</code></p> <pre><code>if( (a[*it] == 1 &amp;&amp; index&gt;*it) || (a[index]==b[index] &amp;&amp; a[index]==-1 &amp;&amp; index!=0)) { final = !final; } </code></pre> <p>Accourding to me, the second part of the if condition <code>|| (a[index]==b[index] &amp;&amp; a[index]==-1 &amp;&amp; index!=0)</code> can never be true, because <code>a[index]</code> will not be equal to <code>-1</code> for any <code>index</code> , it would be either <code>1</code> or <code>0</code>. .. so I thought I can remove that part, however the result changes, for some particular conditions (I dont know the conditions, it's an online judge type site and I dont know what their testcases are).</p> <p>I added another test condition just before this <code>if</code> part in my program,</p> <pre><code>if(a[index]==-1){ cout&lt;&lt;"its True"; } </code></pre> <p>but "its True" was never printed.</p> <p>Why could this happen?</p> <p>Edit : Adding actual code, the purpose of program is to handle queries of type</p> <p><code>set_a INDEX VALUE</code></p> <p><code>set_b INDEX VALUE</code></p> <p>which sets corresponding index(0..n-1) to value(0 or 1) and</p> <p><code>get_c index</code> which prints out <code>c[index]</code> where C=A+B</p> <p>note, <code>INDEX</code> is from LSB side, i am storing LSB of A and B in MSB of the array <code>a</code> and <code>b</code></p> <p>Code :</p> <pre><code>int main(int argc, char * argv[]) { int n, q; char c; bool a[100005] = {0}, b[100005] = {0}; VanEmdeBoasTree equals; cin&gt;&gt;n; cin&gt;&gt;q; for(int i=0;i&lt;n;i++){ cin&gt;&gt;c; if(c=='1') a[n-i-1] = true; } for(int i=0;i&lt;n;i++){ cin&gt;&gt;c; if(c=='1') b[n-i-1] = true; if(a[n-i-1] == b[n-i-1]){ equals.insert(n-i-1); } } string query; int index, val, lastval; for(int i=0;i&lt;q;i++){ cin&gt;&gt;query; cin&gt;&gt;index; if(query[4] == 'a'){ cin&gt;&gt;val; if(a[index] != val){ a[index] = val; if(val == b[index]){ equals.insert(index); }else{ equals.erase(index); } } }else if(query[4] == 'b'){ cin&gt;&gt;val; if(b[index] != val){ b[index] = val; if(a[index] == val){ equals.insert(index); }else{ equals.erase(index); } } }else if(query[4] == 'c'){ int final = 0; if(index &gt;n-1){ if(equals.size() &gt; 0){ int last = (*(equals.predecessor(n+1))); if(a[last] == 1){ final = 1; } } }else{ if(equals.size() &gt; 0 &amp;&amp; index&gt;0){ VanEmdeBoasTree::const_iterator it = equals.predecessor(index); final = (a[index] + b[index])%2; if( (a[*it] == 1 &amp;&amp; index&gt;*it) || (a[index]==b[index] &amp;&amp; a[index]==-1 &amp;&amp; index!=0)){ final = !final; } }else{ final = (a[index] + b[index])%2; } } cout&lt;&lt;final; } } return 0; } </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