| | | | |
|
|
Tiêu đề: Bài tập về mảng 1 chiều.!!! | |
| | | | | | - Code:
-
#include<iostream> #include<conio.h> #include<time.h> #include<stdlib.h> #define N 20 void NhapMang (int a[],int n); void XuatMang (int a[],int n); void tongchanle(int a[],int n); void timx(int a[],int n,int x); void timmax(int a[],int n); void kttangdan(int a[],int n); void main() { int n,a[N],x; cout<<"Nhap kich thuoc mang: "; cin>>n; NhapMang(a,n); cout<<"Cac gia tri cua mang vua nhap: "<<endl; XuatMang(a,n); tongchanle(a,n); cout<<"\nNhap X: "; cin>>x; timx(a,n,x); timmax(a,n); cout<<"Kiem tra mang tang dan:"; kttangdan(a,n); getche(); } void NhapMang (int a[],int n) { srand(time(NULL)); int i; for(int i=0;i<n;i++) a[i]=rand()%10; } void XuatMang (int a[],int n) { for(int i=0;i<n;i++) cout<<a[i]<<"\t"; } void tongchanle(int a[],int n) { int i,tc,tl; tc=tl=0; for(i=0;i<n;i++) if(a[i]%2==0) tc +=a[i]; else tl +=a[i]; cout<<"\nTong chan la: "<<tc<<endl; cout<<"Tong le la: "<<tl; } void timx(int a[],int n,int x) { int i,f=0; for(i=0;i<n;i++) if(a[i]==x) f=1; if(f==1) cout<<"Co "<<x<<" trong mang"; else cout<<"Khong co "<<x<<" trong mang"; } void timmax(int a[],int n) { int m=a[0]; for(int i=1;i<n;i++) if(m<a[i]) m=a[i]; cout<<"\nSo lon nhat la: "<<m<<"\n"; } void kttangdan(int a[],int n) { int i; for(i = 0; i<n-1;i++) if(a[i]>a[i+1])break; if(i==n-1) cout<<"\nMang tang dan"; else cout<<"\nMang khong tang dan"; } | | | | |
|
|