Found this answer to Ch4Ex15 of Stroustrups beginner book, the question is to find the first n amount of primes:
#include "std_lib_facilities.h"
bool prime (vector<int> table, int number) {
for (int i = 0; i < table.size(); ++i)
if (number%table[i] == 0) return false;
return true;
}
int main () {
int count, next;
cout << "Input the number of primes\n";
cin >> count;
vector<int> table;
next = 2;
while (table.size() < count) {
if (prime(table,next)) table.push_back(next);
++next;
}
for (int n = 0; n < table.size(); ++n)
cout << table[n] << " ";
cout << endl;
// keep_window_open();
return 0;
}
Two things I'm struggling to understand:
- Why is there a section of code outside int main at the top, is the executed after int main?
- How do these statements work (are they double conditions?)
bool prime (vector<int> table, int number)
andif (prime(table,next))
Thanks, Sean