evoloc
evoloc

Reputation: 9

Sorting 2D Dynamic Array C++

I am trying to sort a two dimensional dynamic array when row 1 is for product ID and row 2 is for the product price. I want to sort by product ID, and have the results displayed formatted with width of 5. Here is my code:

This section is fine and does what I am looking for:

void readData (int**, int, int);
void printData(int**, int, int);
void sortbyPartID(int**, int, int);

int main()
{
    int index;
    int **PriceSheet, rows, columns;
    cout << "Enter the number of Products, and then the number of values associated with the products:  ";
    cout << "For default values, enter 5 (FIVE ITEMS, and enter 2 (TWO Values: ID and PRICE). ";
    cin >> columns >> rows;
    cout << endl;

    PriceSheet = new int* [rows];
    for (int row = 0; row < rows; row++)
        PriceSheet [row] = new int[columns];

    readData (PriceSheet, rows, columns);
    cout << endl;

    printData(PriceSheet, rows, columns);

    sortbyPartID(PriceSheet, rows, columns);

    return 0;

}

void readData (int **p, int rowSize, int colSize)
{
    for (int row = 0; row < rowSize; row++)
    {

        cout << "Row ZERO is the Product ID and Row 1 is the Product Price\n";
        cout << "Enter " << colSize << " numbers for the row number " << row << ": ";
        for (int col = 0; col < colSize; col++)
        cin >> p[row][col];
        cout << endl;
    }
}

void printData (int **p, int rowSize, int colSize)
{
    cout << "\n\nThese are the Products IDs and Prices as entered in the system:\n";
    for (int row = 0; row < rowSize; row++)
    {
        for (int col = 0; col < colSize; col++)
            cout << setw(5) << p[row][col];
        cout << endl;
    }
}

THIS SECTION IS WHERE I NEED HELP

It reads correctly and prints the unsorted array also correctly, but I cannot figure out a way to sort the array. Specifically speaking, I need help on the void sortbyPartID function. I would like to use bubble sort, and I cannot figure out how to get this function to work. Any help with the sorting function/algorithm would be greatly appreciated.

void sortbyPartID (int **p, int rowSize, int colSize)
{
    int swap = -1;
    int end = colSize;
    int sortedID = **p;
    cout << "\n\nThese are the Products sorted Products IDs:\n";

    for (int counter = colSize -1; counter >= 0; counter --)
        for (int index = 0; index < end ; index ++)
        {
            if (sortedID[index] > sortedID[index + 1])
            {
                swap = *sortedID[index + 1];
                sortedID[index + 1] = sortedID[index];
                *sortedID[index] = swap;
            }
        }

    for(int index = 0; index < end; index++)
    {
        cout << sortedID[index] << ", ";
    }
    cout << endl;
    end --;
}

When I run, I get some weird results on the last section. Maybe I am missing something simple, not sure.

Upvotes: 0

Views: 1127

Answers (3)

Jarod42
Jarod42

Reputation: 217980

int sortedID = **p; is not what you want, and should be removed. (I think you wanted int** sortedID = p;)

Your bubble-sort should be something like:

for (int counter = colSize -1; counter >= 0; --counter)
{
    for (int index = 0; index < end - 1 ; ++index)
    {
        if (p[index][0] > p[index + 1][0])
        {
            // std::swap(p[index], p[index + 1]);
            int* swap = p[index + 1];
            p[index + 1] = p[index];
            p[index] = swap;
        }
    }
}

Live Demo

Upvotes: 1

Sorin
Sorin

Reputation: 11968

You can simplify the entire thing by using objects. Objects allow you to handle the related data in a sane fashion. Also highly recommend are vectors instead of C arrays.

struct Product {
  int id;
  int price;
  vector<int> others;
}

You can then store your products in vector<Product> my_products; and then sorting everything with

std::sort(my_products.begin(), my_products.end(),
          [](const Product& a, const Product& b) { return a.id < b.id; });

You can keep the existing input/output format, but place the values in the right place. This way it's almost impossible to mess up the attributes and everything is easy to work with.

Upvotes: 1

PatilUdayV
PatilUdayV

Reputation: 647

We can also perform this using do-while as follows:

bool isSwaped;
do
{
    isSwaped = false;
    for (int index = 0; index < end - 1 ; ++index)
    {
        if (p[index][0] > p[index + 1][0])
        {
            int swap = p[index + 1][0];
            p[index + 1][0] = p[index][0];
            p[index][0] = swap;
            isSwaped = true;
        }
    }
} while (isSwaped);

Upvotes: 2

Related Questions