排序2D int数组的特定列(C ++)

时间:2018-12-11 08:02:23

标签: c++ arrays

我有一个难题。我目前正在尝试创建用户定义的函数,以对我在主函数中创建并填充的2D int数组的列(以升序排列)进行排序。我感觉自己接近了,但是由于某种原因最终输出不正确,它为最终值提供了一个甚至不在数组中的数字。从提供的值和编译花费的额外几秒钟来看,我认为我在代码中的某个时候弄乱了边界/超出了边界,但是我一直在努力奋斗数小时而徒劳无功而且我觉得新鲜的(也许是更有经验的)眼睛会有些用处。我仍在编程的“入门”课程中,因此鼓励我改写一个明显的错误,因为本周四的决赛是我的结社,并且对所有指针/技巧都表示赞赏。干杯!

    #include <iostream>

    using namespace std;


    void sort2D(int arr[3][5], int rows, int columns) //the problem child
{
    int userCol;
    cout<<"Please enter the number of the column you'd like to sort: "<<endl;
    cin>>userCol; //ask for appropriate column


    for (int row_index=0; row_index<rows; row_index++) //start with first row and continue for all values in code
        {
            int temp;

            if ((arr[row_index][userCol-1]<arr[row_index+1][userCol-1]))//if first value of selected column is less than next value
                {
                    temp = arr[row_index+1][userCol-1];//create copy of second value
                    arr[row_index+1][userCol-1]=arr[row_index][userCol-1]; //replace second value with first value
                    arr[row_index][userCol-1]=temp;//set first equal to second's original value
                }
        }

    for(int i=0; i<rows; i++)//print that shiz
        {
            for(int j=0; j<columns; j++)
                {
                    cout<<arr[i][j]<<" ";
                }
            cout<<endl;
}
}

int main()
{
    const int rows = 3;
    const int columns = 5;

    int arr[rows][columns];

    for (int row_index=0; row_index<rows; row_index++)
    {
        for (int column_index=0; column_index<columns; column_index++)
        {
            arr[row_index][column_index] = (50+rand()%51);
            cout << arr[row_index][column_index]<<" ";
        }
        cout << endl;
    }

    findMaxandIndex(arr, rows, columns);//i left my code for this out because it's working and isn't utilized in the problem code
    cout << endl;
    sort2D(arr, rows, columns);
    return 0;

2 个答案:

答案 0 :(得分:0)

问题1:您在int arr[3][5];中声明的sort2D()与您在arr[rows][columns];中声明的int main()不同。

课程:检查“通过引用传递”和“按值传递”。为简单起见,我建议按值传递。

问题2:排序仅比较2个值并且只运行1次。.因此{2,1,4,3}可能会排序为{1,2,3,4}但{1,4,3 ,2}仅经过1遍才能到达{1,3,2,4}。 @ZDF注释对此部分很有帮助。

问题3:在此行。temp = arr[row_index+1][userCol-1];当row_index为2时,它将引用不在arr [] []数组中的位置。仅为row = 0,1,2 ..而不是3定义arr(当row_index为2时,row_index + 1为3)。这可能会回答:

  

它为最终值提供了一个甚至不在数组中的数字。

解决方案。我建议您看看并尝试..并分享您坚持的地方。您也可以在作为单独功能进行操作之前在main函数中测试sort2D。恕我直言,您可以从1开始寻找适用的排序算法(使用示例数据)。然后在该项目中继续使其工作。 (:

p / s:我不认为我的帖子是答案。更像是更正指南。

答案 1 :(得分:0)

您的排序功能非常接近气泡排序,它是要理解和实现的最简单的排序算法之一。稍作修改,您的代码即可运行:

    void sort2D(int arr[3][5], int rows, int columns) //the problem child
    {

            //input userCol ...

            //sorting start here ...
           bool found = true; //we can quit the loop if the array is already sorted.

            for (int bubble = 0; bubble < rows-1 && found ; bubble++)
            {
                    found = false;

                    for (int row_index=0; row_index < rows - bubble - 1; row_index++)
                    {
                            int temp;

                            if ((arr[row_index][userCol-1] < arr[row_index+1][userCol-1]))//if first value of selected column is less than next value
                            {
                                    //swap two elements.
                                    temp = arr[row_index+1][userCol-1];//create copy of second value
                                    arr[row_index+1][userCol-1]=arr[row_index][userCol-1]; //replace second value with first value
                                    arr[row_index][userCol-1]=temp;//set first equal to second's original value

                                    found = true; //we found something, continue to sort.
                            }
                    }
            }
            //print out the result ...
    }

从C ++开始时,建议尽可能使用C ++工具:std::vector用于数组,std::qsort用于元素排序。

相关问题