-4

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:

  1. Why is there a section of code outside int main at the top, is the executed after int main?
  2. How do these statements work (are they double conditions?) bool prime (vector<int> table, int number) and if (prime(table,next))

Thanks, Sean

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    Possible duplicate of [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – πάντα ῥεῖ Jul 14 '18 at 10:39
  • 3
    One could hardly better prove skipping chapters in a language textbook is a bad idea. Thank you. – too honest for this site Jul 14 '18 at 10:43
  • Just as @Olaf mentioned already - you should find the answer to your question in some preceding chapter to the one you currently read... SO is not intended to provide you tutorials or give you lessons! Anyway, to be more explicit on your questions: The "section before main" is a function definition and the second is the call of previously defined function. – Aconcagua Jul 14 '18 at 10:48
  • In my copy of the book, section 4.5.1 has an example of 1). Perhaps you should reread a few pages, and not just peek at the solutions. – Bo Persson Jul 14 '18 at 10:52

1 Answers1

-1

The things you are asking are quite fundamental to the language of C and C++. A read through the first 2-3 chapters of any good C++ textbook would answer these questions for you.

The example code defines 2 functions: prime and main.

  1. The code outside main is the definition of a prime function. It is defined (created) there for you to call later, in the main function.
  2. These are two separate things. The first thing you mention is the definition of the function prime, the second is the call to that function.
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Ole Thomsen Buus
  • 1,333
  • 1
  • 9
  • 24
  • @mountaincabbage There are no stupid question. SO is a site which has become a good Q&A resource. Which is why there is so much focus on keeping up the quality of what is written here - both in questions and answers. In an ideal sense, there should be 1 answer per 1 unknown concept. And it should all be well written. This is impossible to achieve on the Internet of course. My teasing remark at the end, was removed because it somehow makes my answer less "encyclopedic". So again: Please, get back to the text book :) – Ole Thomsen Buus Jul 14 '18 at 11:23
  • 1
    _@Ole_ _"... was removed because it somehow makes my answer less "encyclopedic" ..."_ No, i removed it because you started to sound _patronizing_, and I didn't DV your answer BTW. A bit less condescendence would also look better. – πάντα ῥεῖ Jul 14 '18 at 11:39
  • @πάνταῥεῖ Fair enough. I was an attempt at a light tone. That can be difficult to communication in pure text. – Ole Thomsen Buus Jul 14 '18 at 11:48