2016-07-29 2 views
0

재미있게 나는 소수를 사용하여 이미지를 만드는 프로그램을 작성하고 있습니다. 이를 위해 나는 임의의 지점까지 모든 자연수의 2 차원 배열을 만듭니다.

프라임은 이미지에서 검은 색 픽셀로 표시되며 합성 숫자는 흰색입니다. 이 프로그램은 1000 * 1000보다 작은 차원에서 작동하지만 초과하면 멈추게됩니다. 어떻게 수정해야합니까, 어떤 도움을 주셔서 감사합니다.큰 2D 배열을 정의하지 못했습니다 - C++

#include <iostream> 
#include <cmath> 
#include <fstream> 

using namespace std; 

bool isPrime(long long a){ 

    if(a==1){return false;} 

    if(a==2){return true;} 

    if(a%2==0){return false;} 

    long long root = sqrt(a); 
    for(long long i=3;i<=root;i+=2){ 
    if(a%i==0){return false;} 
    } 
    return true; 
} 

int main(){ 
    int width = 0, height = 0; 
    cout << "Which dimentions do you want the picture to be?" << endl; 
    cout << "Width: " << flush; 
    cin >> width; 
    cout << "Height: " << flush; 
    cin >> height; 

    /*Create matrix*/ 
    long long imageMap[height][width]; 
    long long numberOfPixels = width*height; 
    long long i = 1; 
    long long x = 0 , y = 0; 
    cout << "Number of pixels the image will have: " << numberOfPixels << endl; 

    while(i<=numberOfPixels){ 
    imageMap[x][y] = i; 
    x++; 
    if(x==width){ 
     y++; 
     x=0; 
    } 
    i++; 
    } 

    cout << "Image map done" << endl; 
    cout << "Creating prime map, please wait..." << endl; 


    /*Generate prime map*/ 
    int primeMap[width][height]; //The program gets stuck here 


    for(long long y = 0; y < width; y++){ 
    for(long long x = 0; x < height; x++){ 
     if(isPrime(imageMap[x][y])){ 
     primeMap[y][x] = 1; 
     } else { 
     primeMap[y][x] = 0; 
     } 
     cout << " x = " << x << flush; 
    } 
    cout << endl << "y = " << y << endl; 
    } 

    cout << "Writing to file, please wait..." << endl; 

    /*Write to file*/ 
    ofstream primeImage; 
    primeImage.open("prime.pbm"); 

    primeImage << "P1 \n"; 
    primeImage << width << " " << height << "\n"; 

    for(int y = 0; y < width; y++){ 
    for(int x = 0; x < height; x++){ 
     primeImage << primeMap[y][x] << " "; 
    } 
    primeImage << "\n"; 
    } 
    primeImage.close(); 
    cout << "Map creation done" << endl; 
    return 0; 
} 
+1

"막히다"그게 무슨 뜻입니까? 구체적으로 말하십시오 – Rakete1111

+4

두 가지 문제점 : 우선 C++에는 [가변 길이 배열] (https://en.wikipedia.org/wiki/Variable-length_array), 두 번째로 스택 크기 (배열을 포함한 지역 변수) 컴파일러에 의해 저장됩니다 매우 제한됩니다. Windows의 기본 스택 쪽은 프로세스 당 1MB입니다. 스택에 8MB 어레이를 생성하려고합니다. –

+3

가능한 해결책은 대신 ['std :: vector'] (http://en.cppreference.com/w/cpp/container/vector)에 대해 배우는 것이 좋습니다. –

답변

0

메모리가 작동하는 경우 앱의 기본 크기 스택은 1MB입니다. 요아킴은 정확합니다. 앞으로 가장 좋은 방법은 std :: vector에 대해 배우는 것입니다. 그러나 이것이 재미있는 일회용 프로그램이라면 간단히 스택 크기를 늘려서 작동시킬 수 있습니다. Visual Studio를 사용하는 경우 프로젝트 속성을 열고 링커 시스템 탭을 봅니다. 스택 예약 값을 사용하여 스택 크기를 늘릴 수 있습니다. 그러나 실제 작업 프로젝트에서는이 작업을 수행하지 않는 것이 좋습니다.

동의어 : 요아킴이 자신의 의견을 답변으로 다시 보내려는 경우 동의하는 것이 좋습니다.