SHARE
TWEET

Bài 5 Tin

anhkiet2507 Apr 5th, 2019 6,616 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Bài 5: Nhập vào 3 số nguyên. Tính ra min max
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6. #define MAX 100
  7. void NhapMang(int arr[], int &n);
  8. void XuatMang(int arr[], int &n);
  9. void hoanVi(int &a, int &b);
  10. void sapXepTang(int arr[], int n);
  11. int main()
  12. {
  13.     int array[MAX];
  14.     int size;
  15.     NhapMang(array, size);
  16.     system("cls");
  17.     sapXepTang(array, size);
  18.     cout << "Mang duoc sap xep theo thu tu tang dan la" << endl;
  19.     XuatMang(array, size);
  20.     system("pause");
  21.     return 0;
  22. }
  23. void XuatMang(int arr[], int &n)
  24. {
  25.     cout << "Min = " << arr[0] << endl;
  26.     cout << "Max = " << arr[n - 1] << endl;
  27. }
  28.  
  29. void NhapMang(int arr[], int &n)
  30. {
  31.     cout << "Nhap so phan tu cua mang:";
  32.     cin >> n;
  33.     cout << "Nhap cac phan tu:" << endl;
  34.     for (int i = 0; i < n; i++)
  35.     {
  36.         cout << "Nhap phan tu thu [" << i + 1 << "] = ";
  37.         cin >> arr[i];
  38.     }
  39.  
  40. }
  41. void hoanVi(int &a, int &b)
  42. {
  43.     int temp = a;
  44.     a = b;
  45.     b = temp;
  46. }
  47.  
  48. void sapXepTang(int arr[], int n)
  49. {
  50.     for (int i = 0; i < n - 1; i++)
  51.     {
  52.         for (int j = i + 1; j < n; j++)
  53.         {
  54.             if (arr[i] > arr[j])
  55.                 hoanVi(arr[i], arr[j]);
  56.         }
  57.     }
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top