Asap

Write a program that finds prime numbers using an a
lgorithm that works as follows. Assume we wish
to determine which numbers between 2 and 100 are pr
ime. Fill an array with 100 1s: initially every
number between 2 and 100 is considered prime. Consi
der 2: it is prime, so cross out every second
number (4, 6, 8, etc.) (set to 0 the corresponding
array entry). Then come back to the beginning of th
e
array and move (from 2) on to the next prime number
, the next one not crossed out: 3. Cross out every
third number: 6, 9, 12, etc. Continue this process,
advancing to the next prime number and crossing
out all its multiples until you exceed 100. At this
point, every number not crossed out is prime. Your
program should print all prime numbers between 2 an
N, where N is a symbolic constant. Make N
equal to 100.
and this is what i tried so far but it gives me infinite loop idk why
i cout it just to make sure its working right, anyone can help?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 #include<iostream>
using namespace std;

int main() {

bool prime[100];

    for (int i=2;i<100;i++)
        {prime[i]=1;}

        for(int i=2;i<100;i+2)
            {prime[i]=0;
            cout<<prime[i];}
return 0;
}
Fill an array with 100 1s

I don't understand why you would want to fill an array with 100 1s. Shouldn't it be "Fill an array from 1 to 100?
you've got i+2 in line 11, that has no effect, you are simply doing a sum but not assigning to anything.
replace by i += 2


@integralfx: it's just a flag, at the end of the algorithm the ones that have 1 (true) are prime numbers
Topic archived. No new replies allowed.