Advertisement
KuoHsiangYu

TestArray2

Aug 17th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. //KUO_HSIANG_YU
  2. //https://www.facebook.com/groups/1403852566495675/permalink/2392492320965023/
  3.  
  4. #include <iostream>
  5. #include <cstdlib>
  6.  
  7. using std::cout;
  8. using std::cin;
  9.  
  10. int main(int argc, char *argv[]) {
  11.     system("color f0");
  12.  
  13.     int *array = NULL;
  14.     int size = 0;
  15.     int temp = 0;
  16.     int i = 0, j = 0;
  17.     cout << "Please enter array size: ";
  18.     cin >> size;
  19.  
  20.     if (size < 1) {
  21.         cout << "Error, array size can not be less than 1.\n";
  22.         system("pause");
  23.         return 0;
  24.     }
  25.     array = new int[size];
  26.  
  27.     for (i = 0; i < size; i++) {
  28.         array[i] = 0;
  29.     }
  30.  
  31.     for (i = 0; i < size; i++) {
  32.         cout << (i + 1) << ". ";
  33.         cin >> array[i];
  34.     }
  35.  
  36.     //Using Bubble Sort to sort array.
  37.     for (i = 0; i < (size - 1); i++) {
  38.         for (j = (i + 1); j < size; j++) {
  39.             if (array[i] < array[j]) {
  40.                 temp = array[i];
  41.                 array[i] = array[j];
  42.                 array[j] = temp;
  43.             }
  44.         }
  45.     }
  46.  
  47.     bool isSecond = false;
  48.     for (int i = 0; i < size; i++) {
  49.         if (true == isSecond) {
  50.             cout << " > ";
  51.         }
  52.         else {
  53.             isSecond = true;
  54.         }
  55.         cout << array[i];
  56.     }
  57.     cout << "\n";
  58.     cout << "\n";
  59.  
  60.     delete[] array;
  61.     array = NULL;
  62.  
  63.     system("pause");
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement