//chuong trinh nhap mang co dieu kien--------------------------
void nhap()
{
printf("Nhap so phan tu ban muon N = ");
scanf("%d",&n);
if(n<10)
{
for(i=0;i<n;i++)
{
printf("Nhap gia tri phan tu thu a[%d]= ",i);
scanf("%d",&a[i]);
}
}
else
{
printf("Nhap gia tri phan tu phai nho hon 10 ");
}
}
// chon chuonh trinh thuc hien---------------------------------
void chon_mode()
{
printf("\n------------------------\n");
printf(" MENU \n");
printf("1. Tinh tong so phan tu chan, le \n");
printf("2. So luong phan tu chan le \n");
printf("3. Max, Min va vi tri cua chung\n");
printf("4. Sap xep mang theo thu tu giam dan \n");
printf("0. Exit\n");
printf("chon chuong trinh can su dung: ");
scanf("%d", &mode);
}
// chuong trinh tinh tong--------------------------------------
void tinhtong()
{
tong=0;
for(i=0;i<n;i++)
{
tong=tong+a[i];//tinh tong gia tri phan tu mang
if(a[i]%2==0)
{
tongchan=tongchan+a[i];//tinh tong chan
}
else
{
tongle=tongle+a[i];//tinh tong le
}
}
printf("Tong mang: %d\n",tong);
printf("Tong chan: %f, so phan tu chan: %d \n",tongchan,dem);
printf("Tong le: %d, so phan tu le: %d \n",tongle,n-dem);
}
// chuong trinh tinh tong--------------------------------------
void soluong()
{
tong=0;
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
dem=dem+1;//dem so phan tu chan
}
}
printf("So phan tu chan: %d \n",dem);
printf("So phan tu le: %d \n",n-dem);
}
// chuong trinh tim max min va vi tri cua chung ---------------------
void maxmin()
{
max=a[0];
for(i=0;i<10;i++)
{
if(max<a[i])
{
max=a[i];
vitri=i;
}
}
// xuat
printf("Max:%d tai vi tri a[%d]",max,vitri);
}
// chuong trinh sap xem tang giam cac phan tu trong mang---------------------
void sapxep()
{
for(i=0;i<N-1;i++)
{
for(int j=i+1;j<N;j++)
{
if (a[i]>a[j])
{
tam=a[i];
a[i]=a[j];
a[j]=tam;
}
}
}
printf("mang moi sap xep la: ");
for (i=0; i<N;i++)
{
printf("%d ",a[i]);
}
}
//chuong trinh thoat------------------------------------------
void thoat()
{
exit(0);
}
//chuong trinh chinh-------------------------------------------
void main()
{
nhap();
chon_mode();
switch (mode)
{
case 0: thoat(); break;
case 1: tinhtong(); break;
case 2: soluong(); break;
case 3: maxmin(); break;
case 4: sapxep(); break;
}
getch();
}