简体   繁体   中英

Sort Specific Column of 2D int array (C++)

I've got a bit of a conundrum. I'm currently trying to create a user-defined function to sort a column (in ascending order) of a 2D int array I created and populated in the main function. I feel like I'm close, but for some reason the final output is incorrect, it provides a number for the final value that isn't even in the array. Judging from the value provided and the extra few seconds it takes to compile, I'm assuming I've messed up my bounds/ gone beyond them at some point within the code, but I've been fighting this thing for hours to no avail and I feel fresh (and likely more experienced) eyes would be-be of some use. I'm still in my "Intro to" class for programming, so ripping me a new one for obvious errors is encouraged as my final is this Thursday and any and all pointers/tips are appreciated. Cheers!

    #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;

Issue 1: The int arr[3][5]; you declared in sort2D() is NOT the same as the int arr[rows][columns]; you declared in main() .

lesson : check (or web search) on "pass by reference" & "pass by value" . For simplicity, I recommend pass by value.

Issue 2: The sort only compare 2 values and only run for 1 pass.. so {2,1,4,3} may get sorted to {1,2,3,4} but {1,4,3,2} will only get to {1,3,2,4} with 1 pass. @ZDF comment is helpful for this part.

Issue 3: at this line.. temp = arr[row_index+1][userCol-1]; when row_index is 2, this will refer to a location that is not in the arr[][] array. arr are only defined for row = 0,1,2 .. not 3 (when row_index is 2, row_index+1 is 3). This may answer :

it provides a number for the final value that isn't even in the array.

Solution.. hurm. I suggest you have a look and try.. and share where you stuck at. you may also test the sort2D in the main function before doing it as separate function. IMHO, you can start by 1st looking for the sorting algorithm that works (with sample data).. Then work on making it work in this project. ( :

p/s: I don't see my post as an answer.. more like a correction guide.

Your sort function is very close to bubble sort, one of the simplest sorting algorithms to understand and implement. With a little modification, your code will work :

    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 ...
    }

As you start in C++, an advice is to use C++ facilities if possible : std::vector for your array and std::qsort for sorting elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM