我编写了此C ++程序,以使用选择排序对整数矩阵进行排序。我试图编译并运行该程序。但是我遇到了堆栈溢出错误,并且该消息还说了一些有关访问冲突写入位置的信息。
我尝试了以下步骤:
但是所有这些努力都没有用。
这是运行时错误所涉及的部分源代码。
void SelectionSort(int matrix[][10], int rowMax, int colMax)
{
//Runtime error was pointed over here.
int smallest, temp, position, count = 0;
int resultant[10][10];
for (int row = 0; row < rowMax; row++)
{
for (int j = 0; j < rowMax - 1; j++)
{
smallest = matrix[row][j];
position = j;
for (int k = j + 1; k < rowMax; k++)
{
if (matrix[row][k] < smallest)
{
smallest = matrix[row][k];
position = k;
}
}
temp = matrix[row][j];
matrix[row][j] = matrix[row][position];
matrix[row][position] = temp;
}
}
for (int i = 0; i < rowMax; i++)
for (int j = 0; j < colMax; j++)
resultant[i][j] = matrix[j][i];
if (count == 0)
{
SelectionSort(resultant, rowMax, colMax);
count++;
}
else
{
std::cout << "The sorted matrix is: " << std::endl;
for (int i = 0; i < rowMax; i++)
{
for (int j = 0; j < colMax; j++)
{
std::cout << matrix[i][j] << " ";
}
std::cout << "\n";
}
}
}
这些是引发的消息。
Exception thrown at 0x00007FF79D763488 in Sorting Algorithm Benchmarking Program.exe: 0xC0000005: Access violation writing location 0x000000BFBF600000. occurred
和
Unhandled exception at 0x00007FF79D763488 in Sorting Algorithm Benchmarking Program.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x00000058CE0B3000). occurred
我希望可以在控制台中打印排序后的矩阵。
例如,如果未排序的矩阵是:
1 9 5
8 3 6
7 4 2
然后输出应为:
1 2 3
4 5 6
7 8 9
我将要求您不要发送复杂的基于模板的建议,因为我处于C ++编程的中级水平。
谢谢!