- 243.33 KB
- 2022-04-22 11:28:39 发布
- 1、本文档共5页,可阅读全部内容。
- 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
- 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
- 文档侵权举报电话:19940600175。
'《C++程序设计基础》(第4版)(上)习题与解答第1章基本数据与表达式1.1选择题1.一个最简单的C++程序,可以只有一个(C)。(A)库函数(B)自定义函数(C)main函数(D)空函数2.用C++语言编写的源程序要成为目标程序必须要经过(D)。(A)解释(B)汇编(C)编辑(D)编译3.C++程序中的简单语句必须以(B)结束。(A)冒号(B)分号(C)空格(D)花括号4.假设有说明inta=0;doublex=5.16;,则在以下语句中,(C)属于编译错误。(A)x=a/x;(B)x=x/a;(C)a=a%x;(D)x=x*a;5.执行C++程序时出现的“溢出”错误属于(C)错误。(A)编译(B)连接(C)运行(D)逻辑6.在下列选项中,全部都是C++关键字的选项为(C)。(A)whileIFStatic(B)breakchargo(C)sizeofcaseextern(D)switchfloatinteger7.按C++标识符的语法规定,合法的标识符是(A)。(A)_abc(B)new(C)π(D)"age"8.在C++语句中,两个标识符之间(A)不能作为C++的分隔符。(A)数字(B);(C):(D)+9.下列正确的八进制整型常量表示是(B)。(A)0a0(B)015(C)080(D)0x1010.下列错误的十六进制整型常量表示是(C)。(A)0x11(B)0xaf(C)0xg(D)0x1f11.在下列选项中,全部都合法的浮点型数据的选项为(B)。(A)-1e3.515.2e-4(B)12.34-1e+50.1E-12(C)0.2e-2-12345.e-5(D)5.0e(1+4)0.18e+212.在下列选项中,正确的字符常量为(D)。(A)"a"(B)"name"(C)a(D)"101"13.在下列选项中,(D)不能交换变量a和b的值。(A)t=b;b=a;a=t;(B)a=a+b;b=a-b;a=a–b;(C)t=a;a=b;b=t;(D)a=b;b=a;14.关于下列语句,叙述错误的是(A)。
inti=10,*p=&i;(A)p的值为10(B)p指向整型变量i(C)*p表示变量i的值(D)p的值是变量i的地址15.有以下变量说明,下面不正确的赋值语句是(B)。inta=5,b=10,c;int*p1=&a,*p2=&b;(A)*p2=b;(B)p1=a;(C)p2=p1;(D)c=*p1*(*p2);16.有以下变量说明,下面正确的语句是(B)。inta=10,b;int&pa=a,&pb=b;(A)&pb=a;(B)pb=pa;(C)pb=&pa;(D)*pb=*pa;17.执行下面语句序列后,a和b的值分别为(B)。inta=5,b=3,t;int&ra=a;int&rb=b;t=ra;ra=rb;rb=t;(A)3和3(B)3和5(C)5和3(D)5和518.在下列运算符中,(D)优先级最高。(A)<=(B)*=(C)+(D)*19.在下列运算符中,(D)优先级最低。(A)!(B)&&(C)!=(D)?:20.已知inti=1,j=2;,则表达式i+++j的值为(C)。(A)1(B)2(C)3(D)421.已知inti=1,j=2;,则表达式++i+j的值为(D)。(A)1(B)2(C)3(D)422.在下列表达式选项中,(C)是正确。(A)++(a++)(B)a++b(C)a+++b(D)a++++b23.已知inti=0,j=1,k=2;,则逻辑表达式++i||- -j&&++k的值为(B)。(A)0(B)1(C)2(D)324.执行下列语句后,x的值是(D),y的值是()。intx,y;x=y=1;++x||++y;(A)不确定(B)0(C)1(D)225.设x为整型变量,不能正确表达数学关系1<x<5的C++逻辑表达式是(A)。(A)1=5)26.已知intx=5;,执行下列语句后,x的值为(C)。x+=x-=x*x;(A)25(B)40(C)–40(D)2027.设inta=1,b=2,c=3,d=4;,则以下条件表达式的值为(A)。a=-100&&y<=-10)&&!(y>=10&&y<=100)6.sqrt(pow((x-10),2)+pow((y-20),2))<357.a+b>c&&b+c>a&&c+a>b8.(year%4==0)&&(year%100!=0)||(year%400==0)1.4阅读下列程序,写出运行结果1.#includeusingnamespacestd;
intmain(){inta=1,b=2;boolx,y;cout<<(a++)+(++b)<b;y=a--&&b;cout<usingnamespacestd;intmain(){intx,y,z,f;x=y=z=1;f=--x||y--&&z++;cout<<"x="<#includeusingnamespacestd;intmain(){inta=123;int&ra=a;int*pa=&a;cout<usingnamespacestd;intmain(){inta,b,c;cout<<"a,b,c=";cin>>a>>b>>c;//对普通数据类型变量赋值int*pa=&a,*pb=&b,*pc=&c;//用变量地址值初始化指针变量cout<<"a,b,c="<usingnamespacestd;intmain(){inta;cout<<"a=";cin>>a;intra=a;int*pa=&a;cout<<"a的值:"<usingnamespacestd;intmain(){inta;cout<<"a=";cin>>a;a=a+5;//名访问cout<<&a<a&&a>x(2)(a+b)!=x(3)a||(b+x)(4)a==(b*x)(5)a-busingnamespacestd;intmain(){inta=10,b=20;int*p=&a,*q=&b;*p=*p**q;int&ra=a;ra=a;int*&rt=q;*rt=30;cout<<"a="<usingnamespacestd;intmain(){inta=10,b=20;int*p=&a,*q=&b;//“*”是指针类型说明符,“&”是取址运算符*p=*p**q;//第1、2、4个“*”是间址访问符,第3个“*”算术乘运算符int&ra=a;//“&”是引用说明符ra=a;int*&rt=q;//“*”是指针类型说明符,“&”是引用说明符*rt=30;//“*”是间址访问符//输出语句中的“*”是间址访问符cout<<"a="<usingnamespacestd;intmain(){intx,i,j,k;cout<<"pleaseinputx:";cin>>x;i=x/100;j=x/10%10;k=x%10;cout<usingnamespacestd;intmain(){doublex,y,b;cout<<"pleaseinputx,y:";cin>>x>>y;b=(-2<=x)&&(x<=2)&&(-2<=y)&&(y<=2);cout<usingnamespacestd;intmain(){inta,b,c,temp,min;cout<<"pleaseinputa,b,c:";cin>>a>>b>>c;temp=(a=0)if(x!=0)if(x==0)y=0;if(x>0)y=1;elsey=-1;elsey=0(C)if(x<=0)(D)y=-1;if(x<0)y=-1;if(x<=0)elsey=0;if(x<0)y=-1;elsey=1;elsey=0;3.假设i=2,执行下列语句后i的值为(B)。switch(i){case1:i++;case2:i--;case3:++i;break;case4:--i;default:i++;}(A)1(B)2(C)3(D)44.已知inti=0,x=0;,在下面while语句执行时循环次数为(D)。while(!x&&i<3){x++;i++;}(A)4(B)3(C)2(D)15.已知inti=3;,在下面do_while语句执行时的循环次数为(B)。do{i--;cout<=0);(C)inta=5;while(a){a--;};
(D)inti=3;for(;i;i--);2.2阅读下列程序,写出运行结果1.#includeusingnamespacestd;intmain(){inta,b,c,d,x;a=c=0;b=1;d=20;if(a)d=d-10;elseif(!b)if(!c)x=15;elsex=25;cout<usingnamespacestd;intmain(){inta=0,b=1;switch(a){case0:switch(b){case0:cout<<"a="<
usingnamespacestd;intmain(){inti=1;while(i<=10){if(++i%3!=1)continue;elsecout<usingnamespacestd;intmain(){inti=0,j=5;do{i++;j--;if(i>3)break;}while(j>0);cout<<"i="<usingnamespacestd;intmain(){inti,j;for(i=1,j=5;iusingnamespacestd;
intmain(){inti,s=0;for(i=0;i<5;i++)switch(i){case0:s+=i;break;case1:s+=i;break;case2:s+=i;break;default:s+=2;}cout<<"s="<usingnamespacestd;intmain(){inti,j,x=0;for(i=0;i<=3;i++){x++;for(j=0;j<=3;j++){if(j%2)continue;x++;}x++;}cout<<"x="<=90,B-(90,80],C-(80,70],D-(70,60],,E-<60#includeusingnamespacestd;intmain(){chargd;cout<<"Enterthegrade:";cin>>gd;//直到输入有效等级,否则程序不继续运行while(!((gd>="A"&&gd<="E")||(gd>="a"&&gd<="e"))){cout<<"Invalidgrade!Pleaseretry:";cin>>gd;}if(gd=="A"||gd=="a")cout<<"nScored90-100!n";elseif(gd=="B"||gd=="b")cout<<"nScored80-89!n";elseif(gd=="C"||gd=="c")cout<<"nScored70-79!n";elseif(gd=="D"||gd=="d")cout<<"nScored60-69!n";elseif(gd=="E"||gd=="e")cout<<"nScoreunder60!n";elsecout<<"Unexpecterror!n";//防止意外错误}程序(2)//此程序用switch输出等级对应的分数段//A->=90,B-(90,80],C-(80,70],D-(70,60],,E-<60#includeusingnamespacestd;intmain(){chargd;cout<<"Enterthegrade:";cin>>gd;//直到输入有效等级,否则程序不继续运行while(!((gd>="A"&&gd<="E")||(gd>="a"&&gd<="e"))){cout<<"Invalidgrade!Pleaseretry:";cin>>gd;}
switch(gd){case"A":case"a":cout<<"nScored90-100!n";break;case"B":case"b":cout<<"nScored80-89!n";break;case"C":case"c":cout<<"nScored70-79!n";break;case"D":case"d":cout<<"nScored60-69!n";break;case"E":case"e":cout<<"nScoreunder60!n";break;default:cout<<"Unexpecterror!n";//防止意外错误}}2.什么叫做循环控制?归纳比较C++语言中各种循环控制语句的语法、循环条件和循环结束条件的表示形式及执行流程。【解答】循环控制是在特定的条件下,程序重复执行一些特定动作。语句语法执行流程使用场合while语句while(表达式)循环体;循环条件:表达式值为非0(真)循环结束条件:表达式值为0(假)程序中常用于根据条件执行操作而不需关心循环次数的情况。先判断形式循环,条件不成立时不进入循环体。do-while语句do循环体while(表达式);循环条件:表达式值为非0(真)循环结束条件:表达式值为0(假)程序中常用于根据条件执行操作而不需关心循环次数。后判断形式循环,至少执行1次循环体。一般情况,while语句和dowhile语句可以互换使用。for语句for([表达式1];[表达式2];[表达式3])循环体;(1)表达式1称为初始化表达式,不是循环体执行部分。(2)表达式3称为后置表达式,作为循环体的最后一个执行表达式。(3)循环条件:表达式2值为非0(真)循环结束条件:表达式2值为0(假)for语句称为步长循环语句,通常用于确定循环次数的情况。由于语句的3个表达式均可以缺省,也可以用于条件循环,即循环次数不确定的情况。
3.根据一个实际问题,用不同的循环语句编程,分析其优缺点。【解答】略。4.用if语句和goto语句组织循环,改写上面第3题编写的程序,并分析在什么情况下可以适当使用goto语句。【解答】在不破坏程序基本流程控制的情况下,可以适当使用goto语句实现从语句结构内部向外的必要跳转,即按特定条件结束结构语句块的执行。程序略。5.有以下程序,希望判断两个输入的整数是否相等。程序通过编译,但不能达到预期结果。请分析程序能够通过C++编译而不能得到期望结果的原因。#includeusingnamespacestd;intmain(){inta,b;cout<<"a:";cin>>a;cout<<"b:";cin>>b;if(a=b)cout<usingnamespacestd;intmain(){doublescore;
cout<<"pleaseinputscore:";cin>>score;if(score>=85)cout<<"Verygood!";elseif(score>=60)cout<<"Good!";elsecout<<"Nogood!";}2.输入3个整数,按从小到大的顺序输出它们的值。【解答】#includeusingnamespacestd;intmain(){inta,b,c,t;cout<<"a,b,c=";cin>>a>>b>>c;if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;}if(b>c){t=b;b=c;c=t;}cout<usingnamespacestd;intmain(){doublea,b,c;cout<<"a,b,c=";cin>>a>>b>>c;if(a+b>c&&b+c>a&&c+a>b){if(a==b&&b==c)cout<<"等边三角形!"<usingnamespacestd;intmain(){doublescore;chargrade;cout<<"score=";cin>>score;if(score>=0&&score<=100){switch(int(score)/10){case10:case9:grade="a";break;case8:grade="b";break;case7:grade="c";break;case6:grade="d";break;case5:case4:case3:case2:case1:case0:grade="e";break;}}else{cout<<"数据输入错误!"<usingnamespacestd;intmain(){
charfirst,second;cout<<"Firstinput(s,rorp):";cin>>first;cout<<"Secondinput(s,rorp):";cin>>second;switch(first){case"s":switch(second){case"s":cout<<"Scissortiesscissor."<usingnamespacestd;intmain(){intm,i=2;cout<<"pleaseinputm:";cin>>m;
while(i<=m)if(m%i==0){cout<#includeusingnamespacestd;intmain(){constdoubleeps=1e-8;doublea,x0,x;cout<<"pleaseinputa:";cin>>a;x0=a/2;x=(x0+a/x0)/2;while(fabs(x-x0)>eps){x0=x;x=(x0+a/x0)/2;}cout<#include#includeusingnamespacestd;intmain(){constdoublepi=3.14159265;inti;doublex,y1,y2,y3;cout<usingnamespacestd;intmain(){inti;for(i=100;i<=200;i++){if((i%3==2)&&(i%5==3)&&(i%7==2))cout<usingnamespacestd;intmain(){inti,a,b,c;for(i=100;i<=999;i++){a=i/100;b=(i-a*100)/10;c=i-a*100-b*10;if(i==a*a*a+b*b*b+c*c*c)cout<usingnamespacestd;intmain(){inti,j,s;
for(i=1;i<=1000;i++){s=0;for(j=1;jusingnamespacestd;intmain(){inti,j,k,n;charch;cout<<"Howmanylines?n";cin>>n;cout<<"Whatcharacter?n";cin>>ch;for(i=1;i<=n;i++){for(k=1;k<=n-i;k++)cout<<"";for(j=1;j<=2*i-1;j++)cout<usingnamespacestd;intmain(){intx,y,z,i;for(x=1;x<=9;x++)
for(y=1;y<=9;y++)for(z=0;z<=9;z++){i=100*x+10*y+z+100*y+10*z+z;if(i==532)cout<<"x="<usingnamespacestd;#includeintf(int);intmain(){inti;for(i=0;i<3;i++)cout<usingnamespacestd;voidfunc(inta,intb,intc=3,intd=4);intmain(){func(10,15,20,30);func(10,11,12);func(12,12);}voidfunc(inta,intb,intc,intd){cout<usingnamespacestd;voidfunc(int,int,int*);intmain(){intx,y,z;func(5,6,&x);func(7,x,&y);func(x,y,&z);cout<usingnamespacestd;voidfunc(int,int,int&);intmain(){intx=0,y=1,z=2;func(1,2,x);func(x+y,y,y);func(z,x+y,z);cout<usingnamespacestd;voidfunc(int*,int*,int*&);intmain(){inta=10,b=20;
int*p=&a,*q=&b;func(p,q,p);cout<<"*p="<<*p<<",*q="<<*q<usingnamespacestd;intf2(int,int);intf1(inta,intb){intc;a+=a;b+=b;c=f2(a+b,b+1);returnc;}intf2(inta,intb){intc;c=b%2;returna+c;}intmain(){inta=3,b=4;cout<usingnamespacestd;intage(intn){intf;if(n==1)f=10;
elsef=age(n-1)+2;returnf;}intmain(){cout<<"age:"<usingnamespacestd;intf1(inta,intb){returna+b;}intf2(inta,intb){returna-b;}intf3(int(*t)(int,int),inta,intb){return(*t)(a,b);}intmain(){int(*p)(int,int);p=f1;cout<usingnamespacestd;intsub(int,int);inta=1;intmain(){intm=1,n=2,f;f=sub(m,n);cout<
usingnamespacestd;intmain(){typedefintmyfunc(int,int);myfuncf,*fp;inta=10,b=6;fp=f;cout<<"Usingf(a):"<usingnamespacestd;voidfuncA(inti){i=i+10;}voidfuncB(int*j){*j=*j+20;}voidfuncC(int&k){k=k+30;}intmain(){inta=1;funcA(a);cout<<"a="<usingnamespacestd;inta=0;//全局变量aintmain(){inta=1;//外层局部变量a{inta=2;//内层局部变量acout<<"Localais"<usingnamespacestd;voidf(int&n);intmain(){inta=1;f(a);cout<<"a="<usingnamespacestd;intmain(){charc;cin>>c;if(c=="y"||c=="Y")inta=1;elseinta=0;cout<<"a="<
usingnamespacestd;intmain(){charc;inta;cin>>c;if(c=="y"||c=="Y")a=1;elsea=0;cout<<"a="<#includeusingnamespacestd;doublesh(doublet);intmain(){doublex,y;cout<<"x=";cin>>x;y=sh(1+sh(x))/(sh(2*x)+sh(3*x));cout<<"y="<>m>>n>>p;f1=f(1,m);f2=f(3,n);f3=f(5,p);if(f1&&f2&&f3){s=(f1+f2)/f3;cout<<"s="<usingnamespacestd;voidfmaxmin(double,double,double,double*,double*);intmain(){doublea,b,c,max,min;cout<<"a,b,c=";cin>>a>>b>>c;fmaxmin(a,b,c,&max,&min);cout<<"max="<y){u=x;v=y;}else{u=y;v=x;};if(z>u)u=z;if(zusingnamespacestd;voidfmaxmin(double,double,double,double&,double&);intmain(){doublea,b,c,max,min;cout<<"a,b,c=";cin>>a>>b>>c;fmaxmin(a,b,c,max,min);cout<<"max="<y){u=x;v=y;}else{u=y;v=x;};if(z>u)u=z;if(zusingnamespacestd;intRand(int,int);//生成指定范围的随机数intmain(){intw,i,r,t=0;charop,answer;inta,b,d;while(1)//练习开始{cout<<"现在开始?(Y或N)n";cin>>answer;if(answer=="N"||answer=="n")break;while(1){cout<<"请输入难度(1或2):";cin>>w;if(w!=1&&w!=2)cout<<"输入难度错误,重新输入!"<>op;if(op!="+"&&op!="-"&&op!="*"&&op!="/")cout<<"输入运算符错误,重新输入!"<>d;switch(op){case"+":r=a+b;break;case"-":r=a-b;break;case"*":r=a*b;break;case"/":r=a/b;break;}if(r==d){cout<<"你算对了,加10分!"<=n);returnr;}5.已知勒让德多项式为:编写程序,从键盘输入x和n的值,使用递归函数求pn(x)的值。【解答】#includeusingnamespacestd;doublep(doublex,intn);intmain(){intn;doublex;cout<<"pleaseinputxandn:";cin>>x>>n;cout<<"p("<usingnamespacestd;voidprint(intw){
for(inti=1;i<=w;i++){for(intj=1;j<=i;j++)cout<usingnamespacestd;voidprint(intw){inti;if(w){print(w-1);for(i=1;i<=w;i++)cout<#includeusingnamespacestd;
doublef1(doublex){return4/(1+x*x);}doublef2(doublex){returnsqrt(1+x*x);}doublef3(doublex){returnsin(x);}doubletrap(double(*fun)(doublex),doublea,doubleb,longn){doublet,h;inti;t=((*fun)(a)+(*fun)(b))/2.0;h=(b-a)/n;for(i=1;i<=n-1;i++)t+=(*fun)(a+i*h);t*=h;returnt;}intmain(){doublet1,t2,t3;t1=trap(f1,0,1,10000);cout<<"t1="<usingnamespacestd;voiddisplay(doubled){cout<<"adouble:"<usingnamespacestd;voidsort(doublex,doubley);voidsort(doublex,doubley,doublez);intmain(){sort(5.6,79);sort(0.5,30.8,5.9);}voidsort(doublex,doubley){if(x>y)cout<usingnamespacestd;#include"Fabricate.h"intmain(){intm,n;cout<<"inputmandn:";cin>>m>>n;cout<<"Fabricate("<>n;inta[n];2.下列数组定义语句中,不合法的是(A)。(A)inta[3]={0,1,2,3};(B)inta[]={0,1,2};(C)inta[3]={0,1,2};(D)inta[3]={0};3.已知inta[10]={0,1,2,3,4,5,6,7,8,9},*p=a;,不能表示数组a中元素的式子是(C)。(A)*a(B)*p(C)a(D)a[p-a]4.已知inta[]={0,2,4,6,8,10},*p=a+1;其值等于0的表达式是(D)。(A)*(p++)(B)*(++p)(C)*(p--)(D)*(--p)5.以下不能对二维数组a进行正确初始化的语句是(C)。(A)inta[2][3]={0};(B)inta[][3]={{0,1},{0}};(C)inta[2][3]={{0,1},{2,3},{4,5}};(D)inta[][3]={0,1,2,3,4,5};6.已知inta[][3]={{0,1},{2,3,4},{5,6},{7}};则a[2][1]的值是(C)。(A)0(B)2(C)6(D)77.已知inta[3][3]={1,2,3,4,5,6,7,8,9};不能表示数组元素a[2][1]的地址是(B)。(A)&a[2][1](B)*(a[2]+1)(C)a[2]+1(D)*(a+2)+18.已知char*a[]={"fortran","basic","pascal","java","c++"};,则cout<0(D)strcmp(s2,s1)>04.2阅读下列程序,写出运行结果1.#includeusingnamespacestd;
intmain(){inti,count=0,sum=0;doubleaverage;inta[]={1,2,3,4,5,6,7,8,9,10};for(i=0;i<10;i++){if(a[i]%2==0)continue;sum+=a[i];count++;}average=sum/count;cout<<"count="<usingnamespacestd;intmain(){inta[9]={1,2,3,4,5,6,7,8,9};int*p=a,sum=0;for(;p#includeusingnamespacestd;constintN=5;intmain(){inta[N][N]={0},i,j,k;for(k=1,i=0;i=0;j--,k++)a[j][i-j]=k;for(i=0;iusingnamespacestd;intf(int[],int);intmain(){inta[]={-1,3,5,-7,9,-11};cout<0)*=a[i];returnt;}【解答】1355.#includeusingnamespacestd;intf(int[][3],int,int);intmain(){inta[][3]={0,1,2,3,4,5,6,7,8};cout<usingnamespacestd;voidtest1(int*a1){a1=newint(5);cout<<"*a1="<<*a1<usingnamespacestd;intmain(){chars[]="abccda";inti;charc;for(i=1;(c=s[i])!=" ";i++){switch(c){case"a":cout<<"%";continue;case"b":cout<<"$";break;case"c":cout<<"*";break;case"d":continue;}
cout<<"#"<usingnamespacestd;intmain(){char*str[]={"c++","basic","pascal"};char**p;inti;p=str;for(i=0;i<3;i++)cout<<*(p+i)<usingnamespacestd;intmain(){chars1[]="Fortran",s2[]="Foxpro";char*p,*q;p=s1;q=s2;while(*p&&*q){if(*p==*q)cout<<*p;p++;q++;}cout<#includeusingnamespacestd;
intmain(){charstr[][10]={"vb","pascal","c++"},s[10];strcpy_s(s,(strcmp(str[0],str[1])<0?str[0]:str[1]));if(strcmp(str[2],s)<0)strcpy_s(s,str[2]);cout<usingnamespacestd;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:"<usingnamespacestd;voidcreate(int*,int);
intmain(){int*a=NULL,len;cin>>len;create(a,len);for(inti=0;i#includeusingnamespacestd;voidaveMsd(double[],int,double&,double&);//求平均值和均方差值函数intmain(){doubles[]={76,85,54,77,93,83,90,67,81,65};doubleave,msd;inti,n;
n=sizeof(s)/sizeof(double);//求数组元素的个数cout<<"学生成绩:";for(i=0;i#include#include#includeusingnamespacestd;intmain(){inta[10],i,j;srand(int(time(0)));//为随机数生成器设置种子值for(i=0;i<10;i++){l:a[i]=rand();//产生随机数存放到数组中if(a[i]<10||a[i]>=100)//获取指定范围数据gotol;for(j=0;jm)cout<usingnamespacestd;intmain(){inta[]={38,6,29,1,25,20,6,32,78,10};intindex[10];//记录下标的数组inti,j,temp;for(i=0;i<10;i++)index[i]=i;for(i=0;i<=8;i++)for(j=i+1;j<=9;j++)if(a[i]usingnamespacestd;intmain(){intb[10],i,j,k,flag;longnum,n;cout<<"num=";cin>>num;k=0;n=num;do//拆分整数,把各数字放入数组b{b[k++]=n%10;n=n/10;}while(n!=0);
flag=1;//判断标志i=0;j=k-1;//设置指示下标的指针while(iusingnamespacestd;voidmerge(constinta[],intna,constintb[],intnb,intc[],intnc);intmain(){inta[4]={1,2,5,7};intb[8]={3,4,8,8,9,10,11,12};intc[12];inti;merge(a,4,b,8,c,12);for(i=0;i<12;i++)cout<b[j])//当a[i]>b[j],把b[i]写入数组c{c[k]=b[j];k++;j++;}else//当a[i]<=b[j],把a[i]写入数组c{c[k]=a[i];k++;i++;}}while(iusingnamespacestd;intmain(){char*weekday[7]={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};intd;cout<<"pleaseinputweekday:";cin>>d;if(d>=0&&d<=6)cout<usingnamespacestd;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<<"n形成矩阵:n";
output(ap,n);cout<<"去掉靠边元素生成的矩阵:n";del(ap,bp,n);output(bp,n-2);cout<<"主对角线元素之和:"<usingnamespacestd;intmain(){longs[][3]={{300,250,150},{200,240,200},{280,210,180}};longp[]={3500,3300,3800};inti,j;doublesum;for(i=0;i<3;i++){sum=0;for(j=0;j<3;j++)sum+=s[i][j]*p[j];cout<<"第"<usingnamespacestd;
intfmin(int[],int);intmain(){inta[]={73,85,62,95,77,56,81,66,90,80};intindex;index=fmin(a,sizeof(a)/sizeof(int));cout<<"Theminnumnumberis:"<usingnamespacestd;voidinsert(inta[],int,int);intmain(){inta[]={10,12,23,25,48,48,53,58,60,78};intx,n,i;cout<<"pleaseinputinsertdata:";cin>>x;n=sizeof(a)/sizeof(int);//求数组长度insert(a,n,x);//插入元素for(i=0;i=p;j--)//后移元素,挤出最大值a[j]=a[j-1];a[p]=x;//插入元素}}11.一个整型数组的每个元素占4字节。编写一个压缩函数pack,把一个无符号小整数(0~255)数组进行压缩存储,只存放低8位;再编写一个解压函数unpack,把压缩数组舒展开来,以整数形式存放。主函数用随机函数生成数据初始化数组,测试pack和unpack函数。【解答】#include#include#includeusingnamespacestd;voidpack(int*a,unsignedchar*p,intn);voidunpack(unsignedchar*p,int*a,intn);intmain(){int*ary,n,i;unsignedchar*packary;cout<<"请输入数组长度:";cin>>n;ary=newint[n];//建立动态数组packary=newunsignedchar[n];//压缩数组srand(int(time(0)));for(i=0;i#include#includeusingnamespacestd;voidprintarray(int*p,intn);voidadverse(int*p,intn);intmain(){int*p,n,i;cout<<"请输入数组长度:";cin>>n;p=newint[n];//建立动态数组srand(int(time(0)));for(i=0;iusingnamespacestd;intmain(){intp,i,j,k;chars1[40],s2[40],s3[80];cout<<"s1=";cin>>s1;cout<<"s2=";cin>>s2;cout<<"inputinsertposition:";cin>>p;//输入插入位置for(i=0;i