简体   繁体   English

代码在不使用 cout 时卡住并且不适用于某些数字

[英]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.这段代码的作用是给定所需的素数数量,从 2 开始,它用这些素数组成一个数组。 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.找到素数它的作用是将一个可能是素数的数字除以该数组上的每个数字,如果每个除法中的余数不为0,则表示这是一个素数,但如果不是余数之一是 0,这意味着它不是素数,所以我们继续搜索它之后的下一个数字。

The problems:问题:

1. If you delete, in the line 46 1.如果删除,在第46行

cout << "."; 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这就像薛定谔猫,因为如果我不使用 cout,我不知道它卡在代码的哪一部分,如果我使用 cout,它就可以工作

2. When you give numbers, like 1000000, the program is exited with code: -1073741571, why this? 2.当你给数字,比如1000000,程序退出,代码:-1073741571,这是为什么? well, in line 49 a division of 0/n is done;好吧,在第 49 行中,完成了 0/n 的除法; 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好吧,当想要的素数为 1000000 和搜索 1000 个第一个素数时为 1000 时,这部分代码在理论上是相同的,至少在找到前 1000 个素数时它应该是相同的

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;你不能cin >> primesWanted; because it is size of array which must be const.因为它是数组的大小,必须是 const。 other way that code works perfectly even without cout<<".";即使没有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.数组必须有一个const值,这意味着它不能以任何方式依赖于输入。 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:现在,如果我删除cout ,它会完美运行并给出 output :

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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