c语言程序设计教程第二版课后答案
c语言程序设计教程第二版课后答案
【篇一:c语言程序设计(第2版)-- 课后题答案】p> 参考答案
第1章进入c语言程序世界
二、
1.i love china!
printf(we are students.\n)
2.6
项目实训题参考答案
1.编写一个c程序,输出以下信息:
* * * * * * * * * * * * * * * * * * * *
i am a student!
* * * * * * * * * * * * * * * * * * * *
main()
{ printf(********************\n);
printf( i am a student!\n);
printf(********************\n);
}
2.已知立方体的长、宽、高分别是10cm、20cm、15cm,编写程序,求立方体体积。解:
main()
{
int a,b,c,v;
a=10;
b=20;
c=15;
v=a*b*c;
printf(v=%d,v);
}
本程序运行结果为:
v=3000
第2章编制c程序的基础知识
一选择题
c b a b a c c
二操作题
,2,-8,2
3.000000,2.500000,-8.000000
2. abc de
fgh
why is21+35equal 52
3.
3
4
2
1
4. aa
a
项目实训题
1.定义一个符号常量m为5和一个变量n值为2,把它们的乘积输出。
#define m 5
main()
{ int n,c;
n=2; c=m*n;
printf(%d\n,c);}
2.编程求下面算术表达式的值。
(1)x+a%3*(int)(x+y)%2/4,设x=2.5,a=7,y=4.7;
(2)(float)(a+b)/2+(int)x%(int)y,设a=2,b=3,x=3.5,y=2.5。(1)main()
{ int a=7;
float x=2.5,y=4.7;
printf(%f\n,x+a%3*(int)(x+y)%2/4);}
(2)main()
{ int a=2,b=3;
float x=3.5,y=2.5;
printf(%f\n,(float)(a+b)/2+(int)x%(int)y);}
第三章顺序结构程序设计
一选择题
a c d c c
二操作题
1. x=3,a=2,b=3
2. z=12.700000
2 1
3 3 2 bb cc abc n
3. 1 2 1
a
2 1 2
三.编程题
编程题
解:
#include stdio.h
main()
{
float sj,gz,yfgz;
printf(time,salary:);
scanf(%f,%f,sj,gz);
yfgz=sj*gz*0.9;
printf(total salary:%f\n,yfgz);
}
本程序运行结果为:
time,salary:4,3cr
total salary:10.800000
2.编写一个程序求出任意一个输入字符的ascii码
解:
#include stdio.h
main()
{
char c;
printf(input a string:);
scanf(%c,c);
printf(%c ascii is %d\n,c,c);
}
本程序运行结果为:
input a string:acr
a ascii is 97
3、编写一个程序用于水果店售货员算帐:已知苹果每斤2.50元,鸭梨每斤1.80元,香蕉每斤2元,橘子每斤1.6元,要求输入各类水果的重量,打印出应付
3
解:
main()
{
float p,y,x,j,ys,g,fk;
printf(apple,pear,banana,orange(weight)=);
scanf(%f,%f,%f,%f,p,y,x,j);
ys=2.5*p+1.8*y+2*x+1.6*j;
printf(fu kuan=);
scanf(%f,g);
fk=g-ys;
printf(result:\n);
printf(fukuan=%6.2fyuan\nshoukuan=%6.2fyuan\nzhaohui=%6. 2fyuan\n,g,ys,fk);
}
本程序运行结果为:
apple,pear,banana,orange(weight)=1,2,3,4
fu kuan=100
result:
fukuan=100.00yuan
shoukuan= 18.50yuan
zhaohui= 81.50yuan
项目实训
1.假设银行定期存款的年利率rate为2.25%,并已知存款期为n 年,存款本金为capital元,试编程计算n年后可得到本利之和deposit。
#includemath.h
main()
{ int n;
float rate=0.0225,capital,deposit;
scanf(%d,%f,n,capital);
deposit=capital*pow(1+rate,n);
printf(deposit=%f\n,deposit); }
2.将一个三位数整数,正确分离出它的个位、十位和百位数字,并分别在屏幕上输出。
main()
{ int n,a,b,c;
scanf(%3d,n);
a=n/100;
4
b=n%100/10;
c=n%100%10/1;
printf(a=%d,b=%d,c=%d\n,a,b,c); }
第四章选择结构程序设计
一、略
二、b b a b c b a
三、1. 1 0
2. 2 3 2 2
3. 10 20 0
4. ch=’a’ch=’z’||ch=’a’ch=’z’
ch=’0’ch=’9’
ch==’ ’
5. -1
四、上机操作
1. 从键盘输入一个英文字母,如果是大写字母,则将它变为小写字母输出;如果是小写字母,则将其变为大写字母输出。
#includestdio.h
main()
{char ch;
ch=getchar();
if(ch=ach=z) ch+=32;
else if(ch=ach=z) ch-=32;
putchar(ch);
putchar(\n); }
2. 根据输入的x值依据下列表达式,计算y的值。 4+x (x-1)解:
main()
{
float x,y;
scanf(%f,x);
5
【篇二:《c语言程序设计教程(第二版)》习题答案】txt>一、单项选择题(第23页)
1-4.cbbc 5-8.daca
二、填空题(第24页)
1.判断条件
2.面向过程编程
3.结构化
4.程序
5.面向对象的程序设计语言 7.有穷性 8.直到型循环 9.算法 10.可读性 11.模块化 12.对问题的分析和模块的划分
三、应用题(第24页)
2.源程序:
main()
{int i,j,k; /* i:公鸡数,j:母鸡数,k:小鸡数的1/3 */
printf(cock hen chick\n);
for(i=1;i=20;i++)
for(j=1;j=33;j++)
for(k=1;k=33;k++)
if (i+j+k*3==100i*5+j*3+k==100)
printf( %d %d %d\n,i,j,k*3);}
执行结果:
cock hen chick
4 18 78
8 11 81
12 4 84
3.现计算斐波那契数列的前20项。
递推法源程序:
main()
{long a,b;int i;
a=b=1;
for(i=1;i=10;i++) /*要计算前30项,把10改为15。*/ {printf(%8ld%8ld,a,b);
a=a+b;b=b+a;}}
递归法源程序:
main()
{int i;
for(i=0;i=19;i++)
printf(%8d,fib(i));}
fib(int i)
{return(i=1?1:fib(i-1)+fib(i-2));}
执行结果:
1 1
2
3 5 8 13 21 3
4 55
89 144 233 377 610 987 1597 2584 4181 6765
4.源程序:
#include math.h;
main()
{double x,x0,deltax;
do {x0=pow(x+1,1./3);
deltax=fabs(x0-x);
x=x0;
}while(deltax1e-12);
printf(%.10f\n,x);}
1.3247179572
5.源程序略。(分子、分母均构成斐波那契数列)
结果是32.66026079864
6.源程序:
main()
{int a,b,c,m;
printf(please input a,b and c:);
scanf(%d %d %d,a,b,c);
if(ab){m=a;a=b;b=m;}
if(ac){m=a;a=c;c=m;}
if(bc){m=b;b=c;c=m;}
printf(%d %d %d\n,a,b,c);}
执行结果:
please input a,b and c:123 456 789
789 456 123
7.源程序:
main()
{int a;
scanf(%d,a);
printf(a%21==0?yes:no);}
执行结果:
42
yes
3 第2章 c语言概述
一、单项选择题(第34页)
1-4.bdcb 5-8.aabc
(第35页)
1.主
2.c编译系统
3.函数函数
4.输入输出
5.头
6. .obj
7.库函数
8.文本
三、应用题(第36页)
5.sizeof是关键字,stru、_aoto、file、m_i_n、hello、abc、sin90、x1234、until、cos2x、s_3是标识符。
8.源程序:
{int a,b,c;
scanf(%d %d,a,b);
c=a;a=b;b=c;
printf(%d %d,a,b);}
12 34
34 12
4 第3章数据类型与运算规则
一、单项选择题(第75页)
1-5.dbacc 6-10.dbdbc 11-15.adccc 16-20.cbccd 21-25.addbc 26-27.ab
二、填空题(第77页)
8.65,89
三、应用题(第78页)
1.10 9
2.执行结果:
11
12
1
5 第4章顺序结构程序设计
一、单项选择题(第90页)
1-5.dcdad 6-10.bacbb
二、填空题(第91页)
1.一;
2. 5.169000
3.(1)-2002500 (2)i=-200,j=2500 (3)i=-200
j=2500 4.a=98,b=765.000000,c=4321.000000 5.略 6.0,0,3 7.3
8.scanf(%lf%lf%lf,a,b,c); 9. 13 13.000000,13.000000
10.a=a^c;c=c^a;a=a^c;(这种算法不破坏b的值,也不用定义中间变量。)
三、编程题(第92页)
1.仿照教材第27页例2-1。
2.源程序:
main()
scanf(%d:%d,h,m);
printf(%d\n,h*60+m);}
执行结果:
9:23
563
3.源程序:
main()
{int a[]={-10,0,15,34},i;
for(i=0;i=3;i++)
printf(%d\370c=%g\370f\t,a[i],a[i]*1.8+32);}
4.源程序:
main()
{double pi=3.14159265358979,r=5;
printf(r=%lg a=%.10lf s=%.10lf\n,r,2*pi*r,pi*pi*r);}
执行结果:
r=5 a=31.4159265359 s=49.3480220054
5.源程序:
#include math.h;
main()
{double a,b,c;
scanf(%lf%lf%lf,a,b,c);
if (a+bca+cbb+ca)
{double s=(a+b+c)/2;
printf(ss=%.10lf\n,sqrt(s*(s-a)*(s-b)*(s-c)));}
else printf(data error!);}
执行结果:
4 5 6
ss=9.9215674165
6.源程序:
main()
{int a=3,b=4,c=5;float d=1.2,e=2.23,f=-43.56;
printf(a=%3d,b=%-4d,c=**%d\nd=%g\ne=%6.2f\nf=%-
10.4f**\n,a,b,c,d,e,f);}
7.源程序:
main()
{int a,b,c,m;
scanf(%d %d %d,a,b,c);
m=a;a=b;b=c;c=m;
printf(%d %d %d\n,a,b,c);}
执行结果:
5 6 7
6 7 5
8.源程序:
main()
{int a,b,c;
scanf(%d %d %d,a,b,c);
printf(average of %d,%d and %d is %.2f\n,a,b,c,(a+b+c)/3.); 执行结果:
average of 6,7 and 9 is 7.33
9.不能。修改后的源程序如下:
main()
{int a,b,c,x,y;
scanf(%d %d %d,a,b,c);
x=a*b;y=x*c;
printf(a=%d,b=%d,c=%d\n,a,b,c);
printf(x=%d,y=%d\n,x,y);}
6 第5章选择结构程序设计
一、单项选择题(第113页)
1-4.dcbb 5-8.dabd
二、填空题(第115页)
1.非0 0
2.k==0
3.if (abs(x)4) printf(%d,x);else printf(error!);
4.if((x=1x=10||x=200x=210)x1)printf(%d,x);
5.k=1 (原题最后一行漏了个d,如果认为原题正确,则输出k=%。)
6. 8! right!11
7.$$$a=0
8.a=2,b=1
三、编程题(第116页)
1.有错。正确的程序如下:
main()
{int a,b,c;
scanf(%d,%d,%d,a,b,c);
printf(min=%d\n,ab?bc?c:b:ac?c:a);}
2.源程序:
main()
{unsigned long a;
scanf(%ld,a);
for(;a;printf(%d,a%10),a/=10);}
执行结果:
12345
54321
3.(1)源程序:
【篇三:c语言程序设计现代方法(第二版)习题答案】answers to selected exercises
2. [was #2] (a) the program contains one directive (#include) and four statements (three calls of printf and one return).
parkinsons law:
work expands so as to fill the time
available for its completion.
3. [was #4]
#include stdio.h
int main(void)
{
int height = 8, length = 12, width = 10, volume;
volume = height * length * width;
printf(dimensions: %dx%dx%d\n, length, width, height);
printf(volume (cubic inches): %d\n, volume);
printf(dimensional weight (pounds): %d\n, (volume + 165) / 166);
return 0;
}
4. [was #6] heres one possible program:
#include stdio.h
int main(void)
{
int i, j, k;
float x, y, z;
printf(value of i: %d\n, i);
printf(value of j: %d\n, j);
printf(value of k: %d\n, k);
printf(value of x: %g\n, x);
printf(value of y: %g\n, y);
printf(value of z: %g\n, z);
return 0;
}
when compiled using gcc and then executed, this program produced the following output:
value of i: 5618848
value of j: 0
value of k: 6844404
value of x: 3.98979e-34
value of y: 9.59105e-39
value of z: 9.59105e-39
the values printed depend on many factors, so the chance that youll get exactly these numbers is small.
5. [was #10] (a) is not legal because 100_bottles begins with a digit.
8. [was #12] there are 14 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3, and ;.
answers to selected programming projects
4. [was #8; modified]
#include stdio.h
int main(void)
{
float original_amount, amount_with_tax;
printf(enter an amount: );
scanf(%f, original_amount);
amount_with_tax = original_amount * 1.05f;
printf(with tax added: $%.2f\n, amount_with_tax);
return 0;
}
the amount_with_tax variable is unnecessary. if we remove it, the program is slightly shorter:
#include stdio.h
int main(void)
{
float original_amount;
printf(enter an amount: );
scanf(%f, original_amount);
printf(with tax added: $%.2f\n, original_amount * 1.05f);
return 0;
}
chapter 3
answers to selected exercises
2. [was #2]
(a) printf(%-8.1e, x);
(b) printf(%10.6e, x);
(c) printf(%-8.3f, x);
(d) printf(%6.0f, x);
5. [was #8] the values of x, i, and y will be 12.3, 45, and .6, respectively. answers to selected programming projects
1. [was #4; modified]
#include stdio.h
int main(void)
{
int month, day, year;
printf(enter a date (mm/dd/yyyy): );
scanf(%d/%d/%d, month, day, year);
printf(you entered the date %d%.2d%.2d\n, year, month, day); return 0;
}
3. [was #6; modified]
#include stdio.h
int main(void)
{
int prefix, group, publisher, item, check_digit;
printf(enter isbn: );
scanf(%d-%d-%d-%d-%d, prefix, group, publisher, item,
check_digit);
printf(gs1 prefix: %d\n, prefix);
printf(group identifier: %d\n, group);
printf(publisher code: %d\n, publisher);
printf(item number: %d\n, item);
printf(check digit: %d\n, check_digit);
/* the five printf calls can be combined as follows:
printf(gs1 prefix: %d\ngroup identifier: %d\npublisher
code: %d\nitem number: %d\ncheck digit: %d\n,
prefix, group, publisher, item, check_digit);
*/
return 0;
}
chapter 4
answers to selected exercises
2. [was #2] not in c89. suppose that i is 9 and j is 7. the value of (-i)/j could be either –1 or –2, depending on the implementation. on the other hand, the value of -(i/j) is always –1, regardless of the implementation. in c99, on the other hand, the value of (-i)/j must be equal to the value of -(i/j).
9. [was #6]
(a) 63 8
(b) 3 2 1
(c) 2 -1 3
(d) 0 0 0
13. [was #8] the expression ++i is equivalent to (i += 1). the value of both expressions is i after the increment has been performed. answers to selected programming projects
2. [was #4]
#include stdio.h
int main(void)
{
int n;
printf(enter a three-digit number: );
scanf(%d, n);
printf(the reversal is: %d%d%d\n, n % 10, (n / 10) % 10, n / 100); return 0;
}
chapter 5
answers to selected exercises
2. [was #2]
(a) 1
(b) 1
(c) 1
(d) 1
4. [was #4] (i j) - (i j)
6. [was #12] yes, the statement is legal. when n is equal to 5, it does nothing, since 5 is not equal to –9.
10. [was #16] the output is
onetwo
since there are no break statements after the cases.
answers to selected programming projects
2. [was #6]