Advertisement
huutho_96

Phần tử lẻ cuối

Dec 20th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. //https://www.facebook.com/CungHocLapTrinhUIT/?ref=bookmarks
  2. #include <iostream>
  3. using namespace std;
  4. void Nhap(float a[], int &n)
  5. {
  6.     cout << "Nhap so phan tu n: ";
  7.     cin >> n;
  8.     cout << "Nhap mang: " << endl;
  9.     for (int i = 0; i < n; i++)
  10.     {
  11.         cout << "Nhap phan tu thu " << i + 1 << " : ";
  12.         cin >> a[i];
  13.     }
  14. }
  15. int CuoiCung(float a[], int n)
  16. {
  17.     for (int i = n - 1; i > 0; i--)
  18.     {
  19.         if (a[i] < 0 && a[i] >= -1.0f)
  20.             return i;
  21.     }
  22.     return -1;
  23. }
  24. int main()
  25. {
  26.     float a[100];
  27.     int n;
  28.     Nhap(a, n);
  29.     int ret = CuoiCung(a, n);
  30.     if (ret == -1)
  31.         cout << "Khong co phan tu le\n" << endl;
  32.     else
  33.         cout << "vi tri so le cuoi cung: " << ret << endl;
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement