You will write a program that uses a multidimensional array having 3 rows and 8 columns and sorts each of the rows using both a bubble sort and a selection sort.

You must declare the array inside of main. You will have a for loop containing the calls to bubbleSort and selectionSort. You need to pass into function bubbleSort and selectionSort the following: 1) each column of the multidimensional array, 2) the size of the column, and 3) a particular row number of the multidimensional array to be used for printing out the "pass" shown on the following pages.

I'm missing something in int main() to get it to sort properly. Any help is greatly appreciated.

# include <iostream>

using namespace std; 


void bubbleSort (int arr[], int size) 

{ 
    bool swap; 
    int temp; 

    do 
    { 
        swap = false; 
        for (int count = 0; count < (size - 1) ; count ++) 
        { 
            if (arr[count] > arr [count + 1]) 
            { 
                temp = arr  [count]; 
                arr [count] = arr [count + 1]; 
                arr [count + 1] = temp; 
                swap = true; 
            } 

        } 

    } while (swap); 

} 

void selectionSort (int arr[], int size)
{ 
    int startScan, minIndex, minValue; 

    for (startScan = 0; startScan < (size - 1); startScan++) 

    { 
        minIndex = startScan; 
        minValue = arr[startScan]; 
        int index; 
        for (index = startScan + 1; index < size; index++)
        {
            if (arr[index] < minValue)

            { 
                minValue = arr[index]; 
                minIndex = index; 
            } 
        }
        arr[minIndex] = arr[startScan]; 
        arr[startScan] = minValue; 
    }
}




int main()

{
    const int SIZE1 = 3; 
    const int SIZE2 = 8; 

    int arr [SIZE1][SIZE2] = { { 105, 102, 107, 103, 106, 100, 104, 101 },
                               { 108, 106, 105, 110, 111, 100, 101, 107 },
                               { 112, 118, 104, 103, 111, 100, 102, 101 }

    };

    for (int i = 0; i <= SIZE1 - 1; i++)

    {
        bubbleSort(arr[i], SIZE2); 

        cout << endl; 

        selectionSort(arr[i], SIZE2); 

        cout << endl;



}

    system("pause"); 
    return 0; 

}

I'm confused by the assignment statement. You cannot directly pass a column from the 2D array to the sort functions. You can, and you are, passing rows from the array.

Also, your loop in main sorts a row with the bubble sort, then sorts that row again using selection - but it's already in order.

As ddanbe mentiones, you aren't displaying the array after sorting.

I think some of the assignment is missing here.

This article has been dead for over six months. Start a new discussion instead.