Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. //Bài tập 6: Viết hàm tráo đổi giá trị của 2 số. Sau đó thực hiện sắp xếp dãy số cho trước.
  2.  
  3. #include <iostream>
  4. using namespace std;
  5. void exchange(int &a, int &b)
  6. {
  7.     int t;
  8.     t = a;
  9.     a = b;
  10.     b = t;
  11. }
  12. int main()
  13. {
  14.     int x, y;
  15.     cout << " Enter - x , y: ";
  16.     cin >> x >> y;
  17.     cout << "the two - digit order when unchanged is : " << x << " ; " << y << endl;
  18.     exchange(x, y);
  19.     cout << "the two - digit order when unchanged is : " << x << " ; " << y << endl;
  20.     int arr[8] = { 1, 3, 2, 4, 7, 5, 8, 6 };
  21.     cout << "Serial number when not sorted : " << endl;
  22.     for (int i = 0; i < 8; i++)
  23.     {
  24.         cout << arr[i] << " , ";
  25.     }
  26.     cout << endl;
  27.     for (int i = 0; i < 7; i++)
  28.     {
  29.         for (int j = i + 1; j < 8; j++)
  30.         {
  31.             if (arr[i] > arr[j])
  32.             {
  33.                 exchange(arr[i], arr[j]);
  34.             }
  35.         }
  36.     }
  37.     cout << "The sequence of numbers when shorted from baby to large is : " << endl;
  38.     for (int i = 0; i < 8; i++)
  39.     {
  40.         cout << arr[i] << " , ";
  41.     }
  42.     cout << endl;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement