Search code examples
c++arraysmultidimensional-arraybubble-sort

Swapping rows in 2-D array C++


So yeah, I wrote a program which sort rows of two-dimensional array in ascending order according to sum of all positive even elements in every row, BUT it does not work properly. Sometimes it can swap rows correctly but mostly it feels like this program only swap two adjacent rows or something like that.

Probably, there is a problem with incorrect using bubble sort for 2-d arrays. Here is the function where I did most things:

void print(int** arr, int rows, int columns) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            cout << setw(7) << arr[i][j];
        }
        cout << endl;
    }
    int* sumRows = new int[rows];
    int sum = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            if (arr[i][j] > 0 && arr[i][j] % 2 == 0)
            {
                //cout << setw(7) << arr[i][j];
                sum = sum + arr[i][j];
            }
        }
        cout << endl << "Sum of positive even elements in the row " << i + 1 << " = " << sum;
        sumRows[i] = sum;
        sum = 0;
    }
    cout << endl << "Array of sums: ";
    for (int i = 0; i < rows; i++) {
        cout << setw(7) << sumRows[i];
    }

    //for (int i = 0; i < r; i++) cout << setw(7) << sumRows[i];
    cout << endl;

    bool swapped;
    for (int i = 0; i < rows - 1; i++)
    {
        swapped = false;
        for (int j = 0; j < columns - 1; j++)
        {
            for (int k = 0; k < rows - i - 1; k++) {
                if (sumRows[k] > sumRows[k + 1])
                {
                    swap(arr[k][j], arr[k + 1][j]);
                    swapped = true;
                }
            }
        }

        if (swapped == false) break;
    }

    cout << endl << endl << "Swapped array:" << endl;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            cout << setw(7) << arr[i][j];
        }
        cout << endl;
    }
}

Full code:

#include <iostream>
#include <iomanip>
#include <time.h>
#include <conio.h>
#include <algorithm>
using namespace std;

int** createMalloc(int, int);
int** createCalloc(int rows, int columns);

int** createNew(int rows, int columns);
void deleteNew(int** arr, int rows);

void init(int**, int, int);
void freeMemory(int**, int);
void print(int**, const int, const int);

void initPrint(int** arr, int rows, int columns);

void main() {
    int rowCount, colCount;
    cout << "Enter number of rows: "; cin >> rowCount;
    cout << "Enter number of columns: "; cin >> colCount;
    cout << " Array creation algorithm\n";
start:
    cout << "Input number : \n1 for malloc\n2 for calloc\n3 for new\n";
    int k;
    cin >> k;
    switch (k) {
    case 1: {
        int** a = createMalloc(rowCount, colCount);
        initPrint(a, rowCount, colCount);

        freeMemory(a, rowCount);
        break;
    }
    case 2: {
        int** a = createCalloc(rowCount, colCount);
        initPrint(a, rowCount, colCount);

        freeMemory(a, rowCount);
        break;
    }
    case 3: {
        int** a = createNew(rowCount, colCount);
        initPrint(a, rowCount, colCount);

        deleteNew(a, rowCount);
        break;
    }
    default:cout << "Input 1, 2 or 3, please.";
        cout << endl << endl;
        goto start;
    }
    cout << endl << endl;
}

int** createMalloc(int rows, int columns) {
    int** arr = (int**)malloc(rows * sizeof(int*));
    for (int i = 0; i < rows; i++) {
        arr[i] = (int*)malloc(columns * sizeof(int));
    }
    return arr;
}

int** createCalloc(int rows, int columns) {
    int** arr = (int**)calloc(rows, sizeof(int*));
    for (int i = 0; i < rows; i++) {
        arr[i] = (int*)calloc(columns, sizeof(int));
    }
    return arr;
}

int** createNew(int rows, int columns) {
    int** arr = new int* [rows];
    for (int i = 0; i < rows; i++) {
        arr[i] = new int[columns];
    }
    return arr;
}

void initPrint(int** arr, int rows, int columns) {
    init(arr, rows, columns);
    print(arr, rows, columns);
}

void init(int** arr, int rows, int columns) {
    const int Low = -10, High = 10;
    srand((unsigned)time(NULL));
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            arr[i][j] = Low + rand() % (High - Low + 1);
        }
    }
}

void freeMemory(int** arr, int rows) {
    for (int i = 0; i < rows; i++) {
        free(arr[i]);
    }
    free(arr);
}

void deleteNew(int** arr, int rows) {
    for (int i = 0; i < rows; i++) {
        delete[] arr[i];
    }
    delete[] arr;
}

void print(int** arr, int rows, int columns) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            cout << setw(7) << arr[i][j];
        }
        cout << endl;
    }
    int* sumRows = new int[rows];
    int sum = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            if (arr[i][j] > 0 && arr[i][j] % 2 == 0)
            {
                //cout << setw(7) << arr[i][j];
                sum = sum + arr[i][j];
            }
        }
        cout << endl << "Sum of positive even elements in the row " << i + 1 << " = " << sum;
        sumRows[i] = sum;
        sum = 0;
    }
    cout << endl << "Array of sums: ";
    for (int i = 0; i < rows; i++) {
        cout << setw(7) << sumRows[i];
    }

    //for (int i = 0; i < r; i++) cout << setw(7) << sumRows[i];
    cout << endl;

    bool swapped;
    for (int i = 0; i < rows - 1; i++)
    {
        swapped = false;
        for (int j = 0; j < columns - 1; j++)
        {
            for (int k = 0; k < rows - i - 1; k++) {
                if (sumRows[k] > sumRows[k + 1])
                {
                    swap(arr[k][j], arr[k + 1][j]);
                    swapped = true;
                }
            }
        }

        //IF no two elements were swapped by inner loop, then break 
        if (swapped == false) break;
    }

    cout << endl << endl << "Swapped array:" << endl;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            cout << setw(7) << arr[i][j];
        }
        cout << endl;
    }
}

P.S. Two-dimensional arrays must be necessarily dynamic ones. Besides, I needed to give user a choice of creating an array with malloc, calloc or new. Also, cannot do this task using vector yet.

P.P.S. I know that most of you will find it easy but for me it is definitely not as this task is my homework in a university and we have not learnt any sorting algorithms. Anyway, when I asked the teacher how can I sort rows like that, he told me to use bubble sort so here I am


Solution

  • What actually helped me:

    void swapInitial(int** arr, int* sumRows, int rows, int columns) {
        bool swapped;
        for (int i = 0; i < rows - 1; i++)
        {
            swapped = false;
    
            for (int k = 0; k < rows - i - 1; k++) {
                if (sumRows[k] > sumRows[k + 1])
                {
                    swap(arr[k], arr[k + 1]); 
                    swap(sumRows[k], sumRows[k + 1]); 
                    swapped = true;
                }
            }
    
            if (swapped == false) break;
        }
    }