Facebook
From Gray Anoa, 1 Year ago, written in C++.
Embed
Download Paste or View Raw
Hits: 102
  1. Viết chương trình c++ nhập vào kích thước mảng sau đó là các giá trị của mảng theo đúng thứ tự. Tìm và trả về phần tử nhỏ thứ 2 (nhì) trong mảng.                               #include <iostream>
  2.  
  3. using namespace std;
  4. void phantu2(double a[], int& n) {
  5.         cin >> n;
  6.         for     (int i = 0; i < n; i++)
  7.         {
  8.                 cin >> a[i];
  9.         }
  10.         for (int i = 0; i < n - 1; i++)
  11.         {
  12.                 for (int j = i + 1; j < n; j++)
  13.                 {
  14.                         if (a[j] < a[i])
  15.                         {
  16.                                 int tem = a[j];
  17.                                 a[j] = a[i];
  18.                                 a[i] = tem;
  19.                         }
  20.                 }
  21.         }
  22.         cout << a[1];
  23. }
  24. int main()
  25. {
  26.         int n;
  27.         double a[100];
  28.         phantu2(a, n);
  29.    return 0;
  30.  
  31. }
  32.