그룹 빠른 선택 알고리즘을 구현하는 데이 코드를 사용하려고하면이 이상한 문제가 발생합니다. 나는 동적으로 할당 된 2D 배열을 사용하여 임의로 생성 된 정렬되지 않은 배열 10 개 그룹의 개별 요소를 보유합니다. 그룹 크기가 2, 5 또는 10 인 코드를 실행하면 완벽하게 작동합니다. 그러나 그룹 크기를 다른 그룹보다 작은 그룹으로 변경하면 어레이의 내용을 일부 테스트 수치로 초기화하려고 할 때 중단됩니다. 어떤 조언을 주셔서 감사합니다.나머지 2 차원 배열 액세스 위반
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <random>
#include <array>
using namespace std;
int groupSize = 0;
int groupSelect(int *, int, int, int);
int main()
{
// randomize array of size 10 with entries between 1 and 20.
random_device rd;
mt19937 eng(rd());
uniform_int_distribution<> distr(1, 20);
int max = 10;
int * Array;
Array = new int[max];
for (int i = 0; i < max; i++)
{
Array[i] = distr(eng);
}
// display array contents (unsorted)
cout << "Array contents are:\n";
for (int i = 0; i < max; i++)
{
cout << Array[i] << ", ";
}
cout << endl;
/*------------------------------------------------------------------------------*/
groupSize = 3;
int poo = groupSelect(Array, 0, 9, 5);
return 0;
/*------------------------------------------------------------------------------*/
delete[] Array;
}
int groupSelect(int* arr, int start, int end, int k)
{
bool remainder = false;
int size = 10;
if ((size % groupSize) != 0)
{
remainder = true;
}
//size = amount of groups of 5 (and remainder group)
size = size - (size % groupSize);
size = size/groupSize;
if(remainder)
size++;
cout << "Size = " << size << endl;
int** groups = new int*[size];
for (int i = 0; i < size; ++i)
{
if (remainder == true)
{
if (size - i == 1)
groups[i] = new int[((size) % (groupSize))];
}
else
groups[i] = new int[groupSize];
}
int testV = 0;
for (int i = 0; i < size; i++)
{
int temp = groupSize;
if (size - i == 1)
{
if (remainder)
temp = size % groupSize;
}
for (int j = 0; j < temp; j++)
{
groups[i][j] = testV; // codes break here
testV++;
}
}
cout << "\nGroup arrays' contents\n" << endl;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < groupSize; j++)
{
cout << "groups[" << i << "]["<<j<<"] contents = " << groups[i][j] << endl;
}
}
delete[] groups;
return 0;
}
에 액세스를 시도 그리고 아니, 그것은 외설! 알고리즘 전체를 구현하는 데까지는 아직 끝나지 않았습니다. 액세스 위반이라는이 장애물을 극복하는 데 도움이 필요합니다. –