fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using std::cout;
  5. using std::endl;
  6. using std::setw;
  7.  
  8. void readData (int**, int, int);
  9. void printData(int**, int, int);
  10. void sortbyPartID(int**, int, int);
  11.  
  12. int main()
  13. {
  14. int columns = 2;
  15. int rows = 5;
  16. cout << endl;
  17.  
  18. int **PriceSheet = new int* [rows];
  19. for (int row = 0; row < rows; row++)
  20. PriceSheet [row] = new int[columns];
  21.  
  22. readData (PriceSheet, rows, columns);
  23. cout << endl;
  24.  
  25. printData(PriceSheet, rows, columns);
  26.  
  27. sortbyPartID(PriceSheet, rows, columns);
  28.  
  29. printData(PriceSheet, rows, columns);
  30.  
  31.  
  32. for (int row = 0; row < rows; row++)
  33. delete[] PriceSheet[row];
  34. delete[] PriceSheet;
  35. }
  36.  
  37. void readData (int **p, int rowSize, int colSize)
  38. {
  39. for (int row = 0; row < rowSize; row++)
  40. {
  41. for (int col = 0; col < colSize; col++)
  42. p[row][col] = (row % 2 ? 2 * row + 5 : row) + col;
  43. }
  44. }
  45.  
  46. void printData (int **p, int rowSize, int colSize)
  47. {
  48. cout << "\n\nThese are the Products IDs and Prices as entered in the system:\n";
  49. for (int row = 0; row < rowSize; row++)
  50. {
  51. for (int col = 0; col < colSize; col++)
  52. cout << setw(5) << p[row][col];
  53. cout << endl;
  54. }
  55. }
  56.  
  57. void sortbyPartID (int **p, int rowSize, int colSize)
  58. {
  59. int swap = -1;
  60. int end = rowSize;
  61. for (int counter = rowSize - 1; counter >= 0; --counter)
  62. {
  63. for (int index = 0; index < end - 1 ; ++index)
  64. {
  65. if (p[index][0] > p[index + 1][0])
  66. {
  67. // std::swap(p[index], p[index + 1]);
  68. int* swap = p[index + 1];
  69. p[index + 1] = p[index];
  70. p[index] = swap;
  71. }
  72. }
  73. }
  74. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout


These are the Products IDs and Prices as entered in the system:
    0    1
    7    8
    2    3
   11   12
    4    5


These are the Products IDs and Prices as entered in the system:
    0    1
    2    3
    4    5
    7    8
   11   12