//* 8.8 *
//*************
# include <iostream.h>
# include <math.h>
# include <D3DRMDEF.H>
# include <iomanip.h>
long a[25];
long * turn(long * x,int y);
void main()
{
int i;
for(i=1;i<25;i++)
a=rand();
for(int l=1;l<25;l++)
{
cout<<setw(10)<<a[l-1]<<",";
if(l%5==0)
cout<<endl;
}
cout<<endl<<endl;
long * b=turn(a,25);
for(int m=1;m<25;m++)
{
cout<<setw(10)<<b[m-1]<<",";
if(m%5==0)
cout<<endl;
}
cout<<endl<<endl;
}
long * turn(long * x,int y)
{
long * m=new long[y];
for(int j=0;j<y;j++)
{
m[j]=x[(j%5)*5+j/5];
}
return m;
}
//******************************
//* 8.9 *
//*i=615852/cability:246341200 *
//******************************
# include <iostream.h>
void main()
{
int m;
int * p;
long i=0;
do
{
p=new int[100];
++i;
cout<<i<<",";
}while(p != NULL);
m=sizeof(int)*100*(i+1);
cout<<"cability:"<<m<<endl;
}
第9章
//*************
//* 9.1 *
//*************
//(1)
# include <iostream.h>
const size=10;
void findmax(int * & a,int n, int i,int & pk);//
void main()
{
int a[size];
int n=0;
cout<<"please input"<<size<<"datas:/n";
for(int i=0;i<size;i++)
{
cin>>a;
}
int * p=a;//
int * & q=p;//
findmax(q,size,0,n);
cout<<"the maximum is"<<a[n]<<endl
<<"Its index is"<<n<<endl;
}
void findmax(int * & a,int n, int i,int & pk)//
{
if(i<n)
{
if(a>a[pk])
pk=i;
findmax(a,n,i+1,pk);//
}
}
//(2)在输入10个int数后,通过findmax查找最大数及其位置,并打印。
//(3)
# include <iostream.h>
const size=10;
void findmax(int * & a,int m,int & pk);//
void main()
{
int a[size];
int n=0;
cout<<"please input "<<size<<" datas:/n";
for(int i=0;i<size;i++)
{
cin>>a;
}
int * p=a;//
int * & q=p;//
findmax(q,size,n);
cout<<"the maximum is: "<<a[n]<<endl
<<"Its index is: "<<n<<endl;
}
void findmax(int * & a,int m,int & pk)//
{
for(int j=0;j<m;j++)
{
if(a[j]>a[pk])
pk=j;
}
}
//*************
//* 9.2 *
//*************
# include <iostream.h>
int & put(int n);
int get(int n);
int vals[10];
int error=-1;
void main()
{
put(0)=10;
put(1)=20;
put(9)=30;
cout<<get(0)<<endl;
cout<<get(1)<<endl;
cout<<get(9)<<endl;
put(12)=1;
}
int & put(int n)
{
if((n<0)||(n>9))
{
error=1;
cout<<"range error in put() value!";
return error;
}
else
return vals[n];
}
int get(int n)
{
int m=vals[n];
return m;
}
//*************
//* 9.3 *
//*************
# include <iostream.h>
void exchange(char * & a,char * & b);
char* ap="hello";
char* bp="how are you?";
void main()
{
cout<<ap<<endl;
cout<<bp<<endl;
exchange(ap,bp);
cout<<ap<<endl;
cout<<bp<<endl;
}
void exchange(char * & a,char * & b)
{
char * p=a;
a=b;
b=p;
}