/*Nhap MAng 2 Chieu Bang De Quy*/
#include<iostream>
using namespace std;
void Nhap(int **a,int dong,int cot,int i=0)
{
if(i==dong*cot)
{
return ;
}
cout<<"nhap a["<<i/cot<<"]["<<i%cot<<"]=";cin>>a[i/cot][i%cot];
return Nhap(a,dong,cot,i+1);
}
void Xuat(int **a,int dong,int cot,int i=0)
{
if(i==dong*cot)
{
return ;
}
cout<<"\t"<<a[i/cot][i%cot];
if((i+1)%cot==0)
{
cout<<endl;
}
return Xuat(a,dong,cot,i+1);
}
int TinhTong(int **a,int dong,int cot,int i=0,int tong=0)
{
if(i==dong*cot)
{
return tong;
}
return TinhTong(a,dong,cot,i+1,tong+a[i/cot][i%cot]);
}
int TimMax(int **a,int dong,int cot,int i=0,int max=0)
{
if(i==dong*cot)
{
return max;
}
max=max>a[i/cot][i%cot]?max:a[i/cot][i%cot];
return TimMax(a,dong,cot,i+1,max);
}
int TimMin(int **a,int dong,int cot,int i=0,int min=INT_MAX)
{
if(i==dong*cot)
{
return min;
}
min=min<a[i/cot][i%cot]?min:a[i/cot][i%cot];
return TimMin(a,dong,cot,i+1,min);
}
int DemSoLuongSoChan(int **a,int dong,int cot,int i=0,int diem=0)
{
if(i==dong*cot)
{
return diem;
}
if(a[i/cot][i%cot]%2==0)
diem++;
return DemSoLuongSoChan(a,dong,cot,i+1,diem);
}
// cách 2
//int DemSoLuongSoChan(int **a,int dong,int cot,int i=0,int diem=0)
//{
//if(i==dong*cot)
//{
//return diem;
//}
//return DemSoLuongSoChan(a,dong,cot,i+1,a[i/cot][i%cot]%2==0?diem+1:diem);
//}
void HoanVi(int &a,int &b)
{
int c=a;
a=b;
b=c;
}
void XapTangDan(int **a,int dong,int cot ,int i=0)
{
if(i==dong*cot)
{
return;
}
for(int k=i+1;k<dong*cot;k++)
{
//xap tang dan
if(a[i/cot][i%cot]>a[k/cot][k%cot])
{
HoanVi(a[i/cot][i%cot],a[k/cot][k%cot]);
}
}
return XapTangDan(a,dong,cot,i+1);
}
void XapDamDan(int **a,int dong,int cot ,int i=0)
{
if(i==dong*cot)
{
return;
}
for(int k=i+1;k<dong*cot;k++)
{
//xap Dam Dan
if(a[i/cot][i%cot]<a[k/cot][k%cot])
{
HoanVi(a[i/cot][i%cot],a[k/cot][k%cot]);
}
}
return XapDamDan(a,dong,cot,i+1);
}
int main()
{int dong=3,cot=3;
// cấp phát con trỏ cấp cao
int **a=new int*[dong];
for(int i=0;i<dong;i++)
{
a[i]=new int[cot];
}
Nhap(a,dong,cot);
Xuat(a,dong,cot);
cout<<"\ntong la: \t"<<TinhTong(a, dong, cot);
cout<<"\nMax La: "<<TimMax(a,dong,cot);
cout<<"\nMin La: "<<TimMin(a,dong,cot);
cout<<"\nSo Luong Phan Tu Chan La \t" <<DemSoLuongSoChan(a,dong,cot);
XapTangDan(a,dong,cot);// giải phóng con trỏ cấp cao
cout<<"\nMang Sau Khi Xap Xep Tang Dan La: \n";
Xuat(a,dong,cot);
XapDamDan(a,dong,cot);
cout<<"\nMang Sau Khi Xap Xep Dam Dan La: \n";
Xuat(a,dong,cot);
for(int i=0;i<dong;i++)
{
delete[] a[i];
}
delete[] a;
cout<<endl;
system("pause");
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.