简体   繁体   中英

Code gets stuck when not using cout and doesnt work with some numbers

What the code does:

What this code does is given that number of primes that are wanted, startin from 2,it makes an array with those prime numbers. to find the prime numbers what it does is to divide a number that could be a prime by every number on that array, if the remainder in every division isn't 0, that means that is a prime, but if otherwise one of the remainders is 0, that means that isn't a prime, so we continue searching with the next number after it.

The problems:

1. If you delete, in the line 46

cout << ".";

the program gets stuck. This is like the schrodinger cat, because if I dont use cout, i cant know in what part of the code it gets stuck, and if i use cout, it works

2. When you give numbers, like 1000000, the program is exited with code: -1073741571, why this? well, in line 49 a division of 0/n is done; what is the weird here? well, that part of the code is theorically the same when the number of primes wanted is 1000000 and 1000 when searching the 1000 first prime numbers, and it should make the same, at least,while finding the first 1000 primes

Finally, the code:

#include<iostream>

using namespace std;

unsigned int primesWanted;
unsigned int possiblePrime=3; //We are going to start searching on the 3
unsigned int primesFound = 1; //We start asigning 2 because later we declare that we have one prime found, the 2

int main(int argc, char* argv[])
{
    cout <<"Number of primes wanted?:"<< endl << "=========================" << endl;
    cin  >> primesWanted;
    cout << endl << "========================="<< endl;     //the ======== is just for decoration
    
    int primes[primesWanted];                               // this is an array that contains all primes found
    primes[0]= 2;                                           // We asign the 2 as the first prime found
    
    while(primesFound < primesWanted)                       // this will be executed until all primes wanted are found
    {
        cout<< ".";                                         //if you delete this, it doesnt work i dont know why
        for (unsigned int i = 1; i <= primesFound; i++)
        {
            if(possiblePrime % primes[i-1])                 //if the number that we are searching remainder is 0 when dividing by a prime, thats mean that could be a prime
            {
                if(i == primesFound)                        //if i is equal as the number of primes found, that means that that possiblePrime is a prime
                {
                    primes[primesFound] = possiblePrime;    //we add it to the prime list
                    primesFound++;                          //we add one because we have added on prime to the list
                }
            }
            else
            {
                break;                                      //if the number that we are searching remainder is 0 when dividing by a prime, thats mean that is not a prime
            }
        }
        possiblePrime++;                                    //we continue to the next number to search
    }
    
    
    
    
                                                            //we print the primes array
    cout <<endl << "========================="<< endl;
    for (unsigned int i = 0; i < primesFound; i++)
    {
        cout <<primes[i] << ", ";
    }
    cout <<endl << "========================="<< endl;
    
    system("pause");
    return 0;
}

u can't cin >> primesWanted; because it is size of array which must be const. other way that code works perfectly even without cout<<".";which is impossible to be problem

There is a problem in your code, which when fixed, makes your code run perfectly!

An array MUST have a const value, which means it can't depend on input in any way. It must always be the same.

Change:

int primes[primesWanted];   

To:

int* primes = new int[primesWanted];

After all, an array is basically a pointer, so this is the same thing. Now if I remove the cout , it works perfectly and gives an output of:

Number of primes wanted?:
=========================
10

=========================

=========================
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
=========================
Press any key to continue . . .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM