재미있게 나는 소수를 사용하여 이미지를 만드는 프로그램을 작성하고 있습니다. 이를 위해 나는 임의의 지점까지 모든 자연수의 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;
}
"막히다"그게 무슨 뜻입니까? 구체적으로 말하십시오 – Rakete1111
두 가지 문제점 : 우선 C++에는 [가변 길이 배열] (https://en.wikipedia.org/wiki/Variable-length_array), 두 번째로 스택 크기 (배열을 포함한 지역 변수) 컴파일러에 의해 저장됩니다 매우 제한됩니다. Windows의 기본 스택 쪽은 프로세스 당 1MB입니다. 스택에 8MB 어레이를 생성하려고합니다. –
가능한 해결책은 대신 ['std :: vector'] (http://en.cppreference.com/w/cpp/container/vector)에 대해 배우는 것이 좋습니다. –