-1

I'm doing the following exercise of the Stroustrup's book:

Create a program to find all the prime numbers between 1 and 100. There is a classic method for doing this, called the “Sieve of Eratosthenes.” If you don’t know that method, get on the web and look it up. Write your program using this method.

I've understood the exercise but I'm having problems with how to implement it in C++.
This is the code so far:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){

    vector<int> nonprimi;
    vector<int> primi;

    for(int i=0; i <=100; i++){
        primi.push_back(i);
    }

    int numero = 2;

    for(int i = 0; i < 100; i++){
        numero += 2;
        numero == nonprimi[i];
    }

    numero = 3;
    for(int i = 0; i < 100; i++){
        numero += 3;
        numero == nonprimi[i];
    }

    numero = 5;
    for(int i = 0; i < 100; i++){
        numero += 5;
        numero == nonprimi[i];
   }

   numero = 7;
   for(int i = 0; i < 100; i++){
       numero += 7;
       numero == nonprimi[i];
   }

   for(int i = 0; i < nonprimi.size(); i++){
       if(primi[i] != nonprimi[i])
       cout << "\n" << primi[i] << "\n";
   }

return 0;
}

Could you provide me with some advice that will help me implement the algorithm successfully?

Note: Probably I should read the chapter again .

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Andrea Garau
  • 69
  • 2
  • 5
  • 1
    You have to tell us what the problem is in order for us to figure out where it is; is it a compiler error message, do you get the wrong result, what's wrong? – R_Kapp Oct 27 '15 at 19:27
  • The program starts but the output is absent. – Andrea Garau Oct 27 '15 at 19:29
  • `numero == nonprimi[i]` this does nothing. – Neil Kirk Oct 27 '15 at 19:30
  • All of your accesses to `nonprimi` are undefined as `nonprimi` hasn't had any space allocated. Also, your use of `==` indicates that you don't know how that operator works. – R_Kapp Oct 27 '15 at 19:30
  • When you have repetitive code that differs only with a number, this is a hint you should use an array and loop. You can put 2, 3, 5, 7 in a static const array, and loop through that, performing the logic. – Neil Kirk Oct 27 '15 at 19:31
  • 1
    @NeilKirk: Righto; meant that no space has ever been allocated. Changed my comment to reflect as such. – R_Kapp Oct 27 '15 at 19:33
  • Sorry but i'm not understanding very well, in part for my bad english and in part because i'm at the start of c++. Can you tell me the right code to understand better how to do it? – Andrea Garau Oct 27 '15 at 19:41

2 Answers2

1

Unlike the primi vector, which you push back 101 times in the first loop and it acquires a size of 101, the nonprimi vector never gets pushed back so it stays at a size of zero. Then, in the next loops, when you try to index its elements, a "vector subscript out of range" exception is thrown.

Also, expressions like this numero == nonprimi[i]; are comparison statements and do not accomplish anything unless they are inside the parentheses of an if statement or while loop.

Consider this alternative:

The long if statement in the second for loop literally means, if the number from the first array (which has been populated with all integers from 0 to 99) is 2, 3, 5 or 7 or is not divisible by any of 2, 3, 5 or 7, then add it into the primi vector.

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
    vector<int> nonprimi;
    vector<int> primi;
    for (int i = 0; i < 100; i++)
    {
        nonprimi.push_back(i);
    }
    for (int i = 0; i < nonprimi.size(); i++) {
        if (((nonprimi[i] == 2) || (nonprimi[i] == 3) ||
             (nonprimi[i] == 5) || (nonprimi[i] == 7)) || 
             (nonprimi[i] % 2 != 0) && (nonprimi[i] % 3 != 0) && 
             (nonprimi[i] % 5 != 0) && (nonprimi[i] % 7 != 0)) {

            primi.push_back(i);
        }
    }           

    for (int i = 0; i < primi.size(); i++) {
        cout << primi[i] << " ";
    }

    // or in a more STL way
    // copy(primi.begin(), primi.end(), ostream_iterator<int>(cout, " "));

    // system("pause");
    return 0;
}
Ziezi
  • 6,375
  • 3
  • 39
  • 49
dspfnder
  • 1,135
  • 1
  • 8
  • 13
  • Right, primi will have a size of 101. Edited. – dspfnder Oct 27 '15 at 20:20
  • It will be better if you exchange `system("pause")` with `getchar()` or simply insert a break point on a dummy statement before exiting the program – Ziezi Oct 27 '15 at 20:59
  • I use code blocks now and it isn't required, the program ask to press a key even if i miss getchar() or system("PAUSE") – Andrea Garau Oct 27 '15 at 21:03
  • @simplicis veritatis That's why it's commented out, to indicate it's optional – dspfnder Oct 27 '15 at 21:07
  • I get that is optional , however, how good option it is? Check this: http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong – Ziezi Oct 27 '15 at 21:20
0

This is my answer, not sure if it is ideal:

#include <vector>
#include <iostream>
using namespace std;

int main()
{
    // Find the prime numbers from 2, 3, 4, 5, through 100
    // using Sieve of Eratosthenes
    
    vector <int> numbers;

    for (int i = 2; i <= 100; i++)
        numbers.push_back(i);

    int p = 0;  // index of "numbers"

    cout << "Prime numbers:\n";
    while (p < (numbers.size() - 1)) {
        int prime = numbers[p];

        for (int i = p + prime; i < numbers.size(); i += prime)
        {
            numbers[i] = -1;    // make all non-prime numbers less than zero
        }

        
        do {
            if (p < (numbers.size() - 1)) {
                p++;    // increase the index until you encounter a positive number
            }
            else
                break;
        } while (numbers[p] < 0);
        cout << prime << endl;
    }
    

}
baki
  • 41
  • 3