2.4

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.3 kB
19
Indexable
Never
#include <iostream>
using namespace std;
void Nhap(int*,int,bool&);
void Xuat(int*,int,bool);
int MinEvenNumber(int*,int);

int main()
{
    int n;
    bool exist = false;
    cout<<" n = ";
    cin>>n;
    int *a = new int[n];
    Nhap(a,n,exist);
    Xuat(a,n,false);
    if(exist)
    {
        Xuat(a,n,true);
        cout<<"So chan nho nhat trong mang : "<<MinEvenNumber(a,n)<<endl;

    }
    else
    {
        cout<<"Khong ton tai so chan trong mang!\n";
    }
    return 0;
}
void Nhap(int *a,int n,bool &exist)
{
    for(int i =0; i < n; ++i)
    {
        cout<<"a["<<i<<"] = ";
        cin>>a[i];
        if(a[i] % 2 ==0)exist = true;
    }
}
void Xuat(int*a,int n,bool printEvenNumber = false)
{
    cout<<"a[]";
    if(printEvenNumber)cout<<"(Chan)";
    cout<<":"<<endl;
    for(int i =0; i < n; ++i)
    {
        if(printEvenNumber)
        {
            if(a[i] % 2 == 0 )cout<<a[i]<<"\t";
        }
        else
        {
            cout<<a[i]<<"\t";
        }
    }
    cout<<endl;
}
int MinEvenNumber(int *a,int n)
{
    int result = INT_MAX;
    for(int i = 0; i < n; ++i)
    {
        if(a[i] < result && a[i] % 2 == 0)
        {
            result = a[i];
        }
    }
    return result;
}