Skip to content

Instantly share code, notes, and snippets.

@tungaqhd
Created July 17, 2019 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tungaqhd/ed168dcd018a3b7ff95bbc053910ae6b to your computer and use it in GitHub Desktop.
Save tungaqhd/ed168dcd018a3b7ff95bbc053910ae6b to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
void nhap_mang(double *a, int n)
{
for(int i=0; i<n; i++)
{
cout<<"a["<<i<<"] = ";
cin>>a[i];
}
}
void sap_xep(double *a, int n)
{
for(int i=0; i<n; i++)
{
for(int j=i; j<n; j++)
{
if(a[i] > a[j])
{
double c = a[i];
a[i] = a[j];
a[j] = c;
}
}
}
}
void xuat_mang(double *a, int n)
{
for(int i=0; i<n; i++)
cout<<fixed<<a[i]<<" ";
cout<<endl;
}
double tong_pt_chan(double *a, int n)
{
double rs = 0;
for(int i=0; i<n; i+=2)
rs += a[i];
return rs;
}
void ghi(double *a, int n)
{
ofstream f("data.txt", ios::out);
f<<n<<endl;
for(int i=0; i<n; i++)
f<<setprecision(2)<<fixed<<a[i]<<' ';
f<<endl<<"end";
f.close();
}
void lon_n(double *a, int n)
{
cout<<"Phan tu lon nhat la : "<<a[n-1]<<endl;
cout<<"Phan tu lon thu nhi la : "<<a[n-2]<<endl;
}
bool dlt(double *a, int n)
{
for(int i=0; i<n-2; i++)
if(a[0]>0.0 && a[1]>0.0 && a[2]>0.0)
return true;
return false;
}
void xoa(double *a, int &n, int k)
{
for(int i=k; i<n-1; i++)
a[i] = a[i+1];
n--;
}
void xoa5(double *a, int &n)
{
while(true)
{
bool b = true;
for(int i=0; i<n; i++)
{
if(a[i] > 5.0)
{
b = false;
xoa(a, n, i);
}
}
if(b)
break;
}
}
int main()
{
int n;
cout<<"Nhap so luong phan tu : ";
cin>>n;
double *a = new double[n];
//nhap mang
nhap_mang(a, n);
// sap xep
sap_xep(a, n);
// tong chan
cout<<"Tong cac phan tu o vi tri chan la : "<<endl;
cout<<tong_pt_chan(a, n)<<endl;
// xuat mang
cout<<"Mang hien tai la : ";
xuat_mang(a, n);
// xuat file
ghi(a, n);
// phan tu lon nhat va lon nhi
lon_n(a, n);
// 3 so nguyen lien tiep
if(dlt(a, n))
cout<<"Mang co ton tai 3 so duong lien tiep"<<endl;
else
cout<<"Mang khong ton tai 3 so duong lien tiep"<<endl;
// xoa so lon hon 5
xoa5(a, n);
// xuat mang
xuat_mang(a, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment