Why does this loop break at the first iteration?

DomeWTF

I'm learning C++ and am trying to create a program that iterates from 1 to 100 and discovers all the prime numbers, but basically it stops at the fist iteration in the multiply function.

the

cout << "Base: " << base << " To: " << to << " Check: " << check << " tmp: " << tmp << endl;

i've added it to check where it stopped

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

using namespace std;

int multiply(int base, int to, int check, vector<int> &all_numbers) {

    int tmp;

    for(int i = 1; i <= to; i++) {

        tmp = base * i;

        if(tmp != check) {

            cout << "Base: " << base << " To: " << to << " Check: " << check << " tmp: " << tmp << endl;

            if( (tmp % check) == 0 ) {

                if( find( all_numbers.begin(), all_numbers.end(), tmp ) != all_numbers.end() ) {

                    all_numbers.erase( remove( all_numbers.begin(), all_numbers.end(), tmp ), all_numbers.end() );

                }

            }

        }

    }

    return 0;

}

int main(int argc, char** argv) {

    int top = 100;
    vector<int> primes;

    for(int i = 1; i <= top; i++) {

        primes.push_back(i);

    }

    for(int i = 0; i < top; i++) {

        multiply(1, top, i, primes);

    }

    for(vector<int>::iterator it = primes.begin(); it != primes.end(); ++it) {

        cout << *it << " - ";

    }

    cout << endl;

    return 0;
}
JosEduSol
Why does this loop break at the first iteration?

You are dividing by zero in:

if( (tmp % check) == 0)

Consider this way:

bool isPrime(int n) {  
   if (n == 1 || n == 2)
      return false;   

   for (int i=2; i <= n/2; i++)
        if((n % i) == 0)
            return false;

   return true;
}

int main(int argc, char** argv) {
    int top = 100;
    vector<int> primes;

    for(int i = 1; i < top; i++)
        if (isPrime(i))
             primes.push_back(i);

    for(vector<int>::iterator it = primes.begin(); it != primes.end(); ++it)
        cout << *it << " - ";

    cout << endl;
    return 0;
}

Output:

3 - 5 - 7 - 11 - 13 - 17 - 19 - 23 - 29 - 31 - 37 - 41 - 43 - 47 - 53 - 59 - 61 - 67 - 71 - 73 - 79 - 83 - 89 - 97 - 

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does this for loop run into an ArrayIndexOtOfBounds error? (Java)

분류에서Dev

React Native - Why does displaying components through iteration give the id prop as the last iteration number to all of the components?

분류에서Dev

Why this Windows message loop does not handle shorcut/tab keys?

분류에서Dev

How to break a for loop from inside an if statement in Julia?

분류에서Dev

Why does this program loop?

분류에서Dev

Cannot break out of an infinite loop in c++

분류에서Dev

Why does C program(to delete a line) delete the first character of my .txt file?

분류에서Dev

Break condition to continue loop

분류에서Dev

InputStreamReader throws NPE on second iteration of for loop

분류에서Dev

JSTL Break out of loop

분류에서Dev

Why does the first assert work, but not the second?

분류에서Dev

How can I break a symbolic link loop?

분류에서Dev

Why does this while stop after first iteration?

분류에서Dev

For loop executing only one iteration

분류에서Dev

Why does Python logging only hide stdout if the handler is added first?

분류에서Dev

why does RewriteCond %{SCRIPT_FILENAME} !-d break my RewriteRule

분류에서Dev

Append new list every iteration of the for loop

분류에서Dev

Why this break can't get me out of this while-do loop?

분류에서Dev

Why does Visual Studio break on Exceptions thrown and handled in a third party library?

분류에서Dev

Why does this for loop that iterates an array throw an exception?

분류에서Dev

Printing the final value of the iteration of a for loop

분류에서Dev

Why does a certain if statement only trigger once in a while loop?

분류에서Dev

For Loop Logic Iteration Why not behaving Same ?

분류에서Dev

Why will this only work with break;?

분류에서Dev

How to break a bash loop by output of an internal script?

분류에서Dev

Why does the first access to samba shared folder always fail?

분류에서Dev

Code does not terminate on second iteration in Scala Spark

분류에서Dev

Why does the cat command only read from the first file descriptor?

분류에서Dev

Why does katoolin break my Ubuntu 16.04 instalation?

Related 관련 기사

  1. 1

    Why does this for loop run into an ArrayIndexOtOfBounds error? (Java)

  2. 2

    React Native - Why does displaying components through iteration give the id prop as the last iteration number to all of the components?

  3. 3

    Why this Windows message loop does not handle shorcut/tab keys?

  4. 4

    How to break a for loop from inside an if statement in Julia?

  5. 5

    Why does this program loop?

  6. 6

    Cannot break out of an infinite loop in c++

  7. 7

    Why does C program(to delete a line) delete the first character of my .txt file?

  8. 8

    Break condition to continue loop

  9. 9

    InputStreamReader throws NPE on second iteration of for loop

  10. 10

    JSTL Break out of loop

  11. 11

    Why does the first assert work, but not the second?

  12. 12

    How can I break a symbolic link loop?

  13. 13

    Why does this while stop after first iteration?

  14. 14

    For loop executing only one iteration

  15. 15

    Why does Python logging only hide stdout if the handler is added first?

  16. 16

    why does RewriteCond %{SCRIPT_FILENAME} !-d break my RewriteRule

  17. 17

    Append new list every iteration of the for loop

  18. 18

    Why this break can't get me out of this while-do loop?

  19. 19

    Why does Visual Studio break on Exceptions thrown and handled in a third party library?

  20. 20

    Why does this for loop that iterates an array throw an exception?

  21. 21

    Printing the final value of the iteration of a for loop

  22. 22

    Why does a certain if statement only trigger once in a while loop?

  23. 23

    For Loop Logic Iteration Why not behaving Same ?

  24. 24

    Why will this only work with break;?

  25. 25

    How to break a bash loop by output of an internal script?

  26. 26

    Why does the first access to samba shared folder always fail?

  27. 27

    Code does not terminate on second iteration in Scala Spark

  28. 28

    Why does the cat command only read from the first file descriptor?

  29. 29

    Why does katoolin break my Ubuntu 16.04 instalation?

뜨겁다태그

보관