2013-11-20 2 views
0

Sawve of 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

leastU의 사용해야합니다. – Barmar

+0

본문에 단 하나의 문장이 있더라도'if ...','while','for' 등의 본문 주위에'{...}'을 넣는 습관을 가져야합니다. – Barmar

+0

그것은 체를 구현하기위한 매우 복잡한 코드입니다. 알고리즘은 매우 간단하며 코드를 간신히 이해할 수 있습니다. 아마도 오류를 찾는 데 도움이되는 출력에 전체 테이블을 인쇄 할 수 있습니다. – luk32

답변

3

을해야한다 연결된 목록으로 이것을 구현해야한다. se std :: list). 이제는 무시하도록 지정된 요소를 삭제하면됩니다.

initializeNumbers의 닫는 괄호를 잊어 버린이 프로그램은 컴파일되지 않습니다.

다음,이 루프를 해결해야합니다

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

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

당신은 당신의 괄호는`initializeNumbers의 더 가까운() '함수가 없다, 균형없는 i 대신

2

여기에 문제가 있습니다.

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

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

는 할당해야한다 : 첫째

 number[i] = 'C'; 
1

대신 ignor 기호, 당신

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

이 문제는 여기에있다

number[i] = 'C'; 
관련 문제