### The boolean seems to be not following my if statement

i was working on sieve of eratosthenes, i set the bool to TRUE if its a possible prime the false if its not. i really wanted to know why it seems not to be following my if statement...
here is my code

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include int main() { int limit, num=2, ctr; cout << "input limit of primes: "; cin >> limit; bool mark[limit]; // true if possible prime // false if composite for(ctr=0;ctr<=limit;ctr++) // marks every number to N as possible prime { mark[ctr]=1; } for(num=2;num<=limit;num++) // start testing if prime { if(mark[num]=true) // only test further number if possible prime { cout << "\n" << num; for(ctr=num+1;ctr<=limit;ctr++) { if(ctr%num!=0) { cout << " " << ctr << "-prime "; mark[ctr]=true; // mark as possible prime } else if(ctr%num==0) { cout << " " << ctr << "-composite "; mark[ctr]=false; // mark as composite } } } // end if the number is prime } system("pause"); return 0; }``````

Last edited on
`if(mark[num]=true)`
Should be

`if(mark[num] == true)`

= is assignment, == is comparison.

Please always use code tags - the <> button on the right
 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include int main() { int limit, num=2, ctr; cout << "input limit of primes: "; cin >> limit; bool mark[limit]; // true if possible prime // false if composite for(ctr=0;ctr<=limit;ctr++) // marks every number to N as possible prime { mark[ctr]=1; } for(num=2;num<=limit;num++) // start testing if prime { if(mark[num]==true) // only test further number if possible prime { cout << "\n" << num; for(ctr=num+1;ctr<=limit;ctr++) { if(ctr%num!=0) { cout << " " << ctr << "-prime "; mark[ctr]=true; // mark as possible prime } else if(ctr%num==0) { cout << " " << ctr << "-composite "; mark[ctr]=false; // mark as composite } } } // end if the number is prime } system("pause"); return 0; }``````

it still prints out all numbers from 2 to N... but first it prints out all even numbers as false except 2 which is correct. but the rest, the BOOl changes everyafter cycle of loop...
Topic archived. No new replies allowed.