Filling vector with primes in a certain way

Viewed 175

I was asked to fill a given vector with prime numbers, I can't use any other vectors, arrays or collections. I've come up with something like this, but it doesn't work properly and I can't figure out why.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool isPrime(int n){                           
    if (n<=1){return false;}
    for (int i = 2; i < n; i++){
        if (n % i == 0){
            return false;
        }
    }
    return true;}

int fillWithPrimesVec(vector<int>& vec){
    for (int i = 0; i < vec.size(); i++){
        for (int j = 0; j<INT_MAX; j++){
            if (isPrime(j)){
                vec.push_back(j);}
            else{continue;}
            }
        }

    return vec[0];
}

int main(){

int c[15];
size_t szc = sizeof(c)/sizeof(*c);
vector<int> d(szc,0);

cout << fillWithPrimesVec(d);

return 0;
}

Could anyone please help me?

0 Answers
Related