Problem printing vector

Apr 5, 2017 at 10:53pm
Help fixing the code to print vector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "std_lib_facilities.h"
vector <int>p;
bool prime(int number)
{
    for (int i = 2; i < sqrt(number); i++)
    {
        if (number%i == 0)
        {
            return false;
        }
    }
    return true;
}

int main(){
    int x;
    cout << "Enter a value: ";
    while(cin >> x)
    {
        p.push_back(x);
    }
        for (int i = 2; i<p.size(); i++)
        {
                cout << p[i] << " ";
    }
    cout << endl;
    return 0;
}
Last edited on Apr 5, 2017 at 11:05pm
Apr 5, 2017 at 11:55pm
prime() is never called within main() - so what is this program trying to do? check if a number is prime or print a vector? not clear
Apr 6, 2017 at 2:43am
Create a program to find all the prime numbers between 2 and max. One way to do this is to is
as follows. Start with a vector called primes and push back 2 as it is prime. Then use a for loop
from 3 to max. With each iteration do the following:
1. Set a flag isprime to true at the beginning of each iteration.
2. Use a for loop over the primes to see if the current value is divisible by any of the values in
the vector. If so, set the flag to false.
3. Depending on the flag, push back the current value into the vector of primes.
Apr 6, 2017 at 8:50am
Apr 6, 2017 at 12:08pm
Hi, CoderGirlV.
Why don't you follow the given requirements step by step? They already take you to the simplest solution.



What's follow is an example of code which follows the instructions.
Do not read if you want to go your own way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Create a program to find all the prime numbers between 2 and max. 

// One way to do this is to is as follows. 
// Start with a vector called primes and push back 2 as it is prime. 
// Then use a for loop from 3 to max. 
// With each iteration do the following:
//    1. Set a flag isprime to true at the beginning of each iteration.
//    2. Use a for loop over the primes to see if the current value 
//       is divisible by any of the values in the vector. 
//       If so, set the flag to false.
//    3. Depending on the flag, push back the current value into the vector 
//       of primes. 
#include <iostream>
#include <vector>

int main()
{
    // "...Start with a vector called primes..."
    std::vector<int> primes;
    // "...and push back 2 as it is prime..."
    primes.push_back(2);

    // "...all the prime numbers between 2 and max..."
    std::cout << "\nPlease, specify the upper bound: ";
    int max;
    std::cin >> max;

    // "...Then use a for loop from 3 to max..."
    for(int curr_value=3; curr_value<max; curr_value++) {
        // "...1. Set a flag isprime to true at the beginning 
        ///  of each iteration..."
        bool isprime = true;

        //  "...2. Use a for loop over the primes..."
        for(int i=0; i<primes.size(); i++) {
            // "...to see if the current value is divisible by any 
            //   of the values in the vector..."
            if(curr_value % primes.at(i) == 0) {
                // "...If so, set the flag to false..."
                isprime = false;
            }
        }

        // "...3. Depending on the flag, push back the current value 
        //   into the vector of primes..."
        if(isprime) { // <-- if(isprime == true)
            primes.push_back(curr_value);
        }
    }

    for(int i=0; i<primes.size(); i++) {
        std::cout << primes.at(i) << ' ';
    }
    std::cout << '\n';

    return 0;
}

Topic archived. No new replies allowed.