南通大学C++课后习题答案四

合集下载

工程经济学_南通大学中国大学mooc课后章节答案期末考试题库2023年

工程经济学_南通大学中国大学mooc课后章节答案期末考试题库2023年

工程经济学_南通大学中国大学mooc课后章节答案期末考试题库2023年1.费用效果分析的适用范围是()答案:经济效果可以量化的项目_公益性建设主体效益难以货币化;_评价项目主体的效益难于货币化的项目2.下列资产中应该计算摊销费的是()答案:商标权3.针对在项目实施过程中可能发生难以预料的支出,需事先预留的费用为()答案:基本预备费4.某常规投资项目,NPV(17%)=230,NPV(18%)=-78,则该项目的内部收益率为()答案:17.7%5.已知单位产品售价为P,年固定成本为F,单位变动成本为V,单位产品销售税金为T,则以年产量表示的盈亏平衡点BEP(Q)=()答案:F/(P-V-T)6.按我国现行规定,预备费包括基本预备费和涨价预备费,其中基本预备费的计取基数是()答案:工程费用和工程建设其他费用7.社会折现率是()的计算依据。

答案:经济净现值_经济内部收益率8.关于财务评价和费用效益分析,下列说法中正确的是()答案:都考虑了资金的时间价值_采用的评价指标都为净现值、净年值、内部收益率等_都是经济效果评价9.影子工资的构成包括()答案:劳动力的机会成本10.费用效益分析只对项目的()进行分析。

答案:盈利能力11.下列属于清偿能力指标的是()答案:资本金净利润率12.现金流入与现金流出之差称为?答案:净现金流量13.假设某项目,第一年年初投入70万,第一年年末收益40万,第二年初再投入70万,第二年到第六年末分别收益60万,则第五年末的累计净现金流量为?答案:140万14.下列费用中不计入经营成本的是()。

答案:利息支出_折旧费15.城市维护建设税的计税依据是()答案:增值税和消费税16.利息备付率越(),偿债备付率越(),项目的清偿能力越好。

答案:高,高17.敏感度系数是指()与不确定因素的变化率的比值。

答案:评价指标的变化率18.敏感性分析的一般步骤包括:1.确定敏感性因素;2确定评价指标;3分析不确定因素的波动幅度及其对评价指标可能带来的增减变化情况;4选择需要分析的不确定因素。

《C语言程序设计》课后习题答案第四版谭浩强

《C语言程序设计》课后习题答案第四版谭浩强

第1章程序设计和C语言11.1什么是计算机程序11.2什么是计算机语言1printf(" Very Good!\n\n");printf ("**************************\n");return 0;}1-6#include <stdio.h>int main()return 0;}第2章算法——程序的灵魂162.1什么是算法162.2简单的算法举例172.3算法的特性21第章最简单的C程序设计——顺序程序设计37 3.1顺序程序设计举例373.2数据的表现形式及其运算39习题823-1 #include <stdio.h>}3-2-1#include <stdio.h>#include <math.h>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;次p5=p*pow(1+r0/4,4*5); // 存活期存款。

活期利息每一季度结算一次printf("p1=%f\n",p1); // 输出按第1方案得到的本息和printf("p2=%f\n",p2); // 输出按第2方案得到的本息和printf("p3=%f\n",p3); // 输出按第3方案得到的本息和printf("p4=%f\n",p4); // 输出按第4方案得到的本息和printf("p5=%f\n",p5); // 输出按第5方案得到的本息和}r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*((1+r5)*5); // 一次存5年期p2=p*(1+2*r2)*(1+3*r3); // 先存2年期,到期后将本息再存3年期次}3-2-3#include <stdio.h>#include <math.h>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p4=p*pow(1+r1,5); // 存1年期,到期后将本息存再存1年期,连续存5次p5=p*pow(1+r0/4,4*5); // 存活期存款。

《C语言程序设计》课后习题答案(第四版)谭浩强

《C语言程序设计》课后习题答案(第四版)谭浩强

第1章程序设计和C语言1什么是计算机程序1什么是计算机语言1语言的发展及其特点3最简单的C语言程序5最简单的C语言程序举例6语言程序的结构10运行C程序的步骤与方法12程序设计的任务141-5 #include <>int main ( ){ printf ("**************************\n\n"); printf(" Very Good!\n\n");printf ("**************************\n"); return 0;}1-6#include <>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max); return 0;}第2章算法——程序的灵魂16什么是算法16简单的算法举例17算法的特性21怎样表示一个算法22用自然语言表示算法22用流程图表示算法22三种基本结构和改进的流程图26用N S流程图表示算法28用伪代码表示算法31用计算机语言表示算法32结构化程序设计方法34习题36第章最简单的C程序设计——顺序程序设计37 顺序程序设计举例37数据的表现形式及其运算39常量和变量39数据类型42整型数据44字符型数据47浮点型数据49怎样确定常量的类型51运算符和表达式52语句57语句的作用和分类57最基本的语句——赋值语句59 数据的输入输出65输入输出举例65有关数据输入输出的概念67 用printf函数输出数据68用scanf函数输入数据75字符数据的输入输出78习题823-1 #include <>#include <>int main(){float p,r,n;r=;n=10;p=pow(1+r,n); printf("p=%f\n",p);return 0;}3-2-1#include <>#include <>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5; p=1000;r5=;r3=;r2=;r1=;r0=;p1=p*((1+r5)*5); #include <> #include <>int main(){float d=300000,p=6000,r=,m;m=log10(p/(p-d*r))/log10(1+r);printf("m=%\n",m);return 0;}3-4#include <>int main(){int c1,c2;c2=198;printf("c1=%c,c2=%c\n",c1,c2);printf("c1=%d,c2=%d\n",c1,c2);return 0;}3-5#include <>int main(){int a,b;float x,y;char c1,c2;scanf("a=%d b=%d",&a,&b);scanf("%f %e",&x,&y);scanf("%c%c",&c1,&c2);printf("a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c\n",a,b,x,y,c1,c2); return 0;}3-6#include <>int main(){char c1='C',c2='h',c3='i',c4='n',c5='a';c1=c1+4;c2=c2+4;c3=c3+4;c5=c5+4;printf("passwor is %c%c%c%c%c\n",c1,c2,c3,c4,c5); return 0;}3-7#include <>int main (){float h,r,l,s,sq,vq,vz;float pi=;printf("请输入圆半径r,圆柱高h∶");scanf("%f,%f",&r,&h); #include <> int main(){ int x,y;printf("输入x:");scanf("%d",&x);if(x<1) /* x<1 */{ y=x;printf("x=%3d, y=x=%d\n" ,x,y);}else if(x<10) /* 1=<x<10 */{ y=2*x-1;printf("x=%d, y=2*x-1=%d\n",x,y);}else /* x>=10 */{ y=3*x-11;printf("x=%d, y=3*x-11=%d\n",x,y);}return 0;}4-7-1#include <>int main(){int x,y;printf("enter x:");scanf("%d",&x);y=-1;if(x!=0)if(x>0)y=1;elsey=0;printf("x=%d,y=%d\n",x,y);return 0;}4-7-2#include <>int main(){int x,y;printf("please enter x:");scanf("%d",&x);y=0;if(x>=0)if(x>0) y=1;else y=-1;printf("x=%d,y=%d\n",x,y);return 0;}4-8#include <>int main(){ float score;char grade;printf("请输入学生成绩:");scanf("%f",&score);while (score>100||score<0) {printf("\n 输入有误,请重输");scanf("%f",&score);}switch((int)(score/10)){case 10:case 9: grade='A';break;case 8: grade='B';break;case 7: grade='C';break;case 6: grade='D';break;case 5:case 4:case 3:case 2:case 1:case 0: grade='E';}printf("成绩是 %,相应的等级是%c\n ",score,grade);return 0;}4-9#include <>#include <>int main(){int num,indiv,ten,hundred,thousand,ten_thousand,place; .=%d\n",sn); return 0;}5-6#include <>int main(){double s=0,t=1;int n;for (n=1;n<=20;n++){t=t*n;s=s+t;}printf("1!+2!+...+20!=%\n",s);return 0;}5-7#include <>int main(){int n1=100,n2=50,n3=10;double k,s1=0,s2=0,s3=0;for (k=1;k<=n1;k++) /*计算1到100的和*/{s1=s1+k;}for (k=1;k<=n2;k++) /*计算1到50各数的平方和*/ {s2=s2+k*k;}for (k=1;k<=n3;k++) /*计算1到10的各倒数和*/ {s3=s3+1/k;}printf("sum=%\n",s1+s2+s3);return 0;}5-8#include <>int main(){int i,j,k,n;printf("parcissus numbers are ");for (n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if (n==i*i*i + j*j*j + k*k*k)printf("%d ",n);}printf("\n");return 0;}5-9-1#define M 1000 /*定义寻找范围*/#include <>int main(){int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;int i,a,n,s;for (a=2;a<=M;a++) /* a是2-1000之间的整数,检查它是否完数 */ {n=0; /* n用来累计a的因子的个数 */s=a; /* s用来存放尚未求出的因子之和,开始时等于a */for (i=1;i<a;i++) /* 检查i是否a的因子 */if (a%i==0) /* 如果i是a的因子 */{n++; /* n加1,表示新找到一个因子 */s=s-i; /* s减去已找到的因子,s的新值是尚未求出的因子之和 */ switch(n) /* 将找到的因子赋给k1...k9,或k10 */{case 1:k1=i; break; /* 找出的笫1个因子赋给k1 */case 2:k2=i; break; /* 找出的笫2个因子赋给k2 */case 3:k3=i; break; /* 找出的笫3个因子赋给k3 */case 4:k4=i; break; /* 找出的笫4个因子赋给k4 */case 5:k5=i; break; /* 找出的笫5个因子赋给k5 */case 6:k6=i; break; /* 找出的笫6个因子赋给k6 */case 7:k7=i; break; /* 找出的笫7个因子赋给k7 */case 8:k8=i; break; /* 找出的笫8个因子赋给k8 */case 9:k9=i; break; /*找出的笫9个因子赋给k9 */case 10:k10=i; break; /* 找出的笫10个因子赋给k10 */}}if (s==0){printf("%d ,Its factors are ",a);if (n>1) printf("%d,%d",k1,k2); /* n>1表示a至少有2个因子 */if (n>2) printf(",%d",k3); /* n>2表示至少有3个因子,故应再输出一个因子 */if (n>3) printf(",%d",k4); /* n>3表示至少有4个因子,故应再输出一个因子 */if (n>4) printf(",%d",k5); /* 以下类似 */if (n>5) printf(",%d",k6);if (n>6) printf(",%d",k7);if (n>7) printf(",%d",k8);if (n>8) printf(",%d",k9);if (n>9) printf(",%d",k10);printf("\n");}}return 0;}5-9-2#include <>int main(){int m,s,i;for (m=2;m<1000;m++){s=0;for (i=1;i<m;i++)if ((m%i)==0) s=s+i;if(s==m){printf("%d,its factors are ",m); for (i=1;i<m;i++)if (m%i==0) printf("%d ",i);printf("\n");}}return 0;}5-10#include <>int main(){int i,n=20;double a=2,b=1,s=0,t;for (i=1;i<=n;i++){s=s+a/b;t=a,a=a+b,b=t;}printf("sum=%\n",s);return 0;}5-11#include <>int main(){double sn=100,hn=sn/2;int n;for (n=2;n<=10;n++){sn=sn+2*hn; /*第n次落地时共经过的米数*/ hn=hn/2; /*第n次反跳高度*/}printf("第10次落地时共经过%f米\n",sn);printf("第10次反弹%f米\n",hn);return 0;}5-12#include <>int main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2; /*第1天的桃子数是第2天桃子数加1后的2倍.*/ x2=x1;day--;}printf("total=%d\n",x1);return 0;}5-13#include <>#include <>int main(){float a,x0,x1;printf("enter a positive number:");scanf("%f",&a);x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-5);printf("The square root of % is %\n",a,x1);return 0;}#include <>#include <>int main(){double x1,x0,f,f1;x1=;do{x0=x1;f=((2*x0-4)*x0+3)*x0-6;f1=(6*x0-8)*x0+3;x1=x0-f/f1;}while(fabs(x1-x0)>=1e-5);printf("The root of equation is %\n",x1); return 0;}5-15#include <>#include <>int main(){float x0,x1,x2,fx0,fx1,fx2;do{printf("enter x1 & x2:");scanf("%f,%f",&x1,&x2);fx1=x1*((2*x1-4)*x1+3)-6;fx2=x2*((2*x2-4)*x2+3)-6;}while(fx1*fx2>0);{x0=(x1+x2)/2;fx0=x0*((2*x0-4)*x0+3)-6; if ((fx0*fx1)<0){x2=x0;fx2=fx0;}else{x1=x0;fx1=fx0;}}while(fabs (fx0)>=1e-5); printf("x=%\n",x0);return 0;}5-16#include <>int main(){int i,j,k;for (i=0;i<=3;i++){for (j=0;j<=2-i;j++)printf(" ");for (k=0;k<=2*i;k++)printf("*");printf("\n");for (i=0;i<=2;i++){for (j=0;j<=i;j++)printf(" ");for (k=0;k<=4-2*i;k++)printf("*");printf("\n");}return 0;}5-17#include <>int main(){char i,j,k; /*是a的对手;j是b的对手;k是c的对手*/ for (i='x';i<='z';i++)for (j='x';j<='z';j++)if (i!=j)for (k='x';k<='z';k++)if (i!=k && j!=k)if (i!='x' && k!='x' && k!='z')printf("A--%c\nB--%c\nC--%c\n",i,j,k);return 0;}第6章利用数组处理批量数据142 怎样定义和引用一维数组142怎样定义一维数组143怎样引用一维数组元素144一维数组的初始化145一维数组程序举例146怎样定义和引用二维数组148怎样定义二维数组149怎样引用二维数组的元素150二维数组的初始化151二维数组程序举例152字符数组154怎样定义字符数组154字符数组的初始化155怎样引用字符数组中的元素155 字符串和字符串结束标志156字符数组的输入输出159使用字符串处理函数161字符数组应用举例165习题1686-1#include <>#include <>int main(){int i,j,n,a[101];for (i=1;i<=100;i++)a[i]=i;a[1]=0;for (i=2;i<sqrt(100);i++)for (j=i+1;j<=100;j++){if(a[i]!=0 && a[j]!=0) if (a[j]%a[i]==0)a[j]=0;}printf("\n");for (i=2,n=0;i<=100;i++){ if(a[i]!=0){printf("%5d",a[i]); n++;}if(n==10){printf("\n");n=0;}}printf("\n");return 0;}6-2#include <>int main(){int i,j,min,temp,a[11];printf("enter data:\n");for (i=1;i<=10;i++){printf("a[%d]=",i);scanf("%d",&a[i]);}printf("\n");printf("The orginal numbers:\n"); for (i=1;i<=10;i++)printf("%5d",a[i]);printf("\n");for (i=1;i<=9;i++){min=i;for (j=i+1;j<=10;j++)if (a[min]>a[j]) min=j;temp=a[i];a[i]=a[min];a[min]=temp;}printf("\nThe sorted numbers:\n"); for (i=1;i<=10;i++)printf("%5d",a[i]);printf("\n");return 0;}6-3#include <>int main(){int a[3][3],sum=0;int i,j;printf("enter data:\n");for (i=0;i<3;i++)for (j=0;j<3;j++)scanf("%3d",&a[i][j]);for (i=0;i<3;i++)sum=sum+a[i][i];printf("sum=%6d\n",sum);return 0;}6-4#include <>int main(){ int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number,end,i,j;printf("array a:\n");for (i=0;i<10;i++)printf("%5d",a[i]);printf("\n");printf("insert data:");scanf("%d",&number);end=a[9];if (number>end)a[10]=number;else{for (i=0;i<10;i++){if (a[i]>number){temp1=a[i];a[i]=number;for (j=i+1;j<11;j++){temp2=a[j];a[j]=temp1;temp1=temp2;}break;}}}printf("Now array a:\n"); for (i=0;i<11;i++)printf("%5d",a[i]);printf("\n");return 0;}6-5#include <>#define N 5int main(){ int a[N],i,temp;printf("enter array a:\n");for (i=0;i<N;i++)scanf("%d",&a[i]);printf("array a:\n");for (i=0;i<N;i++)printf("%4d",a[i]);for (i=0;i<N/2;i++) n",number);;printf("continu or not(Y/N)");scanf(" %c",&c);if (c=='N'||c=='n')flag=0;}return 0;}6-10#include <>int main(){int i,j,upp,low,dig,spa,oth;char text[3][80];upp=low=dig=spa=oth=0;for (i=0;i<3;i++){ printf("please input line %d:\n",i+1); gets(text[i]);for (j=0;j<80 && text[i][j]!='\0';j++) {if (text[i][j]>='A'&& text[i][j]<='Z')upp++;else if (text[i][j]>='a' && text[i][j]<='z') low++;else if (text[i][j]>='0' && text[i][j]<='9') dig++;else if (text[i][j]==' ')spa++;elseoth++;}}printf("\nupper case: %d\n",upp);printf("lower case: %d\n",low);printf("digit : %d\n",dig);printf("space : %d\n",spa);printf("other : %d\n",oth);return 0;}6-11#include <>int main(){ char a[5]={'*','*','*','*','*'};int i,j,k;char space=' ';for (i=0;i<5;i++){ printf("\n");printf(" ");for (j=1;j<=i;j++)printf("%c",space);for (k=0;k<5;k++)printf("%c",a[k]);}printf("\n");return 0;}6-12a-c#include <>int main(){ int j,n;char ch[80],tran[80];printf("input cipher code:");gets(ch);printf("\ncipher code :%s",ch);j=0;while (ch[j]!='\0'){ if ((ch[j]>='A') && (ch[j]<='Z'))tran[j]=155-ch[j];else if ((ch[j]>='a') && (ch[j]<='z')) tran[j]=219-ch[j];elsej++;}n=j;printf("\noriginal text:");for (j=0;j<n;j++)putchar(tran[j]);printf("\n");return 0;}6-12b#include <>int main(){int j,n;char ch[80];printf("input cipher code:\n");gets(ch);printf("\ncipher code:%s\n",ch);j=0;while (ch[j]!='\0'){ if ((ch[j]>='A') && (ch[j]<='Z'))ch[j]=155-ch[j];else if ((ch[j]>='a') && (ch[j]<='z')) ch[j]=219-ch[j];elsej++;}n=j;printf("original text:");for (j=0;j<n;j++)putchar(ch[j]);printf("\n");return 0;}6-13#include <>int main(){ char s1[80],s2[40];int i=0,j=0;printf("input string1:");scanf("%s",s1);printf("input string2:");scanf("%s",s2);while (s1[i]!='\0')i++;while(s2[j]!='\0')s1[i++]=s2[j++];s1[i]='\0';printf("\nThe new string is:%s\n",s1);return 0;}6-14#include <>int main(){ int i,resu;char s1[100],s2[100];printf("input string1:");gets(s1);printf("\ninput string2:");gets(s2);i=0;while ((s1[i]==s2[i]) && (s1[i]!='\0'))i++; if (s1[i]=='\0' && s2[i]=='\0')resu=0;elseresu=s1[i]-s2[i];printf("\nresult:%d.\n",resu);return 0;}6-15#include <>#include <>int main(){ char s1[80],s2[80];int i;printf("input s2:");scanf("%s",s2);for (i=0;i<=strlen(s2);i++)s1[i]=s2[i];printf("s1:%s\n",s1);return 0;}第7章用函数实现模块化程序设计170 为什么要用函数170怎样定义函数172为什么要定义函数172定义函数的方法173调用函数174函数调用的形式174函数调用时的数据传递175函数调用的过程177函数的返回值178对被调用函数的声明和函数原型179 函数的嵌套调用182函数的递归调用184数组作为函数参数192数组元素作函数实参193数组名作函数参数194多维数组名作函数参数197局部变量和全局变量199局部变量199全局变量200变量的存储方式和生存期204动态存储方式与静态存储方式204 局部变量的存储类别205全局变量的存储类别208存储类别小结212关于变量的声明和定义214内部函数和外部函数215内部函数215外部函数215习题2187-1-1#include <>int main(){int hcf(int,int);int lcd(int,int,int);int u,v,h,l;scanf("%d,%d",&u,&v);h=hcf(u,v);printf("",h);l=lcd(u,v,h);printf("",l);return 0;}int hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}return(v);}int lcd(int u,int v,int h) {return(u*v/h);}7-1-2#include <>int Hcf,Lcd;int main(){void hcf(int,int);void lcd(int,int);int u,v;scanf("%d,%d",&u,&v);hcf(u,v);lcd(u,v);printf("",Hcf);printf("",Lcd);return 0;}void hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}Hcf=v;}void lcd(int u,int v){Lcd=u*v/Hcf;}7-2#include <>#include <>float x1,x2,disc,p,q;int main(){void greater_than_zero(float,float); void equal_to_zero(float,float);void smaller_than_zero(float,float);float a,b,c;printf("input a,b,c:");scanf("%f,%f,%f",&a,&b,&c);printf("equation: %*x*x+%*x+%=0\n",a,b,c); disc=b*b-4*a*c;printf("root:\n");if (disc>0){greater_than_zero(a,b);printf("x1=%f\t\tx2=%f\n",x1,x2);}else if (disc==0){equal_to_zero(a,b);printf("x1=%f\t\tx2=%f\n",x1,x2);}else{smaller_than_zero(a,b);printf("x1=%f+%fi\tx2=%f-%fi\n",p,q,p,q); }return 0;}void greater_than_zero(float a,float b){x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}void equal_to_zero(float a,float b){x1=x2=(-b)/(2*a);}void smaller_than_zero(float a,float b) {p=-b/(2*a);q=sqrt(-disc)/(2*a);}7-3#include <>int main(){int prime(int);int n;printf("input an integer:");scanf("%d",&n);if (prime(n))printf("%d is a prime.\n",n);elseprintf("%d is not a prime.\n",n); return 0;}int prime(int n){int flag=1,i;for (i=2;i<n/2 && flag==1;i++) if (n%i==0)flag=0;return(flag);}7-4#include <>#define N 3int array[N][N];int main(){ void convert(int array[][3]); int i,j;printf("input array:\n");for (i=0;i<N;i++)for (j=0;j<N;j++)scanf("%d",&array[i][j]);printf("\noriginal array :\n"); for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",array[i][j]);printf("\n");}convert(array);printf("convert array:\n");for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",array[i][j]);printf("\n");}return 0;}void convert(int array[][3]){int i,j,t;for (i=0;i<N;i++)for (j=i+1;j<N;j++){t=array[i][j];array[i][j]=array[j][i];array[j][i]=t;}}#include <>#include <>int main(){void inverse(char str[]);char str[100];printf("input string:");scanf("%s",str);inverse(str);printf("inverse string:%s\n",str); return 0;}void inverse(char str[]){char t;int i,j;for (i=0,j=strlen(str);i<(strlen(str)/2);i++,j--){t=str[i];str[i]=str[j-1];str[j-1]=t;}}7-6#include <>int main(){void concatenate(char string1[],char string2[],char string[]); char s1[100],s2[100],s[100];printf("input string1:");scanf("%s",s1);printf("input string2:");scanf("%s",s2);concatenate(s1,s2,s);printf("\nThe new string is %s\n",s);return 0;}void concatenate(char string1[],char string2[],char string[]) {int i,j;for (i=0;string1[i]!='\0';i++)string[i]=string1[i];for(j=0;string2[j]!='\0';j++)string[i+j]=string2[j];string[i+j]='\0';}7-7#include <>int main(){void cpy(char [],char []);char str[80],c[80];printf("input string:");gets(str);cpy(str,c);printf("The vowel letters are:%s\n",c);return 0;}void cpy(char s[],char c[]){ int i,j;for (i=0,j=0;s[i]!='\0';i++)if (s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'|| s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U'){c[j]=s[i];j++;}c[j]='\0';}7-8#include <>#include <>int main(){char str[80];void insert(char []);printf("input four digits:"); scanf("%s",str);insert(str);return 0;}void insert(char str[]){int i;for (i=strlen(str);i>0;i--) {str[2*i]=str[i];str[2*i-1]=' ';}printf("output:\n%s\n",str); }7-9#include <>int letter,digit,space,others;int main(){void count(char []);char text[80];printf("input string:\n");gets(text);printf("string:");puts(text);letter=0;digit=0;space=0;others=0;count(text);printf("\nletter:%d\ndigit:%d\nspace:%d\nothers:%d\n",letter,digit,space,others );return 0;}void count(char str[]){int i;for (i=0;str[i]!='\0';i++)if ((str[i]>='a'&& str[i]<='z')||(str[i]>='A' && str[i]<='Z'))letter++;else if (str[i]>='0' && str [i]<='9')digit++;else if (str[i]==32)space++;elseothers++;}7-10#include <>#include <>int main(){int alphabetic(char);int longest(char []);int i;char line[100];printf("input one line:\n");gets(line);printf("The longest word is :");for (i=longest(line);alphabetic(line[i]);i++) printf("%c",line[i]);printf("\n");return 0;}int alphabetic(char c){if ((c>='a' && c<='z')||(c>='A'&&c<='z'))return(1);elsereturn(0);}int longest(char string[]){int len=0,i,length=0,flag=1,place=0,point; for (i=0;i<=strlen(string);i++)if (alphabetic(string[i]))if (flag){point=i;flag=0;}elselen++;else{flag=1;if (len>=length){length=len;place=point;len=0;}}return(place);}7-11#include <>#include <>#define N 10char str[N];int main(){void sort(char []);int i,flag;for (flag=1;flag==1;){printf("input string:\n");scanf("%s",&str);if (strlen(str)>N)printf("string too long,input again!"); elseflag=0;}sort(str);printf("string sorted:\n");for (i=0;i<N;i++)printf("%c",str[i]);printf("\n");return 0;}void sort(char str[]){int i,j;char t;for(j=1;j<N;j++)for (i=0;(i<N-j)&&(str[i]!='\0');i++)if(str[i]>str[i+1]){t=str[i];str[i]=str[i+1];str[i+1]=t;}}7-12#include <>#include <>int main(){float solut(float a,float b,float c,float d); float a,b,c,d;printf("input a,b,c,d:");scanf("%f,%f,%f,%f",&a,&b,&c,&d);printf("x=%\n",solut(a,b,c,d));return 0;}float solut(float a,float b,float c,float d) {float x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-3);return(x);}7-13#include <>#define N 10#define M 5float score[N][M];float a_stu[N],a_cour[M];int r,c;int main(){ int i,j;float h;float s_var(void);float highest();void input_stu(void);void aver_stu(void);void aver_cour(void);input_stu();aver_stu();aver_cour();printf("\n NO. cour1 cour2 cour3 cour4 cour5 aver\n"); for(i=0;i<N;i++){printf("\n NO %2d ",i+1);for(j=0;j<M;j++)printf("%",score[i][j]);printf("%\n",a_stu[i]);}printf("\naverage:");for (j=0;j<M;j++)printf("%",a_cour[j]);printf("\n");h=highest();printf("highest:% NO. %2d course %2d\n",h,r,c); printf("variance %\n",s_var());return 0;}void input_stu(void){int i,j;for (i=0;i<N;i++){printf("\ninput score of student%2d:\n",i+1);for (j=0;j<M;j++)scanf("%f",&score[i][j]);}}void aver_stu(void){int i,j;float s;for (i=0;i<N;i++){for (j=0,s=0;j<M;j++)s+=score[i][j];a_stu[i]=s/;}}void aver_cour(void){int i,j;float s;for (j=0;j<M;j++){s=0;for (i=0;i<N;i++)s+=score[i][j];a_cour[j]=s/(float)N; }}float highest(){float high;int i,j;high=score[0][0];for (i=0;i<N;i++)for (j=0;j<M;j++)if (score[i][j]>high) {high=score[i][j];r=i+1;c=j+1;}return(high);}float s_var(void){int i;float sumx,sumxn;sumx=;sumxn=;for (i=0;i<N;i++){sumx+=a_stu[i]*a_stu[i];sumxn+=a_stu[i];}return(sumx/N-(sumxn/N)*(sumxn/N)); }7-*14#include <>#define N 10#define M 5float score[N][M];float a_stu[N],a_cour[M];int r,c;int main(){ int i,j;float h;float s_var(void);float highest();。

南通大学C++课后习题答案三

南通大学C++课后习题答案三

注意:第5章1~3和第14题不是编程题我没做,其它都做好了,如下!!!【4】#include<iostream>#include<cmath>using namespace std;double f(int n){if(n==1)return 1;else return 1.0/n*f(n-1);}int main(){double s=0;for(int i=1;i<=10;i++){s=f(i)+s;}cout<<s<<endl;return 0;}【5】#include<iostream>#include<cmath>using namespace std;int g(int a,int b){return a>b?a:b;}int f(int m,int n){int max;for(int i=1;i<=g(m,n);i++){if(m%i==0&&n%i==0)max=i;}return max;}int main(){int x,y;cout<<"请输入两个数:";cin>>x>>y;cout<<"最大公约数为:"<<f(x,y)<<endl;cout<<"最小公倍数为:"<<x*y/f(x,y)<<endl;return 0;}【6】#include<iostream>#include<cmath>using namespace std;int f(int n){if(n==1) return 1;else return n*n+f(n-1);}int main(){int n=1,s;do{n++;s=f(n);}while(s<1000);cout<<"1*1+2*2+...+n*n<1000的最大n为:"<<n-1<<endl;return 0;}【7】#include<iostream>using namespace std;void f(int n){int i=2,f=0;while(n>1)if(n%i==0){n=n/i;if(f==0){cout<<i;f=1;}else cout<<','<<i;}else i++;cout<<endl;}int main(void){int n;cout<<"请输入一个整数:";cin>>n;cout<<"它的所有素数因子为:";f(n);return 0;}【8】#include<iostream>using namespace std;float max(float a,float b){return a>b?a:b;}int main(void){float a,b,c;cout<<"请输入三个数:";cin>>a>>b>>c;if(a>max(b,c))cout<<"最大数为:"<<a<<endl;else cout<<"最大数为:"<<max(b,c)<<endl;return 0;}【9】#include<iostream>#include<iomanip>using namespace std;void f(int n){cout<<"该数的十六进制值是:"<<hex<<n<<endl; }int main(void){int n;cout<<"请输入一个十进制数:";cin>>n;f(n);return 0;}【10】#include<iostream>#include<cmath>using namespace std;int prime(int x){int i,j;j=(int)sqrt(x);for(i=2;i<=j;i++)if(x%i==0)break;if(i>j)cout<<x<<' ';else cout<<endl;return 0;}int main(void){int x,a,b;cout<<"请输入一个大于2的偶数和两个和为它因子的数:";cin>>x>>a>>b;if(x!=prime(a)+prime(b))cout<<"哥德巴赫猜想正确"<<endl;else cout<<"哥德巴赫猜想不正确"<<endl;return 0;}【11】#include<iostream>using namespace std;int main(){int a[100],b[100],c[100],s,m;cout<<"请输入个数:";cin>>m;cout<<"请输入此数:"<<endl;for(int i=0;i<m;i++)cin>>a[i];{for(i=0;i<m;i++){b[i]=a[i];}for(i=m;i>=0;i--){c[i]=a[i];}for(i=0;i<m;i++){if(b[i]==c[i]&&i==m-1)s=0;else s=1;}if(s==0)cout<<"此数为回文!"<<endl;if(s==1)cout<<"此数不为回文!"<<endl;}return 0;}【12】(1)#include<iostream>#include<cmath>using namespace std;int max(int a,int b){return a>b?a:b;}float max(float a,float b){return a>b?a:b;}int main(){int x=1,y=3;float a=2,b=4;cout<<"大数为:"<<max(x,y)<<","<<max(a,b)<<endl;return 0;}【12】(2)#include<iostream>#include<cmath>using namespace std;int max(int a,int b){return a>b?a:b;}float max(float a,float b){return a>b?a:b;}int main(){int x,y,z;cout<<"请输入三个整数:";cin>>x>>y>>z;float a,b,c;cout<<"请输入三个小数:";cin>>a>>b>>c;if(z>max(x,y))cout<<"整数中的大数为:"<<z<<endl;if(c>max(a,b))cout<<"小数中的大数为:"<<c<<endl;else cout<<"整数中的大数为:"<<max(x,y)<<'\n'<<"小数中的大数为:"<<max(a,b)<<endl;return 0;}【13】(1)#include<iostream>#include<cmath>using namespace std;inline int max(int a,int b){return a>b?a:b;}int main(){int x,y;cout<<"请输入两个整数:";cin>>x>>y;cout<<"整数中的大数为:"<<max(x,y)<<endl;return 0;}【13】(2)#include<iostream>#include<cmath>using namespace std;inline int max(int a,int b){return a>b?a:b;}int main(){int x,y,z;cout<<"请输入三个整数:";cin>>x>>y>>z;if(z>max(x,y))cout<<"整数中的大数为:"<<z<<endl;else cout<<"整数中的大数为:"<<max(x,y)<<endl;return 0;}【15】#include<iostream>#include<cmath>using namespace std;int root(float a,float b,float c,float*px1,float*px2){if(b*b-4*a*c<0)return 0;if(b*b-4*a*c==0){*px1=*px2=(-b/2/a);return 1;}if(b*b-4*a*c>0){*px1=(-b+sqrt(b*b-4*a*c))/2/a;*px2=(-b-sqrt(b*b-4*a*c))/2/a;return 2;}}int main(void){float a,b,c,x1,x2;cout<<"请输入一元二次方程的3个系数:"<<endl;cin>>a>>b>>c;root(a,b,c,&x1,&x2);if(root(a,b,c,&x1,&x2)==0)cout<<"此方程无根!"<<endl;else cout<<"两根为:"<<x1<<","<<x2<<endl;return 3;}【16】#include<iostream>#include<iomanip>#include<cmath>using namespace std;int f(int n){if(n==0)return 1;return n*f(n-1);}double myexp(double x,double precision=1e-4){int n=0;double s=0;do{s+=pow(x,n)/f(n);n++;}while(pow(x,n)/f(n)>precision);return s;}int main(void){float x;double precision;cout<<"请分别输入指数和精度数:";cin>>x>>precision;cout<<"结果为:"<<fixed<<setprecision(4)<<"e^"<<"="<<myexp(x,precision)<<endl;return 0;}【17】#include<iostream>#include<cmath>using namespace std;int f(int n){if(n==1||n==2) return 1;else return f(n-2)+f(n-1);}int main(){int s=0;for(int i=1;i<=10;i++){s=f(i)+s;}cout<<"前十项的和为:"<<s<<endl;return 0;}【18】#include<iostream>using namespace std;int main(void){int a[100],n;cout<<"请输入要输入的个数:";cin>>n;cout<<"请输入各数:";for(int i=0;i<n;i++)cin>>a[i];cout<<"请输出各数:";for(i=n-1;i>=0;i--)cout<<a[i];return 0;}【19】#include<iostream>#include<cmath>using namespace std;int sum(int n)if(n==1) return 1;else return n+sum(n-1);}int main(){cout<<"前十项的和为:"<<sum(10)<<endl;cout<<"前二十项的和为:"<<sum(20)<<endl;return 0;}【20】#include<iostream>#include<cmath>using namespace std;float f(float x,int n){if(n==0) return 1;if(n==1) return 2.0*x;if(n>1) return 2.0*x*f(x,n-1)-2.0*(n-1)*f(x,n-2); }int main(){float x,int n;cout<<"请输入实数x和整数n:";cin>>x>>n;cout<<"前n项为:"<<f(x,n)<<endl;return 0;}【21】#include<iostream>#include<cmath>using namespace std;double f(double x1,double x2){double fx1,fx2,fx,x;fx1=x1*x1*x1-6*x1-1;fx2=x2*x2*x2-6*x2-1;if(fx1*fx2>0) cout<<"请重新输入有根区间:";do{x=(x1+x2)/2;fx=x*x*x-6*x-1;if(fx*fx1>0)x1=x,fx1=fx;else x2=x,fx2=fx;}while (fabs(fx)>1e-8);return x;}int main(void){double x1,x2;cout<<"请输入有根区间:";cin>>x1>>x2;cout<<"x*x*x-6*x-1=0根为:"<<f(x1,x2)<<endl;return 0;}。

《C语言程序设计》课后习题答案(第四版)谭浩强

《C语言程序设计》课后习题答案(第四版)谭浩强

第1章程序设计和C语言11.1什么是计算机程序11.2什么是计算机语言11.3C语言的发展及其特点31.4最简单的C语言程序51.4.1最简单的C语言程序举例6 1.4.2C语言程序的结构101.5运行C程序的步骤与方法12 1.6程序设计的任务14令狐采学1-5 #include <stdio.h>int main ( ){ printf ("**************************\n\n"); printf(" Very Good!\n\n");printf ("**************************\n"); return 0;}1-6#include <stdio.h>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max);return 0;}第2章算法——程序的灵魂16 2.1什么是算法16 2.2简单的算法举例17 2.3算法的特性21 2.4怎样表示一个算法22 2.4.1用自然语言表示算法22 2.4.2用流程图表示算法22 2.4.3三种基本结构和改进的流程图26 2.4.4用N S流程图表示算法28 2.4.5用伪代码表示算法31 2.4.6用计算机语言表示算法322.5结构化程序设计方法34习题36第章最简单的C程序设计——顺序程序设计37 3.1顺序程序设计举例37 3.2数据的表现形式及其运算393.2.1常量和变量39 3.2.2数据类型42 3.2.3整型数据44 3.2.4字符型数据47 3.2.5浮点型数据49 3.2.6怎样确定常量的类型51 3.2.7运算符和表达式52 3.3C语句57 3.3.1C语句的作用和分类57 3.3.2最基本的语句——赋值语句59 3.4数据的输入输出65 3.4.1输入输出举例65 3.4.2有关数据输入输出的概念67 3.4.3用printf函数输出数据68 3.4.4用scanf函数输入数据75 3.4.5字符数据的输入输出78习题823-1 #include <stdio.h>#include <math.h>int main(){float p,r,n;r=0.1;n=10;p=pow(1+r,n);printf("p=%f\n",p);return 0;}3-2-1#include <stdio.h>#include <math.h>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*((1+r5)*5); // 一次存5年期p2=p*(1+2*r2)*(1+3*r3); // 先存2年期,到期后将本息再存3年期p3=p*(1+3*r3)*(1+2*r2); // 先存3年期,到期后将本息再存2年期p4=p*pow(1+r1,5); // 存1年期,到期后将本息存再存1年期,连续存5次p5=p*pow(1+r0/4,4*5); // 存活期存款。

南通大学c++2010年6月期末试卷1答案

南通大学c++2010年6月期末试卷1答案
x=(B*lin.C-lin.B*C)/t;
y=(C*lin.A-lin.C*A)/t;
}
};
void main()
{Line L1(1,1,2),L2(2,-2.5,7);//1分
double x,y;
if(!L1.IsParal(L2))//2分
{L1.intersection(L2,x,y);
#include <math.h>
class Line{//1分
double a,b,c,A,B,C;
public:
Line(double a1,double b1,double c1)//2分
{double t=sqrt(a*a+b*b);
a=a1;b=b1;பைடு நூலகம்=c1;
A=a/t;B=b/t;C=c/t;
sum+=a[n];
n++;
cin>>a[n];
}
cout<<"偶数个数:"<<os<<endl//2分
<<"奇数个数:"<<js<<endl
<<"平均值"<<1.0*sum/n<<endl;
}
2、
#include <iostream.h>
voidInsertSort(int a[],int size)//1分
以下答案仅供参考,具体判分根据考生答题情况操作。
1、
#include <iostream.h>
void main(void)
{int a[100],n=0,os=0,js=0,sum=0;//3分

南通大学C++课后习题答案六

南通大学C++课后习题答案六

using namespace std; int isIn(char* str,char c) {
int m; for(int i=0;i<30;i++)
if(str[i]==c){m=i;break;} return ++m; } int main() { cout<<"首次出现的位置为:"<<isIn("jhadjsfhbhsdfgbkfj",'b')<<endl; return 0; } 【8】 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; void sdelete(char* str,char c) { for(int i=0;i<30;i++)
cout<<a<<s<<b<<'='<<a+b<<'\n';break; case '-':
cout<<a<<s<<b<<'='<<a-b<<'\n';break; case '*':
cout<<a<<s<<b<<'='<<a*b<<'\n';break; case '/':
if(b)cout<<a<<s<<b<<'='<<a/b<<'\n'; else cout<<"非法操作,除数为0!\n";

C程序设计4完整版-课后习题答案

C程序设计4完整版-课后习题答案

C程序设计(第四版)(谭浩强)第一章课后习题答案P006 向屏幕输出文字.#include<>代码均调试成功,若有失误大多不是代码问题.自已找找.int main(){printf("Welcome to \n");return 0; }P008 求两个数的和.#include<>int main(){int a,b,sum;a=5;b=4;sum=a+b;printf("The sum is %d .\n",sum);return 0;}P008 调用函数比较两个数的大小.#include<>int main(){int max(int x,int y); int a,b,c;scanf("%d,%d",&a,&b); c=max(a,b); printf("The max is %d .\n",c);return 0;}int max(int x,int y) {int z; if (x>y)z=x;elsez=y;return(z); }P015 三个数的大小.(数字0表示课后练习题)#include<>int main(){int a,b,c,d; int max(int x , int y , int z);printf("Please input 3 numbers :\n");scanf("%d %d %d",&a,&b,&c);d=max(a,b,c); printf("The max is :%d .\n",d); }int max(int x , int y , int z){int m;if (x>y && x>z) m=x;if (y>x && y>z)m=y;if (z>y && z>x)m=z;return (m); }C程序设计(第四版)(谭浩强)第2章课后习题答案算法——程序的灵魂P017 计算机1-5相乘的积.#include<>int main(){int i,s=1; for(i=1;i<6;i++) {s=s*i; n",s);return 0;}#include<> int main(){int i,s=1; for(i=1;i<12;i++) 可以是i=i+2 {if(i%2!=0) s=s*i;elsecontinue; }printf("The sum is %d .\n",s);return 0;}P019 按要求输出80分以上的学生信息.暂时没法做.P019 判断2000-2500年中的闰年,并输出.年的概念是地球围绕太阳一周的时间(所谓公转周期)称为一年,这个周期是相当稳定的,很长时间也不会变动1秒,但是真正的一年是天(目前)。

南通大学c++部分作业题答案(二)

南通大学c++部分作业题答案(二)
cin>>n;
for(i=0;i<=5;i++)
{
a[i]=n%10;
n=n/10;
}
cout<<a[0]<<a[1]<<a[2]<<a[3]<<a[4]<<a[5]<<endl;
for(i=1;i<=5;i++)
{
for(j=0;j<=5-i;j++)
}
n=i-1;
for(j=0;j<=99;j++)
{
cin>>b[j];
if(b[j]<0)break;
}
for(j=0;b[j]>0;j++)
{
i=0;
while(i<=n)
{
if(b[j]==a[i]){break;}
else {i++;}
{
if(a[j]<a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
cout<<a[0]<<a[1]<<a[2]<<a[3]<<a[4]<<a[5];
return 0;
}
-----------------------------求两个集合的并集-----------------------------
{
cin>>a[i];

《C语言程序设计》课后习题答案第四版谭浩强

《C语言程序设计》课后习题答案第四版谭浩强

第1章程序设计和C语言11.1什么是计算机程序11.2什么是计算机语言11.3C语言的发展及其特点31.4最简单的C语言程序51.4.1最简单的C语言程序举例61.4.2C语言程序的结构101.5运行C程序的步骤与方法121.6程序设计的任务141-5 #include <stdio.h>int main ( ){ printf ("**************************\n\n");printf(" Very Good!\n\n");printf ("**************************\n");return 0;}1-6#include <stdio.h>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max); return 0;}第2章算法——程序的灵魂162.1什么是算法162.2简单的算法举例172.3算法的特性212.4怎样表示一个算法222.4.1用自然语言表示算法222.4.2用流程图表示算法222.4.3三种基本结构和改进的流程图262.4.4用流程图表示算法282.4.5用伪代码表示算法312.4.6用计算机语言表示算法322.5结构化程序设计方法34习题36第章最简单的C程序设计——顺序程序设计37 3.1顺序程序设计举例373.2数据的表现形式及其运算393.2.1常量和变量393.2.2数据类型423.2.3整型数据443.2.4字符型数据473.2.5浮点型数据493.2.6怎样确定常量的类型513.2.7运算符和表达式523.3C语句573.3.1C语句的作用和分类573.3.2最基本的语句——赋值语句593.4数据的输入输出653.4.1输入输出举例653.4.2有关数据输入输出的概念673.4.3用printf函数输出数据683.4.4用scanf函数输入数据753.4.5字符数据的输入输出78习题823-1 #include <stdio.h>#include <math.h>int main(){float p,r,n;r=0.1;n=10;p=pow(1+r,n);printf("p=%f\n",p);return 0;}3-2-1#include <stdio.h>#include <math.h>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*((1+r5)*5); // 一次存5年期p2=p*(1+2*r2)*(1+3*r3); // 先存2年期,到期后将本息再存3年期p3=p*(1+3*r3)*(1+2*r2); // 先存3年期,到期后将本息再存2年期p4=p*pow(1+r1,5); // 存1年期,到期后将本息存再存1年期,连续存5次p5=p*pow(1+r0/4,4*5); // 存活期存款。

c语言程序设计第四版课后习题答案

c语言程序设计第四版课后习题答案

c语言程序设计第四版课后习题答案C语言程序设计第四版是一本经典的编程教材,为学习C语言的初学者提供了全面的知识和实践指导。

在学习过程中,课后习题是巩固所学知识和提高编程能力的重要环节。

本文将为读者提供一些C语言程序设计第四版课后习题的答案,并探讨其中的一些关键概念和技巧。

第一章介绍了C语言的基本概念和编程环境的设置。

在课后习题中,有一个常见的问题是要求编写一个简单的Hello World程序。

这个程序非常简单,只需使用printf函数打印出"Hello World"即可。

通过这个练习,读者可以熟悉C语言的基本语法和函数的使用。

第二章讲解了C语言的数据类型和变量。

在课后习题中,有一个常见的问题是要求计算两个整数的和并输出结果。

读者可以使用scanf函数获取用户输入的两个整数,然后使用加法运算符计算它们的和,并使用printf函数输出结果。

通过这个练习,读者可以巩固对数据类型和变量的理解,并学会使用基本的输入输出函数。

第三章介绍了C语言的运算符和表达式。

在课后习题中,有一个常见的问题是要求计算一个圆的面积和周长。

读者可以使用宏定义或者变量来表示圆的半径,然后使用相应的公式计算面积和周长,并使用printf函数输出结果。

通过这个练习,读者可以熟悉算术运算符和数学函数的使用,同时加深对表达式和运算符优先级的理解。

第四章讲解了C语言的控制语句,包括条件语句和循环语句。

在课后习题中,有一个常见的问题是要求编写一个判断一个数是否为素数的程序。

读者可以使用循环语句和条件语句来实现这个功能,通过判断该数是否能被2到其平方根之间的数整除来确定是否为素数,并使用printf函数输出结果。

通过这个练习,读者可以熟悉条件语句和循环语句的使用,同时加深对算法设计和逻辑思维的理解。

第五章介绍了C语言的函数和递归。

在课后习题中,有一个常见的问题是要求编写一个递归函数来计算一个整数的阶乘。

读者可以使用递归的思想,将问题拆解成更小的子问题,并使用递归函数来解决。

C程序设计第四版习题完整版答案排版整洁

C程序设计第四版习题完整版答案排版整洁

第1章程序设计和C语言1什么是计算机程序1什么是计算机语言1语言的发展及其特点3最简单的C语言程序5最简单的C语言程序举例6语言程序的结构10运行C程序的步骤与方法12程序设计的任务141-5 #include <>int main ( ){ printf ("**************************\n\n"); printf(" Very Good!\n\n");printf ("**************************\n"); return 0;}1-6#include <>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max); return 0;}第2章算法——程序的灵魂16什么是算法16简单的算法举例17算法的特性21怎样表示一个算法22S流程图表示算法28结构化程序设计方法34习题36第章最简单的C程序设计——顺序程序设计37 顺序程序设计举例37数据的表现形式及其运算39语句57数据的输入输出65 习题823-1 #include <>#include <>int main(){float p,r,n;r=;n=10;p=pow(1+r,n);printf("p=%f\n",p);return 0;}3-2-1#include <>#include <>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5; p=1000;r5=;r3=;r2=;r1=;r0=;p1=p*((1+r5)*5); #include <> #include <>int main(){float d=300000,p=6000,r=,m;m=log10(p/(p-d*r))/log10(1+r);printf("m=%\n",m);return 0;}3-4#include <>int main(){int c1,c2;c1=197;c2=198;printf("c1=%c,c2=%c\n",c1,c2); printf("c1=%d,c2=%d\n",c1,c2); return 0;}3-5#include <>int main(){int a,b;float x,y;char c1,c2;scanf("a=%d b=%d",&a,&b);scanf("%f %e",&x,&y);scanf("%c%c",&c1,&c2);printf("a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c\n",a,b,x,y,c1,c2); return 0;}3-6#include <>int main(){char c1='C',c2='h',c3='i',c4='n',c5='a';c1=c1+4;c2=c2+4;c3=c3+4;c4=c4+4;c5=c5+4;printf("passwor is %c%c%c%c%c\n",c1,c2,c3,c4,c5);return 0;}3-7#include <>int main (){float h,r,l,s,sq,vq,vz;float pi=;printf("请输入圆半径r,圆柱高h∶");scanf("%f,%f",&r,&h); #include <> int main(){ int x,y;printf("输入x:");scanf("%d",&x);if(x<1) /* x<1 */{ y=x;printf("x=%3d, y=x=%d\n" ,x,y);}else if(x<10) /* 1=<x<10 */{ y=2*x-1;printf("x=%d, y=2*x-1=%d\n",x,y); }else /* x>=10 */{ y=3*x-11;printf("x=%d, y=3*x-11=%d\n",x,y); }return 0;}4-7-1#include <>int main(){int x,y;printf("enter x:");scanf("%d",&x);y=-1;if(x>0)y=1;elsey=0;printf("x=%d,y=%d\n",x,y); return 0;}4-7-2#include <>int main(){int x,y;printf("please enter x:"); scanf("%d",&x);y=0;if(x>0) y=1;else y=-1;printf("x=%d,y=%d\n",x,y); return 0;}4-8#include <>int main(){ float score;char grade;printf("请输入学生成绩:"); scanf("%f",&score);while (score>100||score<0) {printf("\n 输入有误,请重输"); scanf("%f",&score);switch((int)(score/10)){case 10:case 9: grade='A';break;case 8: grade='B';break;case 7: grade='C';break;case 6: grade='D';break;case 5:case 4:case 3:case 2:case 1:case 0: grade='E';}printf("成绩是 %,相应的等级是%c\n ",score,grade); return 0;4-9#include <>#include <>int main(){int num,indiv,ten,hundred,thousand,ten_thousand,place; .=%d\n",sn); return 0;}5-6#include <>int main(){double s=0,t=1;int n;for (n=1;n<=20;n++){t=t*n;s=s+t;}printf("1!+2!+...+20!=%\n",s);return 0;}5-7#include <>int main(){int n1=100,n2=50,n3=10;double k,s1=0,s2=0,s3=0;for (k=1;k<=n1;k++) /*计算1到100的和*/{s1=s1+k;}for (k=1;k<=n2;k++) /*计算1到50各数的平方和*/ {s2=s2+k*k;}for (k=1;k<=n3;k++) /*计算1到10的各倒数和*/ {s3=s3+1/k;}printf("sum=%\n",s1+s2+s3);return 0;}5-8#include <>int main(){int i,j,k,n;printf("parcissus numbers are ");for (n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if (n==i*i*i + j*j*j + k*k*k)printf("%d ",n);}printf("\n");return 0;}5-9-1#define M 1000 /*定义寻找范围*/#include <>int main(){int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;int i,a,n,s;for (a=2;a<=M;a++) /* a是2-1000之间的整数,检查它是否完数 */ {n=0; /* n用来累计a的因子的个数 */s=a; /* s用来存放尚未求出的因子之和,开始时等于a */for (i=1;i<a;i++) /* 检查i是否a的因子 */if (a%i==0) /* 如果i是a的因子 */{n++; /* n加1,表示新找到一个因子 */s=s-i; /* s减去已找到的因子,s的新值是尚未求出的因子之和 */ switch(n) /* 将找到的因子赋给k1...k9,或k10 */{case 1:k1=i; break; /* 找出的笫1个因子赋给k1 */case 2:k2=i; break; /* 找出的笫2个因子赋给k2 */case 3:k3=i; break; /* 找出的笫3个因子赋给k3 */case 4:k4=i; break; /* 找出的笫4个因子赋给k4 */case 5:k5=i; break; /* 找出的笫5个因子赋给k5 */case 6:k6=i; break; /* 找出的笫6个因子赋给k6 */case 7:k7=i; break; /* 找出的笫7个因子赋给k7 */case 8:k8=i; break; /* 找出的笫8个因子赋给k8 */case 9:k9=i; break; /*找出的笫9个因子赋给k9 */case 10:k10=i; break; /* 找出的笫10个因子赋给k10 */}}if (s==0){printf("%d ,Its factors are ",a);if (n>1) printf("%d,%d",k1,k2); /* n>1表示a至少有2个因子 */if (n>2) printf(",%d",k3); /* n>2表示至少有3个因子,故应再输出一个因子 */if (n>3) printf(",%d",k4); /* n>3表示至少有4个因子,故应再输出一个因子 */if (n>4) printf(",%d",k5); /* 以下类似 */if (n>5) printf(",%d",k6);if (n>6) printf(",%d",k7);if (n>7) printf(",%d",k8);if (n>8) printf(",%d",k9);if (n>9) printf(",%d",k10);printf("\n");}}return 0;}5-9-2#include <>int main(){int m,s,i;for (m=2;m<1000;m++){s=0;for (i=1;i<m;i++)if ((m%i)==0) s=s+i;if(s==m){printf("%d,its factors are ",m); for (i=1;i<m;i++)if (m%i==0) printf("%d ",i); printf("\n");}}return 0;}5-10#include <>int main()int i,n=20;double a=2,b=1,s=0,t; for (i=1;i<=n;i++){s=s+a/b;t=a,a=a+b,b=t;}printf("sum=%\n",s); return 0;}5-11#include <>int main()double sn=100,hn=sn/2;int n;for (n=2;n<=10;n++){sn=sn+2*hn; /*第n次落地时共经过的米数*/ hn=hn/2; /*第n次反跳高度*/}printf("第10次落地时共经过%f米\n",sn); printf("第10次反弹%f米\n",hn);return 0;}5-12#include <>int main(){day=9;x2=1;while(day>0){x1=(x2+1)*2; /*第1天的桃子数是第2天桃子数加1后的2倍.*/ x2=x1;day--;}printf("total=%d\n",x1);return 0;}5-13#include <>#include <>int main(){printf("enter a positive number:");scanf("%f",&a);x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-5);printf("The square root of % is %\n",a,x1); return 0;}5-14#include <>#include <>int main(){double x1,x0,f,f1;x1=;do{x0=x1;f=((2*x0-4)*x0+3)*x0-6;f1=(6*x0-8)*x0+3;x1=x0-f/f1;}while(fabs(x1-x0)>=1e-5);printf("The root of equation is %\n",x1); return 0;}5-15#include <>#include <>int main(){float x0,x1,x2,fx0,fx1,fx2;{printf("enter x1 & x2:"); scanf("%f,%f",&x1,&x2);fx1=x1*((2*x1-4)*x1+3)-6; fx2=x2*((2*x2-4)*x2+3)-6; }while(fx1*fx2>0);do{x0=(x1+x2)/2;fx0=x0*((2*x0-4)*x0+3)-6; if ((fx0*fx1)<0){x2=x0;fx2=fx0;}else{x1=x0;fx1=fx0;}while(fabs (fx0)>=1e-5); printf("x=%\n",x0);return 0;}5-16#include <>int main(){int i,j,k;for (i=0;i<=3;i++){for (j=0;j<=2-i;j++) printf(" ");for (k=0;k<=2*i;k++) printf("*");printf("\n");for (i=0;i<=2;i++){for (j=0;j<=i;j++)printf(" ");for (k=0;k<=4-2*i;k++)printf("*");printf("\n");}return 0;}5-17#include <>int main(){char i,j,k; /*是a的对手;j是b的对手;k是c的对手*/ for (i='x';i<='z';i++)for (j='x';j<='z';j++)if (i!=j)for (k='x';k<='z';k++)if (i!=k && j!=k)if (i!='x' && k!='x' && k!='z')printf("A--%c\nB--%c\nC--%c\n",i,j,k); return 0;}。

C程序设计第四版习题完整版答案 排版整洁

C程序设计第四版习题完整版答案 排版整洁

第1章程序设计和C语言11.1什么是计算机程序11.2什么是计算机语言11.3C语言的发展及其特点31.4最简单的C语言程序51.4.1最简单的C语言程序举例6语言程序的结构101.5运行C程序的步骤与方法121.6程序设计的任务141-5 #include <stdio.h>int main ( ){ printf ("**************************\n\n"); printf(" Very Good!\n\n");printf ("**************************\n"); return 0;}1-6#include <stdio.h>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max); return 0;}第2章算法——程序的灵魂162.1什么是算法162.2简单的算法举例172.3算法的特性212.4怎样表示一个算法22S流程图表示算法282.5结构化程序设计方法34习题36第章最简单的C程序设计——顺序程序设计37 3.1顺序程序设计举例373.2数据的表现形式及其运算393.3C语句573.4数据的输入输出65 习题823-1 #include <stdio.h>#include <math.h>int main(){float p,r,n;r=0.1;n=10;p=pow(1+r,n);printf("p=%f\n",p);return 0;}3-2-1#include <stdio.h>#include <math.h>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5; p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*((1+r5)*5); // 一次存5年期p2=p*(1+2*r2)*(1+3*r3); // 先存2年期,到期后将本息再存3年期p3=p*(1+3*r3)*(1+2*r2); // 先存3年期,到期后将本息再存2年期p4=p*pow(1+r1,5); // 存1年期,到期后将本息存再存1年期,连续存5次p5=p*pow(1+r0/4,4*5); // 存活期存款。

《C语言程序设计》课后习题答案(第四版)谭浩强

《C语言程序设计》课后习题答案(第四版)谭浩强

第1 章程序设计和C语言11.1 什么是计算机程序 11.2 什么是计算机语言 11.3C 语言的发展及其特点 31.4 最简单的C语言程序 51.4.1 最简单的C语言程序举例 6 1.4.2C 语言程序的结构101.5 运行C程序的步骤与方法12 1.6 程序设计的任务141-5 #include <stdio.h>int main ( ){ printf ("**************************\n\n");printf(" Very Good!\n\n");printf ("**************************\n");return 0;}1-6#include <stdio.h>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max); return 0;}第2 章算法——程序的灵魂162.1 什么是算法162.2 简单的算法举例172.3 算法的特性212.4 怎样表示一个算法222.4.1 用自然语言表示算法222.4.2 用流程图表示算法222.4.3 三种基本结构和改进的流程图26 2.4.4 用N 流程图表示算法282.4.5 用伪代码表示算法312.4.6 用计算机语言表示算法321.7结构化程序设计方法34习题36第章最简单的 C 程序设计——顺序程序设计37 1.4.3顺序程序设计举例371.4.4数据的表现形式及其运算392.5 常量和变量392.6 数据类型422.7 整型数据442.8 字符型数据472.9 浮点型数据492.10 怎样确定常量的类型512.11 运算符和表达式521.4.5C 语句572.4.7C 语句的作用和分类572.4.8 最基本的语句——赋值语句591.4.6数据的输入输出653.4.1 输入输出举例653.4.2 有关数据输入输出的概念673.4.3 用printf 函数输出数据683.4.4 用scanf 函数输入数据753.4.5 字符数据的输入输出78习题823-1 #include <stdio.h>#include <math.h>int main(){float p,r,n;r=0.1;n=10;p=pow(1+r,n);printf("p=%f\n",p);return 0;}3-2-1#include <stdio.h>#include <math.h>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*((1+r5)*5); // 一次存 5 年期p2=p*(1+2*r2)*(1+3*r3); // 先存 2 年期,到期后将本息再存 3 年期p3=p*(1+3*r3)*(1+2*r2); // 先存 3 年期,到期后将本息再存 2 年期p4=p*pow(1+r1,5); // 存1 年期,到期后将本息存再存 1 年期,连续存 5 次p5=p*pow(1+r0/4,4*5); // 存活期存款。

C大学教程(第四版)课后习题答案_0001讲义教程

C大学教程(第四版)课后习题答案_0001讲义教程

Copyright 1992–2004 by Deitel amp Associates Inc. and Pearson Education Inc. All Rights Reserved.Instructor’s ManualforC How to Program 4/eDeitel amp DeitelA-PDF Split DEMO : Purchase from to remove thewatermarkContents1Introduction to Computers the Internet and the World WideWeb12Introduction to C Programming53Structured Program Development in C194C Program Control555C Functions976C Arrays1697Pointers2338C Characters and Strings2839C Formatted Input/Output31910Structures Unions Bit Manipulations and Enumerations33311C File Processing35312Data Structures375II 13The Preprocessor44114Other C Topics44715C as a “Better C”45716C Classes and Data Abstraction46317C Classes: Part II48518C Operator Overloading49319CInheritance49920C Virtual Functions and Polymorphism51121C StreamInput/Output51922C Templates53723C Exception Handling: Solution54324Introduction to Java Applications and Applets54725Beyond C amp C: Operators Methods amp Arrays in Java55726Java Object-Based Programming58527Java Object-Oriented Programming60328Java Graphics and Java2D61729Java Graphical User Interface Components63330Java Multimedia: ImagesAnimation and Audio661?? Copyright 1992–2004 by Deitel amp Associates Inc. and Pearson Education Inc. All Rights Reserved.1Introduction to Computers the Internet and the World Wide Web: SolutionsSOLUTIONS1.3Categorize each of the following items as either hardware or software:aCPUANS: hardware.bC compilerANS: software.cALUANS: hardware.dC preprocessorANS: software.einput unitANS: hardware.fa word processor programANS: software.1.4Why might you want to write a program in a machine-independent language instead of a machine-dependent languageWhy might a machine-dependent language be more appropriate for writing certain types of programsANS: Machine independent languages are useful for writing programs to be executed on multiple computer platforms.Machine dependent languages are appropriate for writing programs to be executed on a single platform. Machine dependentlanguages tend to exploit the efficiencies of a particular machine.1.5Translator programs such as assemblers and compilers convert programs from one language referred to as the source lan-guage to another language referred to as the object language. Determine which of the following statements are true and which arefalse:aA compiler translates high-level language programs into object language.ANS: True.bAn assembler translates source language programs into machine language programs.ANS: True.cA compiler converts source language programs into object language programs.ANS: False.dHigh-level languages are generally machine-dependent.ANS: False.eA machine language program requires translation before the program can be run on a computer.ANS: False.2 Introduction to Computers the Internet and the World Wide Web: SolutionsChapter 1?? Copyright 1992–2004 by Deitel amp Associates Inc. and Pearson Education Inc. All Rights Reserved.1.6Fill in the blanks in each of the following statements:aDevices from which users access timesharing computer systems are usually called .ANS: terminals.bA computer program that converts assembly language programs to machine language programs is called .ANS: an assembler.cThe logical unit of the computer that receives information from outside the computer for use by the computer is called.ANS: The input unit.dThe process of instructing the computer to solve specific problems is called . ANS: computer programming.eWhat type of computer language uses English-like abbreviations for machine language instructions .ANS: a high-level language.fWhichlogical unit of the computer sends information that has already been processed by the computer to various de-vices so that the information may be used outside the computer .ANS: The output unit.gThe general name for a program that converts programs written in a certain computer language into machine languageis .ANS: compiler.hWhich logical unit of the computer retains information .ANS: memory unit and secondary storage unit.iWhich logical unit of the computer performscalculations .ANS: arithmetic and logical unit.jWhich logical unit of the computer makes logical decisions .ANS: arithmetic and logical unitkThe commonly used abbreviation for the computers control unit is .ANS: CPU.lThe level of computer language most convenient to the programmer for writing programs quickly and easily is. ANS:high-level language.mThe only language that a computer can directly understand is called that computers .ANS: machine language.nWhich logical unit of the computer coordinates the activities of all the other logical units .ANS: central processing unit.1.7State whether each of the following is true or false. If false explain your answer.aMachine languages are generally machine dependent.ANS: True. Machine languages are closely related to the hardware of a particular machine.bTimesharing truly runs several users simultaneously on a computer.ANS: False. Time sharing systems split CPU time amongst several users so that the users appear to be operating simulta-neouslycLike otherhigh-level languages C is generally considered to be machine independent. ANS: True. C programs can be written on most machines and with some care C programs can be written on one machineand run on many machines with few changes or nochanges.1.8Discuss the meaning of each of the following names:astdin ANS: This refers to the standard input device. The standard input device is normally connected to the keyboardbstdout ANS: This refers to the standard output device. The standard output device is normally connected to the computer screen.cstderr ANS: This refers to the standard error device. Error messages are normally sent to this device which is typically connectedto the computer screen.1.9Why is so much attention today focused onobject-oriented programming in general and C in particularANS: Object-oriented programming enables the programmer to build reusable software components that model items inthe real world. Building software quickly correctly and economically has been an elusive goal in the software industry.The modular object-oriented design and implementation approach has been found to increase productivity 10 to 100 timesover conventional programming languages while reducing development time errors and cost.C is used for object-ori-ented programming because it is a superset of the C programming language and C is widely used. Chapter 1Introduction to Computers the Internet and the World Wide Web: Solutions 3?? Copyright 1992–2004 by Deitel amp Associates Inc. and Pearson Education Inc. All Rights Reserved.1.10Which programming language is best described by each of the followingaDeveloped by IBM for scientific and engineering applications.ANS: FORTRANbDeveloped specifically for business applications.ANS: COBOLcDeveloped for teaching structured programming.ANS: PascaldNamed after the world’s first computer programmer.ANS: AdaeDeveloped to familiarize novices with programming techniques.ANS: BASICfSpecifically developed to help programmers migrate to .NET.ANS: CgKnown as the development language of UNIX.ANS: ChFormed primarily by adding object-oriented programming to C.ANS: CiSucceeded initially because of its ability to create Web pages with dynamic content.ANS: Java4 Introduction to Computers the Internet and the World Wide Web: SolutionsChapter 1??Copyright 1992–2004 by Deitel amp Associates Inc. and Pearson Education Inc. All Rights Reserved.?? Copyright 1992–2004 by Deitel amp Associates Inc. and Pearson Education Inc. All Rights Reserved.2Introduction to C Programming: SolutionsSOLUTIONS:2.7Identify and correct the errors in each of the following statements Note: there may be more than one error per statement:ascanf quotdquot value ANS: scanf “d” ampvalue bprintf quotThe product of d and d is dquotn x y ANS: printf “The product of d and d is dn” x y z cfirstNumber secondNumber sumOfNumbers ANS: sumOfNumbers firstNumber secondNumberdif number gt largest largest number ANS: if number gt largerst largest number e/ Program to determine the largest of three integers / ANS: / Program to determine the largest of three integers/fScanf quotdquot anInteger ANS: scanf “d” ampanInteger gprintf quotRemainder of d divided by d isnquot x y x y ANS: printf “Remainder of f divided by d is dn” x y xy hif x y printf d is equal to dnquot x y ANS: if x y printf “d is equal to dn” x y iprint quotThe sum is dnquot x y ANS: printf “The sum is dn” x y jPrintf quotThe value you entered is: dn ampvalue ANS: printf “The value you entered is: dn” value 2.8Fill in the blanks in each of the following:a are used to document a program and improve its readability.ANS: comments.bThe function used to display information on the screen is .ANS: printf.6 Introduction to C Programming: SolutionsChapter 2?? Copyright 1992–2004 by Deitel amp Associates Inc. and Pearson Education Inc. All Rights Reserved.cA C statement that makes a decision is .ANS: if.dCalculations are normally performed by statements.ANS: assignment.eThe function inputs values from the keyboard.ANS: scanf.2.9Write a single C statement or line that accomplishes each of the following:aPrint the message “Enter two numbers.”ANS: printf “Enter two numbersn” bAssign the product of variables b and c to variable a.ANS: a b ccState that a program performs a sample payroll calculation i.e. use text that helps to document a program.ANS: / Sample payroll calculation program /dInput three integer values from the keyboard and place these values in integer variables a b and c. ANS: scanf “ddd” ampa ampb ampc 2.10State which of the following are true and which are false. If false explain your answer.aC operators are evaluated from left to right.ANS: False. Some operators are evaluated left to right and others are evaluated from right to left depending on theirasso-ciativity see Appendix C.bThe following are all valid variable names: _under_bar_ m928134 t5 j7 her_sales his_account_total a bc z z2.ANS: True. cThe statement printfquota 5quot is a typical example of an assignment statement.ANS: False. The statement prints a 5 on the screen.dA valid arithmetic expression containing no parentheses is evaluated from left to right.ANS: False. Multiplication division and modulus are all evaluated first from left to right then addition and subtractionare evaluated from left to right.eThe following are all invalid variable names: 3g 87 67h2 h22 2h.ANS: False. Those beginning with a number are invalid.2.11Fill in the blanks in each of the following:aWhat arithmetic operations are on the same level of precedence as multiplication .ANS: division modulus.bWhen parentheses are nested which set of parentheses is evaluated first in an arithmetic expression .ANS: The innermost pair of parenthesis.cA location in the computers memory that may contain different values at various times throughout the execution of aprogram is called a .ANS: variable.2.12What if anything prints when each of the following C statements is performed If nothing prints then answer “nothing.”Assume x 2 and y 3.aprintf quotdquot x ANS: 2bprintf quotdquot x x ANS: 4cprintf quotxquot ANS: xdprintf quotxdquot x ANS:x2eprintf quotd dquot x y y x ANS: 5 5fz x y ANS: Nothing. Value of x y is assigned to z. gscanf quotddquot ampx ampy ANS: Nothing. Two integer values are read into the location of x and the location of y.h/ printf quotx y dquot x y / ANS: Nothing. This is a comment.iprintf quotnquot Chapter 2Introduction to C Programming: Solutions 7?? Copyright 1992–2004 by Deitel amp Associates Inc. and Pearson Education Inc. All Rights Reserved.ANS: A newline character is printed and the cursor is positioned at the beginning of the next line on the screen.2.13Which if any of the following C statements contain variables involved in destructive read-inascanf quotdddddquot ampb ampc ampd ampe ampf bp i j k 7 cprintf quotDestructive read-inquot dprintf quota 5quot ANS: a.2.14Given the equation y ax3 7 whichof the following if any are correct C statements for this equationay a x x x 7 bya x x x 7 cy a x x x 7 dy a x x x 7 ey a x x x7 fy a x x x 7 ANS: a d and e.2.15State the order of evaluation of the operators in each of the following C statements and show the value of x after eachstate-ment is performed.ax 7 3 6 / 2 - 1 ANS: is first / is second is third and - is fourth. Value of x is 15.bx 2 2 2 2 - 2 / 2 ANS: is first is second / is third is fourth - is fifth. Value of x is 3.cx 3 9 3 9 3 / 3 ANS: 5 6 4 2 3 1. Value of x is 338.2.16Write a program that asks the user to enter two numbers obtains the two numbers from the user and prints the sum productdifference quotient and remainder of the two numbers.ANS: 1/ Exercise 2.16 Solution /2include ltstdio.hgt34int main56 int x / define first number /7 int y / define second number /8 9 printf quotEnter two numbers: quot / prompt user /10 scanf quotddquot ampx ampy / read values from keyboard / 11 12 / output results /13 printf quotThe sum is dnquot x y 14 printf quotThe product is dnquot x y 15 printf quotThe difference is dnquot x - y 16 printf quotThe quotient is dnquot x / y 17 printf quotThe modulus is dnquot x y 1819 return 0 / indicate successful termination /2021 / end main /Enter two numbers: 20 5The sum is 25The product is100The difference is 15The quotient is 4The modulus is 08 Introduction to C Programming: SolutionsChapter 2?? Copyright 1992–2004 by Deitel amp Associates Inc. and Pearson Education Inc. All Rights Reserved.2.17Write a program that prints the numbers 1 to 4 on the same line. Write the program using the following methods.aUsing one printf statement with no conversion specifiers.bUsing one printf statement with four conversion specifiers.cUsing four printf statements.ANS: 2.18Write a program that asks the user to enter two integers obtains the numbers from the user then prints the larger numberfollowed by the words “is larger.” If the numbers are equal print the message “These numbers are equal.” Use only thesingle-selection form of the if statement you learned in this chapter.ANS: 1/ Exercise 2.17 Solution /2include ltstdio.hgt34int main5 6 printf quot1 2 3 4nnquot / part a /78 printf quotd d d dnnquot 1 2 3 4 / part b /910 printf quot1 quot / part c /11 printf quot2 quot 12 printf quot3 quot 13 printf quot4nquot 1415 return 0 / indicates successful termination /1617 / end main /1 2 3 41 2 3 41 2 3 41/ Exercise 2.18 Solution /2include ltstdio.hgt34int main5 6 int x / define first number /7 int y / define second number /8 9 printf quotEnter two numbers: quot / prompt /10 scanf quotddquot ampx ampy / read two integers / 11 12 / compare the two numbers /13 if x gt y 14 printf quotd is largernquot x 15 / end if /1617 if x lt y 18 printf quotd is largernquot y 19 / end if /2021 if x y 22 printf quotThese numbers are equalnquot 23 / end if /24 Chapter 2Introduction to C Programming: Solutions 9?? Copyright 1992–2004 by Deitel amp Associates Inc. and Pearson Education Inc. All RightsReserved.2.19Write a program that inputs three different integers from the keyboard then prints the sum the average the product thesmallest and the largest of these numbers. Use only the single-selection form of the if statement you learned in this chapter. Thescreen dialogue should appear as follows:ANS: 25 return 0 / indicate successful termination/2627 / end main /Enter two numbers: 5 2020 is largerEnter two numbers: 239 92239 is largerEnter two numbers: 17 17These numbers are equalI.。

c语言程序设计课后习题答案第四版

c语言程序设计课后习题答案第四版

第1章程序设计和C语言11.1什么是计算机程序11.2什么是计算机语言11.3C语言的发展及其特点31.4最简单的C语言程序51.4.1最简单的C语言程序举例61.4.2C语言程序的结构101.5运行C程序的步骤与方法121.6程序设计的任务141-5 #include <stdio.h>int main ( ){ printf ("**************************\n\n");printf(" Very Good!\n\n");printf ("**************************\n");return 0;}1-6#include <stdio.h>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max); return 0;}第2章算法——程序的灵魂162.1什么是算法162.2简单的算法举例172.3算法的特性212.4怎样表示一个算法222.4.1用自然语言表示算法222.4.2用流程图表示算法222.4.3三种基本结构和改进的流程图262.4.4用N S流程图表示算法282.4.5用伪代码表示算法312.4.6用计算机语言表示算法322.5结构化程序设计方法34习题36第章最简单的C程序设计——顺序程序设计37 3.1顺序程序设计举例373.2数据的表现形式及其运算393.2.1常量和变量393.2.2数据类型423.2.3整型数据443.2.4字符型数据473.2.5浮点型数据493.2.6怎样确定常量的类型513.2.7运算符和表达式523.3C语句573.3.1C语句的作用和分类573.3.2最基本的语句——赋值语句593.4数据的输入输出653.4.1输入输出举例653.4.2有关数据输入输出的概念673.4.3用printf函数输出数据683.4.4用scanf函数输入数据753.4.5字符数据的输入输出78习题823-1 #include <stdio.h>#include <math.h>int main(){float p,r,n;r=0.1;n=10;p=pow(1+r,n);printf("p=%f\n",p);return 0;}3-2-1#include <stdio.h>#include <math.h>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*((1+r5)*5); // 一次存5年期p2=p*(1+2*r2)*(1+3*r3); // 先存2年期,到期后将本息再存3年期p3=p*(1+3*r3)*(1+2*r2); // 先存3年期,到期后将本息再存2年期p4=p*pow(1+r1,5); // 存1年期,到期后将本息存再存1年期,连续存5次p5=p*pow(1+r0/4,4*5); // 存活期存款。

【文件】C语言入门经典第4版课后练习参考答案

【文件】C语言入门经典第4版课后练习参考答案

目录第1章C语言编程............................................................................... 错误!未定义书签。

练习1.1 .............................................................................................. 错误!未定义书签。

练习1.2 .............................................................................................. 错误!未定义书签。

练习1.3 .............................................................................................. 错误!未定义书签。

第2章编程初步 .................................................................................. 错误!未定义书签。

习题2.1 .............................................................................................. 错误!未定义书签。

习题2.2 .............................................................................................. 错误!未定义书签。

习题2.3 .............................................................................................. 错误!未定义书签。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档