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
#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
}
elseif(ctr%num==0)
{ cout << " " << ctr << "-composite ";
mark[ctr]=false; // mark as composite
}
}
} // end if the number is prime
}
system("pause");
return 0;
}
#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
}
elseif(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...