操纵二维整数数组时

时间:2019-09-29 16:42:48

标签: c++ visual-studio c++17

我编写了此C ++程序,以使用选择排序对整数矩阵进行排序。我试图编译并运行该程序。但是我遇到了堆栈溢出错误,并且该消息还说了一些有关访问冲突写入位置的信息。

我尝试了以下步骤:

  1. 试图将数组大小从50 x 50减少到10 x 10。
  2. 尝试对结果数组进行堆分配(使用new)。
  3. 试图将结果数组声明为静态。
  4. 试图检查“调试”菜单中的“内存”选项以查找特定的内存 位置(但无法理解)。
  5. 尝试检查IDE自动打开的chkstk.asm文件。

但是所有这些努力都没有用。

这是运行时错误所涉及的部分源代码。

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 ++编程的中级水平。

谢谢!

0 个答案:

没有答案