C++ 为什么我的程序不能识别素数?
我正在使用ErastoSchenes的筛选方法来制作一个程序,它可以打印出最大1000个素数。程序正在运行,但由于某些原因,程序不会删除复合数字。由于我的程序运行,我确信这只是一个逻辑错误,错误在我的identifyPrimes函数中的某个地方,但我一直无法找到它C++ 为什么我的程序不能识别素数?,c++,arrays,C++,Arrays,我正在使用ErastoSchenes的筛选方法来制作一个程序,它可以打印出最大1000个素数。程序正在运行,但由于某些原因,程序不会删除复合数字。由于我的程序运行,我确信这只是一个逻辑错误,错误在我的identifyPrimes函数中的某个地方,但我一直无法找到它 #include <cstdlib> #include <iostream> using namespace std ; void initializeNumbers ( char number[], int
#include <cstdlib>
#include <iostream>
using namespace std ;
void initializeNumbers ( char number[], int ARRAY_SIZE )
{
number[0] = 'I' ; // 'I' means Ignore
number[1] = 'I' ;
for ( int i = 2 ; i < ARRAY_SIZE ; i ++ )
number[i] = 'U' ;
/* --------------------------------------------------------
Function indexOfLeastU returns the least index such that
the character stored at that index is 'U', with the
exception that -1 is returned if no array element has
value 'U'.
-------------------------------------------------------- */
int indexOfLeastU ( char number[], int ARRAY_SIZE )
{
for ( int i = 0 ; i < ARRAY_SIZE ; i ++ )
if ( number[i] == 'U' )
return i ;
return -1 ;
} // end indexOfLeastU function
/* --------------------------------------------------------
Function identifyPrimes identifies which numbers are
prime by placing 'P's at those indices.
Composite #'s are marked with 'C's.
-------------------------------------------------------- */
void identifyPrimes ( char number[], int ARRAY_SIZE )
{
int leastU = indexOfLeastU ( number, ARRAY_SIZE ) ;
while ( leastU >= 0 )
{
number [leastU] = 'P' ; // 'P' for Prime
// mark multiples as Composite ...
for ( int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU)
number [leastU] = 'C' ; // 'C' for Composite
leastU = indexOfLeastU ( number, ARRAY_SIZE ) ;
} // end while loop
} // end identifyPrimes function
/* --------------------------------------------------------
Function printPrimes prints those array indices whose
corresponding elements have the value 'P'.
-------------------------------------------------------- */
void printPrimes ( char number[], int ARRAY_SIZE )
{
// print the indices at which a 'P' is stored ...
cout << "\nThe prime numbers up to 1000 are:\n\n" ;
for ( int i = 0 ; i < ARRAY_SIZE ; i ++ )
if ( number[i] == 'P' )
cout << i << '\t' ;
cout << endl << endl ;
} // end printPrimes function
int main ( )
{
// declare & initialize constants ...
const int MAX_NUMBER = 1000 ;
const int ARRAY_SIZE = MAX_NUMBER + 1 ;
// declare array ...
char number [ ARRAY_SIZE ] = { '\0' } ;
initializeNumbers ( number, ARRAY_SIZE ) ;
identifyPrimes ( number, ARRAY_SIZE ) ;
printPrimes ( number, ARRAY_SIZE ) ;
system("pause");
} // end main function
#包括
#包括
使用名称空间std;
无效初始化枚举数(字符数[],整数数组大小)
{
数字[0]=“I”;/“I”表示忽略
编号[1]=“I”;
对于(int i=2;i=0)
{
数字[leastU]=“P”;/“P”表示素数
//将倍数标记为复合。。。
对于(int i=(2*leastU);i 这里有个问题
for ( int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU)
number [leastU] = 'C'
这里有个问题
for ( int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU)
number [leastU] = 'C'
问题在于:
// mark multiples as Composite ...
for ( int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU)
number [leastU] = 'C' ; // 'C' for Composite
问题在于:
// mark multiples as Composite ...
for ( int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU)
number [leastU] = 'C' ; // 'C' for Composite
首先,您应该使用链表(您可以使用std::list)来实现这一点,而不是使用ignor符号,然后您可以删除您现在指定要忽略的元素
此程序(至少如图所示)不会编译,因为您忘记了initializeNumbers
的右括号
接下来,您需要修复此循环:
for ( int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU)
number [leastU] = 'C' ; // 'C' for Composite
for(int i=(2*leastU);i
您需要使用i
而不是leastU
首先,您应该使用链表(您可以使用std::list)来实现这一点,而不是使用ignor符号。然后您可以删除您现在指定要忽略的元素
此程序(至少如图所示)不会编译,因为您忘记了initializeNumbers
的右括号
接下来,您需要修复此循环:
for ( int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU)
number [leastU] = 'C' ; // 'C' for Composite
for(int i=(2*leastU);i
你需要使用i
而不是leastU
你的大括号不平衡,没有关闭initializeNumbers()
函数。你应该养成习惯,总是把{…}
围绕if
,while
,for
等的主体。即使主体中只有一条语句。这是一个非常复杂的实现筛选的代码。算法非常简单,我几乎无法理解您的代码。您可以在输出上打印整个表,这可能有助于发现错误。w你的输出是什么?还是永远循环?我在代码中添加了一个大括号,但是这个大括号并没有改变我程序的运行方式,程序仍然不能解决我识别素数的问题。你的大括号不平衡,没有关闭initializeNumbers()
函数。你应该养成总是把{…}
围绕if
,while
,for
等的主体。即使主体中只有一条语句。这是一个非常复杂的实现筛选的代码。算法非常简单,我几乎无法理解您的代码。您可以在输出上打印整个表,这可能有助于发现错误。w你的输出是什么?还是永远循环?我在代码中添加了一个大括号,但大括号并没有改变我的程序运行的方式,程序仍然不能解决我识别素数的问题。