78)}【解答】1361015259140481300712000110000I.#includeusingnamespacestd;intf(int[],int);intmainO{inta口={-1,3,5,-7,9,-11)cout?f(a,6)?endl;}intf(inta口,intsize){inti,t=l;for(i=0;i0)t*=a[i];returnt;i.【解答】135i.^includeusingnamespacestd;intf(int[][3],int,int);intmain(){inta[][3]={0,1,2,3,4,5,6,7,8}cout?f(a,3,3)?endl;}intf(inta[][3],introw,intcol){inti,j,t=l;for(i=0;i79#includeusingnamespacestd;voidtestl(int*al){al=newint(5);cout?**al=*?*al?endl;}voidtest2(int*&a2){a2=newint(5);cout?”*a2=*<<*a2?endl;}intmain(){int*p=newint(1);testl(p);cout?”testl:*pl="<<*p?endl;test2(p);cout<<*test2:*p2=*?*p?endl;【解答】*al=5testl:*pl=1*a2=5test2:*p2=51.#includeusingnamespacestd;intmain(){chars[]=*abccda*;inti;charc;for(i=1;(c=s[i])!='\0';i++){switch(c){case*a*:cout<<'%';continue;case'b':cout<<'$';break;case'c':cout?***;break;case'd':continue;)cout??endl;))【解答】$#
80%8.#includeusingnamespacestd;intmainO{char*str[]={"c++”,"basic?!眕ascal”}char**p;inti;p=str;for(i=0;i<3;i++)cout?*(p+i)?endl;1【解答】c++basicpascal9.^includeusingnamespacestd;intmainO{charsi[]="Fortran",s2[]=*Foxpro*;char*p,*q;p=si;q=s2;whi1e(*p&&*q){if(*p==*q)cout?*p;p++;q++;)cout?endl;)【解答】For10.#include#includeusingnamespacestd;intmainO{charstr[][10]={"vb","pascal","c++”},s[10];strcpy_s(s,(strcmp(str[0],str[l])<0?str[0]:str[l]));if(strcmp(str[2],s)<0)strcpy_s(s,str[2]);cout<81數(shù)組說明語句要向編譯器提供數(shù)組名(標識符),數(shù)組元素得類型、數(shù)組維數(shù)、數(shù)組長度(元素得個數(shù))等信息。一維數(shù)組說明語句為:類型數(shù)組名[表達式]二維數(shù)組說明語句為:類型數(shù)組名[表達式][表達式2]1.數(shù)組名、數(shù)組元素得區(qū)別就是什么?歸納一維數(shù)組元素地址、元素值不同得表示形式。若有說明intaa[3],*pa=aa;請使用aa或pa,寫出3個以上與aa[2]等價得表達式?!窘獯稹繑?shù)組名就是一個標識符,執(zhí)行代碼中代表數(shù)組得地址,即指向數(shù)組起始位置得指針;而數(shù)組元素就是下標變量,性質(zhì)相當于普通變量。對一維數(shù)組aa第i個元素得地址可以表示為:&aa[i]aa+i;對一維數(shù)組aa第i個元素得值可以表示為:a[i]*(a+i);與aa[2]等價得表達式:*(aa+2)*(&a[2])*(pa+2)pa[2]3.要把一維數(shù)組inta[m*n]得元素傳送到二維數(shù)組intb[m][n]中,即在程序中要執(zhí)行b[i][j]=a[k];請寫出k^i,j得下標變換公式,并用程序驗證?!窘獯稹哭D(zhuǎn)換公式:i=k/nj=k%n驗證程序:#includeusingnamespacestd;intmain(){constintM=3,N=4;intk,a[M*N]={1,2,3,4,5,6,7,8,9,10,11,12},b[M][N];inti,j;cout<<*arraya:*<\t';cout<823.有以下函數(shù)voidquery(){int*p;p=newint[3];//……delete[]p;p=newdouble[5];//……delete[]p;出現(xiàn)了編譯錯誤。請分析錯誤得原因,并把上述程序補充完整,上機驗證您得判斷?!窘獯稹吭谡Z句P』ewdouble[5];中企圖把動態(tài)浮點型數(shù)組得地址寫入整型指針p,造成錯誤。錯誤為ec*orC2440:“二":無法從“double*"轉(zhuǎn)換為“int*”。改正方法:增加一個double*q指針。voidquery(){int*p;p=newint[3];delete口p;//……double*q;q二newdouble[5];delete[]q;}4.有以下程序根據(jù)輸入值,設計功能就是調(diào)用函數(shù)create建立并初始化動態(tài)數(shù)組,令a[i]=i。但該程序運行后不能得到期望結(jié)果,請分析程序得錯誤原因并修改之。4includeusingnamespacestd;voidcreate(int*,int);intmain(){int*a=NULL,len;cin?len;create(a,len);for(inti=0;i83voidcreate(int*ap,intn){ap=newint[n];for(inti=0;i#includeusingnamespacestd:voidaveMsd(double[],int,double&,double&);〃求平均值與均方差值函數(shù)intmainO{doubles[]={76,85,54,77,93,83,90,67,81,65);doubleave,msd;inti,n;n=sizeof(s)/sizeof(double);〃求數(shù)組元素得個數(shù)cout<<”學生成績:”;for(i=0;i84for(i=0;i#include#include#includeusingnamespacestd;intmain(){inta[10],i,j;srand(int(time(O)));〃為隨機數(shù)生成器設置種子值for(i=0:i<10;i++){1:a[i]=randO;〃產(chǎn)生隨機數(shù)存放到數(shù)組中if(a[i]<10||a[i]>=100)〃獲取指定范圍數(shù)據(jù)goto1;for(j=0;jm)cout<usingnamespacestd;
85intmain(){inta[]={38,6,29,1,25,20,6,32,78,10};intindex[101;〃記錄下標得數(shù)組inti,j,temp;for(i=0;i<10;i++)index[i]=i;for(i=0;i<=8;i++)for(j=i+l;j<=9;j++)
86if(a[i]usingnamespacestd;intmain()intb[10],i,j,k,flaglongnum,n;cout<<*num=*;k=0;n=num;do{b[k++]=n%10;n=n/10;}while(n!=0);flag=l;i=0;j=k-l;while(iusingnamespacestd;voidmerge(constinta[],intna,constintb[],intnb,intc[],intnc);intmain(){inta[4]={1,2,5,7};
87intb[8]={3,4,8,8,9,10,11,12);intc[12];merge(a,4,b,8,c,12);for(i=0;i<12;i++)cout?c[i]?*cout<b[j]){c[k]=b[j];k++;j++;}else{c[k]=a[i];k++;i++;}whi1e(iusingnamespacestd;intmain(){char*weekday[7]={"sunday",*monday*,*tuesday*,“Wednesday",*thursday*,*friday*,“Saturday");intd;cout<<"pleaseinputweekday:cin?d;if(d>=0&&d<=6)cout<88#includeusingnamespacestd;voidcreate(int*&app,intn);voiddel(int*&app,int*&bpp,intn);intmaindiagonal(int*&app,intn);voidoutput(int*&app,int);intmain(){int*ap=NULL,*bp=NULL,n;cout?"輸入矩陣得階:々cin?n;create(ap,n);cout?”
89形成矩陣:
90";output(ap,n);cout?”去掉靠邊元素生成得矩陣:
91";del(ap,bp,n);output(bp,n-2);cout?”主對角線元素之與:“?maindiagonal(ap,n)?endl;)〃形成n階矩陣函數(shù)voidcreate(int*&app,intn){app=newint[n*n];inti,j,k=0;for(i=0;i92{bpp[k]=*(app+i*n+j);k++;)))〃求主對角線元素之與函數(shù)intmaindiagonal(int*&app,intn){inti,j,k=0,sum=0:for(i=0;iusingnamespacestd;intmain(){longs[][3]={{300,250,150},{200,240,200},{280,210,180});longp[]=(3500,3300,3800);
93inti,j;doublesum;for(i=0;i<3;i++){sum=0;for(j=0;j<3;j++)sum+=s[i][j]*p[j];cout?"第"?i+1?”百貨公司得電視機營業(yè)額:”?sum?endl;})8.設計函數(shù)求一整型數(shù)組得最小元素及其下標。在主函數(shù)中定義與初始化該整型數(shù)組,調(diào)用該函數(shù),并顯示最小元素值與下標值?!窘獯稹?includeusingnamespacestd:intfmin(int[],int);intmainO{inta[]={73,85,62,95,77,56,81,66,90,80);intindex;index=fmin(a,sizeof(a)/sizeof(int));cout<<"Theminnumnumberis:*?a[index]?endl;cout<<"Theindexis:"<usingnamespacestd;voidinsert(inta[],int,int);intmain(){inta[]={10,12,23,25,48,48,53,58,60,78};
94intx,n,i;
95cout<<*pleaseinputinsertcin>>x;n=sizeof(a)/sizeof(int);insert(a,n,x);for(i=0;i=p;j)a[j]=a[j-l]:data:〃求數(shù)組長度〃插入元素〃查找插入位置〃后移元素,擠出最大值〃插入元素8.編寫程序,按照指定長度生成動態(tài)數(shù)組,用隨機數(shù)對數(shù)組元素賦值,然后逆置該數(shù)組元素。例如,對數(shù)組A[5],初值為{6,3,7,8,2),逆置后得值就是{2,8,7,3,6).程序輸出逆置前、后得數(shù)組元素序列?!窘獯稹?include#include#includeusingnamespacestd;voidprintarray(int*p,intn);voidadverse(int*p,intn);intmainO{int*p,n,i;cin?n;p=newint[n];srand(time(0));for(i=0;i96cout<<"逆置數(shù)組:“;printarray(p,n);//輸出逆置數(shù)組}//輸出數(shù)組函數(shù)voidprintarray(int*p,intn){inti;for(i=0;iusingnamespacestd;intmain(){intp,i,j,k;charsi[40],s2[40],s3[80];cout<<“si=";cin?si;cout<<*s2=*;cin?s2;cout<<*inputinsertposition:*;cin?p;〃輸入插入位置for(i=0;i97s3[i+j]=s2[j];for(k=p;si[k]!=>\0*;k++)s3[j+k]=si[k];s3[j+k]='\0';cout?"s3二"?S3?endl;}8.把某班學生得姓名與學號分別存放到兩個數(shù)組中,從鍵盤上輸入某一學生學號,查找該學生就是否在該班,若找到該學生,則顯示出相應得姓名?!窘獯稹?includeusingnamespacestd;intmain(){charname[5][20]={"liming*,“zhangqing”,*liuxiaoping”,*wangying”,*lupei"};longnum[5]={20030001,20030002,20030005,20030007,20030010};inti;longsnumber;cout<<*pleaseinputstudentnumber:*;cin?snumber;for(i=0;i<5;i++){if(num[i]=snumber){cout?”Found!Thenameis:“?name[i]?endl;break;}}if(i==5)cout?*Can\*tfound!*?endl;)9.將一組C++關鍵字存放到一個二維數(shù)組中,并找出這些關鍵字得最小者?!窘獯稹?include^includeusingnamespacestd;intmain(){charstring[10];charstr[][10]={"while","break","if","extern","void","auto",long,static,do,constj;inti;strcpy(string,str[0]);for(i=0;i<10;i++)if(strcmp(str[i],string)<0)strcpy(string,str[i]);cout<<*Theminimumstringis:"?string?endl;)10.使用指針函數(shù)編寫程序,把兩個字符串連接起來。
98【解答】#includeusingnamespacestd;char*strcat(char*strl,char*str2){char*p=strl;while(*p!='\0')p++;*p=*str2;do{p++;str2++;*p=*str2;}while(*str2!='\0');return(strl);)intmain(){charstrl[80],str2[80];cout<<*inputstrl:cin?strl;cout<<"inputstr2:*;cin?str2;cout?*strl+str2=*?strcat(strl,str2)<x=1;p、y=2;(d)p={1,2};2.已知有職工情況結(jié)構(gòu)變量emp定義為:structDate{intyear;intmonth;intday;};strnctEmployee{charname[20];longcode;Datebirth};Employeeemp;下列對cmp得birth得正確賦值方法就是(d)o(a)year=1980;month=5;day=l;
99(b)birth、year=1980;birth、month=5;birth、day=l;(c)emp>year=1980;emp、month=5;emp>day=l;(d)emp、birth、year=1980;emp、birth、month=5;emp、birth^day=l;2.假定有以下說明語句,則下面引用形式錯誤得就是(b)ostructStudent{intnum;doublescore;};Studentstu[3]={{1001,80),{1002,75),{1003,91}},*p=stu;num(a)p->num(b)(p++)、num(c)(p++)->num(d)(*p)、3.若有以下說明語句,則下列錯誤得引用就是(d)?structWorker{intno;charname[20];};Workerw,*p=&w;(a)w、no(b)p->no(c)(*p)、no(d)*p、no5.si與s2就是兩個結(jié)構(gòu)類型變量,若要賦值sl=s2合法,則它們得說明應該就是(c)o(a)si只能接受相同類型得數(shù)據(jù)成員(b)結(jié)構(gòu)中得成員相同(c)同一結(jié)構(gòu)類型得變量(d)存儲字節(jié)長度一樣得變量5、2閱讀下列程序,寫出執(zhí)行結(jié)果。1.itincludeusingnamespacestd;structData{intn;doublescore;intmainO{Dataa[3]={1001,87,1002,72,1003,90),*p=a;cout<<(p++)->n?endI;cout<<(p++)->n?endl;cout?p->n++?endl;cout?(*p)、n++?endl;)【解答】10011002100310042.^includeusingnamespacestd;structEmployee{charname[20];
100charsex;);voidfun(Employee*p){if((*p)、sex='m')cout?(*p)>name?endl;}intmainO{Employeeemp[5]={"Liming",'m',^Wangxiaoping*,'f',"Luwei”,*m*}inti;for(i=0;i<3;i++)fun(emp+i);}【解答】LimingLuwei1.#includeusingnamespacestd;structNode{char*s;Node*q;intmainO{Nodea[]={{"Mary”,a+1},{"Jack",a+2},{“Jim”,a}}Node*p=a;cout<s<q->s?endl;cout?p->q->q->s?endl;cout?p->q->q->q->s?endl;【解答】MaryJackJimMary
|