钱能C++教材习题答案八

   /2005-05-08

//*************
//*     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;
}


相关话题/

  • 领限时大额优惠券,享本站正版考研考试资料!
    大额优惠券
    优惠券领取后72小时内有效,10万种最新考研考试考证类电子打印资料任你选。涵盖全国500余所院校考研专业课、200多种职业资格考试、1100多种经典教材,产品类型包含电子书、题库、全套资料以及视频,无论您是考研复习、考证刷题,还是考前冲刺等,不同类型的产品可满足您学习上的不同需求。 ...
    本站小编 Free壹佰分学习网 2022-09-19