2013-11-20 49 views
0

我正在使用Erastosthenes方法的筛选器,以便打印所有素数最多为1000的程序。程序正在运行,但由于某种原因程序不会删除组合的数字。由于我的程序运行,我确信它只是一个逻辑错误,并且该错误在我的identifyPrimes函数中,但我一直无法找到它。为什么我的程序不能识别素数?

#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 
+1

你的括号不均衡,有没有密切的'initializeNumbers的()'函数。 – Barmar

+0

你应该习惯于在'if',''while','for'等身体周围放置'{...}',即使在主体中只有一个语句。 – Barmar

+0

这是一个非常复杂的代码,用于实现筛选。该算法非常简单,我几乎不能理解你的代码。您可以在输出上打印整个表格,这可能有助于发现错误。 – luk32

回答

3

的问题是在这里:

// mark multiples as Composite ... 
    for (int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU) 

     number [leastU] = 'C' ; // 'C' for Composite 

的分配应该是:

 number[i] = 'C'; 
2

这里有个问题。

for (int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU) 
    number [leastU] = 'C' 

应该

number[i] = 'C'; 
1

首先,而非ignor标志,你应该用链表来实现这个(你可以se std :: list)。那么您可以删除您现在指定要忽略的元素。

由于您忘记了关闭括号initializeNumbers,此程序(至少如此处所示)将无法编译。

接下来,你需要解决这个循环:

for (int i = (2 * leastU) ; i < ARRAY_SIZE ; i += leastU) 

       number [leastU] = 'C' ; // 'C' for Composite 

您需要使用的i代替leastU

相关问题