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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream.h>
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream.h>
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.