Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int i, j, array[], userSize;
  5. char ch;
  6. void sort();
  7. void reverseSort();
  8.  
  9. int main()
  10. {
  11. cout << "How many integers would you like to sort? \n";
  12. cin >> userSize;
  13.  
  14. int* array = new int[userSize];
  15.  
  16. for (i = 0; i < userSize; ++i)
  17. {
  18. cout << "Enter digit #" << i + 1 << ": " << endl;
  19. cin >> array[i];
  20. }
  21.  
  22. cout << "Here is that set of values; " << endl;
  23. for (i = 0; i < userSize; ++i)
  24. {
  25. cout << " > " << array[i];
  26. }
  27.  
  28. sort();
  29. reverseSort();
  30.  
  31.  
  32. delete[] array;
  33. cin.get();
  34. ch = cin.get();
  35. }
  36.  
  37. void sort()
  38. {
  39. int* array = new int[userSize];
  40. //this code sorts it in ascending order
  41. for (i = 0; i < userSize; ++i)
  42. {
  43. for (j = 0; j < userSize - i - 1; ++j)
  44. {
  45. if (array[j] > array[j + 1])
  46. {
  47. array[j] = array[j] + array[j + 1];
  48. array[j + 1] = array[j] - array[j + 1];
  49. array[j] = array[j] - array[j + 1];
  50. }
  51. }
  52. }
  53.  
  54. cout << "\nHere is that set in ascending order;\n";
  55. for (i = 0; i < userSize; ++i)
  56. {
  57. cout << " > " << array[i];
  58. }
  59. }
  60.  
  61. void reverseSort()
  62. {
  63.  
  64. int* array = new int[userSize];
  65. //this code sorts it in descending order
  66. for (i = 0; i < userSize; ++i)
  67. {
  68. for (j = 0; j < userSize - i - 1; ++j)
  69. {
  70. if (array[j] < array[j + 1])
  71. {
  72. array[j] = array[j] + array[j + 1];
  73. array[j + 1] = array[j] - array[j + 1];
  74. array[j] = array[j] - array[j + 1];
  75. }
  76. }
  77. }
  78.  
  79. cout << "\nHere is that set in descending order;\n";
  80. for (i = 0; i < userSize; ++i)
  81. {
  82. cout << " > " << array[i];
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement