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



文件信息
文件来源  
文件作者  
更新时间 2005-5-8 10:44:38 
添加编辑 viewsnake 

辅助信息
打印功能 打印本文
背景颜色 杏黄 秋褐 胭红 芥绿 天蓝 雪青 炭灰 奶白
字体大小 特大号字 大号字 中号字 小号字
免责声明 本网站所有文章均来自网络,仅提供预览形式,不提供纸张形式,若涉及到版权的文章,请购买正版,毕竟在电脑上看也不舒服啊,呵呵,这是viewsnake个人网站,纯粹交流学习资料的地方。无商业行为。
选择更多免费考研资料:
阅读正文内容
//********************
//*        7.6       *
//********************
# include <iostream.h>

int m[5][4]={{65,68,54,69},{62,82,83,81},{91,97,82,74},{75,32,91,72},{83,71,82,91}};

void top(int a[][4],int i,int j);
void fail(int b[][4],int i,int j);
void ave(int c[][4],int i,int j);

void main()
{
        top(m,5,4);
        fail(m,5,4);
        ave(m,5,4);
}

void top(int a[][4],int i,int j)
{
        int k,l;
        int x,y,z;
        for(k=0;k<j;k++)
        {
                z=a[0][k];x=0;y=k;
                for(l=0;l<i;l++)
                {
                        if(a[l][k]>z)
                        {x=l;y=k;z=a[l][k];}
                }
                cout<<"对第"<<k<<"门课程,成绩最高的是第"<<x<<"同学,成绩为:"<<z<<endl;
        }
}       
void fail(int b[][4],int i,int j)
{
        cout<<endl;
        int k,l;

        for(k=0;k<i;k++)
        {
                int x=0;
                for(l=0;l<j;l++)
                {
                        if(b[k][l]<60)
                                x=1;
                }
                if (x==1)
                {
                        cout<<"第"<<k<<"个同学有不及格课程,其各科成绩为:";
                        for(int y=0;y<j;y++)
                        {
                                cout<<b[k][y]<<",";
                        }

                }
                cout<<endl;
        }
}
void ave(int c[][4],int i,int j)
{
        cout<<endl;
        int k,l;

        for(k=0;k<j;k++)
        {
                int z=0;
                for(l=0;l<i;l++)
                {
                        z=z+c[l][k];
                }
                cout<<"对第"<<k<<"门课程,平均成绩为:"<<z/i<<endl;
        }
}


//********************
//*        7.7       *
//*                  *
//*   9   5   11     *
//*   5   7    8     *
//*   9   0    3     *
//*                  *
//********************
# include <iostream.h>
# include <iomanip.h>

int a[3][3]={5,7,8,2,-2,4,1,1,1};

int b[3][3]={4,-2,3,3,9,4,8,-1,2};

void main()
{
        int c[3][3];
        for(int i=0;i<3;i++)
        {
                for(int j=0;j<3;j++)
                {
                        c[j]=a[j]+b[j];
                }
        }
        for(int x=0;x<3;x++)
        {
                for(int y=0;y<3;y++)
                {
                        cout<<setw(4)<<c[x][y];
                }
                cout<<endl;
        }
}

第8章

//********************
//*        8.1       *
//*         1        *
//*     0X0012FF5C   *
//*         91       *
//********************
# include <iostream.h>
int * findmax(int * array,int size,int * index);
void main()
{
        int a[10]={33,91,54,67,82,37,85,63,19,68};
        int * maxaddr;
        int idx;
        maxaddr=findmax(a,sizeof(a)/sizeof(*a),&idx);

        cout<<"the index of maximum element is"<<" "<<idx<<endl
                <<"the address of it is"<<" "<<maxaddr<<endl
                <<"the value of it is"<<" "<<a[idx]<<endl;
}

int * findmax(int * array,int size,int * index)
{
        int * temp=array;
        int j;
        for(j=0;j<size;j++)
        {
                if(*(array+j)>*temp)
                {
                        temp=array+j;
                        * index=j;
                }
        }
        return temp;
}


//********************
//*        8.2********
//********************
# include <iostream.h>

int search(int num=10,int interval=3);
void main()
{
        int num1;
        int interval1;

        while(1)
        {
                cout<<"please input the numbers of boys:";
                cin>>num1;
                cout<<"please input the interval:";
                cin>>interval1;
                if((num1<1) || (interval1<1)||(interval1>num1))
                {
                        int i;
                        cout<<endl<<"please choice:"<<endl;
                        cout<<"other number---retry"<<" "<<"0---exit:"<<endl;
                        cin>>i;
                        if(i=0)
                                exit(1);
                }
                else
                        break;
        }

        int n=search(num1,interval1);
        cout<<"\nNo."<<n<<"Boy's won.\n";

}

int search(int num,int interval)
{

        int * a=new int[num];


        for(int j=0;j<num;j++)
                a[j]=j+1;


        for(int k=0;k<num;k++)
                cout<<*(a+k)<<",";
        cout<<endl;

        int l=1;
        int m=-1;

        while(1)
        {
                for(int n=0;n<interval
                {
                        m=(m+1)%num;                       
                        if(*(a+m)!=0)
                                n++;
                }


                if(l==num)
                {
                        return m;
                        break;
                }

                cout<<*(a+m)<<endl;
                *(a+m)=0;

                l++;
        }
        delete []a;
}


//********************
//*     8.3.(1)      *
//********************
# include <iostream.h>
# include <stdlib.h>

int compare(const void * a,const void * b);
int list1[10]={12,32,42,51,8,16,51,21,19,9};
int * list[10]={& list1[0],& list1[1],&list1[2],&list1[3],&list1[4],&list1[5],&list1[6],&list1[7],&list1[8],&list1[9]};

void main()
{
        qsort((void *) list,10,sizeof(list[0]),compare);
        for(int i=0;i<10;i++)
        {
                cout<<*list<<endl;
        }
}
int compare(const void * a,const void * b)
{
        int temp1=**(int**)a;
        int temp2=**(int**)b;
        temp1=temp1/10+(temp1-((temp1/10)*10));
        temp2=temp2/10+(temp2-((temp2/10)*10));
        if(temp1<temp2)
                return -1;
        else
        {
                if(temp1==temp2)
                        return 0;
                else
                        return 1;
        }
}



<<<返回上一页 <<<返回网站首页
<<<您的位置:首页>考研经验>考研笔记>计算机工程笔记>正文