The problem is likely here:
for (int i = 0; i < list.size(); ++i){
if (list[i] == 1){
int c;
c = i;
while (c < list.size()){
c += i;
list[c] = 0; //problem is here. On the last loops c+=i is too big
}
}
}
The reason is because in the outermost for loop you end up with i == list.size() - 1
. Now if c > 1
you will get c + i > list.size()
then you try to access list[c+i]
which is an index greater than the size of the list
of primes. This is why when you run it for 1 it works but fails for any other larger number.
As for the compiler warning, that's because size()
returns an unsigned size_t
and your loop variable i
is a signed int. When you then compare these this is what the compiler is complaining about. Change the loop to this:
for (size_t i = 0; i < list.size(); ++i){
and your compiler warning will go away.