C语言程序设计现代方法(第二版)习题答案
《C语言程序设计教程(第二版)》习题答案

1 【C语言】《C语言程序设计教程(第二版)》习题答案说明1. 本文所指的《C语言程序设计教程(第二版)》是李凤霞主编、北京理工大学出版社出版的,绿皮。
2 第1章程序设计基础知识一、单项选择题(第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==100&&i*5+j*3+k==100)printf(" %d %d %d\n",i,j,k*3);}执行结果:cock hen chick4 18 788 11 8112 4 843.现计算斐波那契数列的前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 123 5 8 13 21 34 5589 144 233 377 610 987 1597 2584 4181 6765 4.源程序:#include "math.h";main(){double x,x0,deltax;x=1.5;do {x0=pow(x+1,1./3);deltax=fabs(x0-x);x=x0;}while(deltax>1e-12);printf("%.10f\n",x);}执行结果:1.32471795725.源程序略。
C语言程序设计(第二版)习题参考答案

6.设 c 为字符型变量值为‘A’,a 为整型变量值为 97,执行语句“putchar(c);putchar(a);”
后,输出结果为( )。
A.Aa
B.A97
C.A9
D.aA
7.已知字母 A 的 ASCII 码值为 65,以下语句段的输出结果是( )。
char c1='A',c2='Y'; printf("%d,%d\n",c1,c2);
printf("*****************************\n");
}
2.仿照例 1.2 编程,输入一个整数,计算这个数的平方。
解:#include<stdio.h>
void main()
{ int a,z;
printf("请输入一个整数:\n");
scanf("%d",&a);
z=a*a;
。
解:k=123
f=8765.4
6.使用 pow()函数时,程序的开头必须写一条预处理命令:
。
解:#include <math.h>
5.填空题。
(1)int i=123,j=45;
函数 printf("%d,%d\n",i,j);的输出结果是
。
解:123,45
4
(2)int i=123; float x=-45.678;
void main()
{ float r,h,s,v;
printf("Please input r and h:\n");
scanf("%f %f",&r,&h);
C语言程序设计现代方法第2版课后习题答案

C语⾔程序设计现代⽅法第2版课后习题答案C语⾔程序设计:现代⽅法[美]K. N. King 著课后习题答案C语⾔的经典之作“近10年来*好的⼀部C语⾔著作”讨论了标准C和C标准库的全部特性强调扫⼀扫⽂末在⾥⾯回复答案+C语⾔程序设计:现代⽅法⽴即得到答案软件⼯程和现代编程理念突出⼯业界的**实践、实际经验和编程风格已被包括哈佛⼤学、⿇省理⼯学院、斯坦福⼤学等全球200多所学校采⽤为教材时⾄今⽇,C语⾔仍然是计算机领域的通⽤语⾔之⼀,但今天的C语⾔已经和初的时候⼤不相同了。
本书主要的⼀个⽬的就是通过⼀种“现代⽅法”来介绍C语⾔,书中强调标准C,强调软件⼯程,不再强调“⼿⼯优化”。
这⼀版中紧密结合了C99标准,并与C89标准进⾏对照,补充了C99中的*特性。
本书分为C语⾔的基础特性、C语⾔的⾼级特性、C语⾔标准库和参考资料4个部分。
每章末尾都有⼀个“问与答”⼩节给出⼀系列与该章内容相关的问题及答案,此外还包含适量的习题。
本书是为⼤学本科阶段的C语⾔课程编写的教材,同时也⾮常适合作为其他课程的辅助⽤书。
K. N. King 世界知名的计算机程序设计教育家,现为佐治亚州⽴⼤学数学与计算机科学系副教授。
他拥有耶鲁⼤学计算机科学硕⼠学位,加州⼤学伯克利分校计算机科学博⼠学位,曾任教于佐治亚理⼯学院。
除本书外,他还撰写了⼴受欢迎的著作Modula-2: A Complete Guide 和Java Programming: From the Beginning,并在Dr.Dobb's Journal等权威杂志上发表了许多⽂章。
业余时间,King教授还在多部电影中扮演过⾓⾊。
⽬ 录第1章 C语⾔概述 11.1 C语⾔的历史 11.1.1 起源 11.1.2 标准化 11.1.3 基于C的语⾔ 21.2 C语⾔的优缺点 31.2.1 C语⾔的优点 31.2.2 C语⾔的缺点 31.2.3 ⾼效地使⽤C语⾔ 4问与答 5第2章 C语⾔基本概念 7显⽰全部信息“我完全沉浸在阅读的过程中,我迫切地想⽤这本书作为授课教材。
C语言程序设计(第二版)习题参考答案

printf("%d*%d=%d\n",a,a,z);
}
*3.仿照例 1.3 编程,输入两个数后,输出其中较小值。
解:#include<stdio.h>
float min(float x, float y)
{ float m;
if (x<y) m=x;
else m=y;
return m;
}
void main()
6.常用字符的 ASCII 码值从小到大的排列规律是:空格、阿拉伯数字、大写英文字母、
小写英文字母。
解:1.F 2.T 3.T 4.T 5.T
6.T
二、单选题
1.在计算机中,最适合进行数值加减运算的数值编码是
。
A. 原码
B. 反码
C. 补码
D. 移码
2.已知英文小写字母 m 的 ASCII 码为十进制数 109,则英文小写字母 y 的 ASCII 码为
面积 s(s x)(s y)(s z)
其中 s
x yz 2
程序如下: #include <stdio.h>
#include <math.h>
void main()
{
double x,y,z,s,dime;
scanf("%lf%lf%lf",&x,&y,&z);
dime=sqrt(s*(s-x)*(s-y)*(s-z));
程序如下:
#include <stdio.h>
void main()
{
int a,b,c,s,z;
printf("Please input a b c:\n");
C语言程序设计:现代方法(第2版)第二章全部习题答案

C语⾔程序设计:现代⽅法(第2版)第⼆章全部习题答案前⾔本⼈在通过《C语⾔程序设计:现代⽅法(第2版)》⾃学C语⾔时,发现国内并没有该书完整的课后习题答案,所以就想把⾃⼰在学习过程中所做出的答案分享出来,以供⼤家参考。
这些答案是本⼈⾃⼰解答,并参考GitHub上相关的分享和相关资料。
因为并没有权威的答案来源,所以可能会存在错误的地⽅,如有错误还希望⼤家能够帮助指出。
第⼆章练习题和编程题答案练习题2.2节1.建⽴并运⾏由Kernighan和Ritchie编写的著名的“hello world”程序:1 #include <stdio.h>2 int main(void)3 {4 printf("hello world!\n");5 }在编译时是否有警告信息?如果有,需要如何改进呢?答:如果使⽤部分⽐较⽼的编译器如turbo C等,会⽣成警告信息,指出main函数并没有返回任何值。
这是因为在声明main函数时使⽤了int,表明函数会返回⼀个整型数值,这个数值为任意(因为main后⾯是void)。
但是上⾯的代码中并没有return语句,所以并不会返回任何值。
加⼊return语句后便可消除警告。
1 /* 加⼊return语句后的版本 */2 #include <stdio.h>3 int main(void)4 {5 printf("Hello world!\n");6 return 0;7 }如果是使⽤IDE中的编译器,且IDE版本⽐较新(如本⼈⽬前使⽤的Code:Blocks)即使没有return语句,编译器也不会⽣成警告信息。
2.思考下⾯的程序:1 #include <stdio.h>2int main(void)3 {4 printf("Parkinson's Law:\nWork expands so as to ");5 printf("fill the time\n");6 printf("available for its completion.\n");7return0;8 }(a)请指出程序中的指令和语句。
c程序设计(第二版)课后习题答案

C 语言程序设计(第二版) 课后习题参考答案
putchar(c2);//将变量 c2 的值输出 printf("\n"); printf("%c%c\n",c1,c2);//用 printf 输出 c1、c2 的值 printf("%d,%d\n",c1,c2);//输出 c1,c2 的 ASCII 码 } 第四章 【习题 4.5】 /*有三个整数 a,b,c,由键盘输入,输出其中最大的数,请编程序。*/ /*变量:三个整数 a、b、c,中间变量 t,最大值 max*/ #include<stdio.h> void main() { int a,b,c,t,max; printf("please input a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c); t=a>b?a:b;//比较 a 与 b 的大小,将大者赋给中间变量 t max=t>c?t:c;//比较 t 与 c 的大小,将大者赋给最大值 max printf("the max is:\n"); printf("%d\n",max); } 【习题 4.6】 /*给出一百分制成绩,要求输出成绩等级'A'、'B'、'C'、'D'、'E'。90 分以上为'A',80~89 分为 'B',70~79 分为'C',60~69 分为'D',60 分以下为'E'。*/ #include<stdio.h> void main() { int score; printf("please input the score:(0-100)\n"); scanf("%d",&score); if(score>=90&&score<=100) printf("A");//如果成绩大于 90 分,输出 A else if(score>=80&&score<=89) printf("B");//如果成绩在 80~89 之间,输出 B else if(score>=70&&score<=79) printf("C");//如果成绩在 70~79 之间,输出 C else if(score>=60&&score<=69) printf("D");//如果成绩在 60~69 之间,输出 D else printf("E");//成绩小于 60 分时,输出 E printf("\n"); } 【习题 4.7】 /*给一个不多于 5 位的正整数,要求:(1)求出它是几位数;(2)分别输出每一个数字;(3)按 逆顺序输出各位数字,例如原数为 321,应输出 123.*/ /*变量:正整数 x、万位数 a、千位位数 b、百位数 c、十位数 d、个位数 e*/ #include <stdio.h>
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,23.000000,2.500000,-8.0000002. abc defghwhy is21+35equal 523.34214. aaa项目实训题1.定义一个符号常量m为5和一个变量n值为2,把它们的乘积输出。
#define m 5main(){ 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=32. z=12.7000002 13 3 2 bb cc abc n3. 1 2 1a2 1 2三.编程题编程题解:#include stdio.hmain(){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,3crtotal salary:10.8000002.编写一个程序求出任意一个输入字符的ascii码解:#include stdio.hmain(){char c;printf(input a string:);scanf(%c,c);printf(%c ascii is %d\n,c,c);}本程序运行结果为:input a string:acra ascii is 973、编写一个程序用于水果店售货员算帐:已知苹果每斤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,4fu kuan=100result:fukuan=100.00yuanshoukuan= 18.50yuanzhaohui= 81.50yuan项目实训1.假设银行定期存款的年利率rate为2.25%,并已知存款期为n 年,存款本金为capital元,试编程计算n年后可得到本利之和deposit。
C语言程序设计(第二版)习题参考答案

C语言程序设计习题参考答案习题 1一、判断题1.在计算机中,小数点和正负号都有专用部件来保存和表示。
2.二进制是由0和1两个数字组成的进制方式。
3.二进制数的逻辑运算是按位进行的,位与位之间没有进位和借位的关系。
4.在整数的二进制表示方法中,0的原码、反码都有两种形式。
5.有符号数有三种表示法:原码、反码和补码。
6.常用字符的ASCII码值从小到大的排列规律是:空格、阿拉伯数字、大写英文字母、小写英文字母。
解:1.F2.T 3.T 4.T 5.T 6.T二、单选题1.在计算机中,最适合进行数值加减运算的数值编码是。
A. 原码B. 反码C. 补码D. 移码2.已知英文小写字母m的ASCII码为十进制数109,则英文小写字母y的ASCII码为十进制数。
A. 112B. 120C. 121D. 1223.关于ASCII码,在计算机中的表示方法准确地描述是。
A. 使用8位二进制数,最右边一位为1B. 使用8位二进制数,最左边一位为1C. 使用8位二进制数,最右边一位为0D. 使用8位二进制数,最左边一位为04.设在机器字长4位,X=0111B,Y=1011B,则下列逻辑运算中,正确的是___________。
A. X∧Y=1000B. X∨Y=1111C. X⊕Y=0011D. ¯Y=10005.下列叙述中正确的是()。
A.高级语言就是机器语言B.汇编语言程序、高级语言程序都是计算机程序,但只有机器语言程序才是计算机可以直接识别并执行的程序C.C语言因为具有汇编语言的一些特性,所以是汇编语言的一种D.C源程序经过编译、连接,若正确,执行后就能得到正确的运行结果6.用C语言编写的源程序经过编译后,若没有产生编译错误,则系统将()。
A.生成可执行文件B.生成目标文件C.输出运行结果D.自动保存源文件7.下列叙述中不正确的是()。
A.main函数在C程序中必须有且只有一个B. C程序的执行从main函数开始,所以main函数必须放在程序最前面C. 函数可以带参数,也可以不带参数。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Chapter 2Answers to Selected Exercises2. [was #2] (a) The program contains one directive (#include) and four statements (three calls of printf and one return).(b)Parkinson's Law:Work expands so as to fill the timeavailable for its completion.3. [was #4]#include <>"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] Here's one possible program:#include <>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: 5618848Value of j: 0Value of k: 6844404Value of x:Value of y:Value of z:The values printed depend on many factors, so the chance that you'll 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 Projects4. [was #8; modified]#include <>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 <>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 Exercises2. [was #2](a) printf("%", x);(b) printf("%", x);(c) printf("%-8.3f", x);(d) printf("%6.0f", x);5. [was #8] The values of x, i, and y will be , 45, and .6, respectively. Answers to Selected Programming Projects1. [was #4; modified]#include <>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 <>`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 4Answers to Selected Exercises2. [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 013. [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 <>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 5Answers to Selected Exercises2. [was #2](a) 1(b) 1(c) 1(d) 14. [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 isonetwosince there are no break statements after the cases.Answers to Selected Programming Projects2. [was #6]#include <>int main(void){int hours, minutes;-printf("Enter a 24-hour time: ");scanf("%d:%d", &hours, &minutes);printf("Equivalent 12-hour time: ");if (hours == 0)printf("12:%.2d AM\n", minutes);else if (hours < 12)printf("%d:%.2d AM\n", hours, minutes);else if (hours == 12)%printf("%d:%.2d PM\n", hours, minutes);elseprintf("%d:%.2d PM\n", hours - 12, minutes);return 0;}4. [was #8; modified]#include <>int main(void)《{int speed;printf("Enter a wind speed in knots: ");scanf("%d", &speed);if (speed < 1)printf("Calm\n");else if (speed <= 3)printf("Light air\n");]else if (speed <= 27)printf("Breeze\n");else if (speed <= 47)printf("Gale\n");else if (speed <= 63)printf("Storm\n");elseprintf("Hurricane\n");return 0;-}6. [was #10]#include <>int main(void){int check_digit, d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5,first_sum, second_sum, total;printf("Enter the first (single) digit: ");】scanf("%1d", &d);printf("Enter first group of five digits: ");scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5); printf("Enter second group of five digits: ");scanf("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5); printf("Enter the last (single) digit: ");scanf("%1d", &check_digit);first_sum = d + i2 + i4 + j1 + j3 + j5;second_sum = i1 + i3 + i5 + j2 + j4;)total = 3 * first_sum + second_sum;if (check_digit == 9 - ((total - 1) % 10))printf("VALID\n");elseprintf("NOT VALID\n");return 0;}10. [was #14]·#include <>int main(void){int grade;printf("Enter numerical grade: ");scanf("%d", &grade);if (grade < 0 || grade > 100) {<printf("Illegal grade\n");return 0;}switch (grade / 10) {case 10:case 9: printf("Letter grade: A\n");break;case 8: printf("Letter grade: B\n");break;;case 7: printf("Letter grade: C\n");break;case 6: printf("Letter grade: D\n");break;case 5:case 4:case 3:case 2:case 1:case 0: printf("Letter grade: F\n");<break;}return 0;}Chapter 6Answers to Selected Exercises4. [was #10] (c) is not equivalent to (a) and (b), because i is incremented before the loop body is executed.10. [was #12] Consider the following while loop:}while (…) {…continue;…}The equivalent code using goto would have the following appearance:while (…) {…goto loop_end;…;loop_end: ; /* null statement */}12. [was #14]for (d = 2; d * d <= n; d++)if (n % d == 0)break;The if statement that follows the loop will need to be modified as well:if (d * d <= n)printf("%d is divisible by %d\n", n, d);else@printf("%d is prime\n", n);14. [was #16] The problem is the semicolon at the end of the first line. If we remove it, the statement is now correct:if (n % 2 == 0)printf("n is even\n");Answers to Selected Programming Projects2. [was #2]#include <>int main(void){:int m, n, remainder;printf("Enter two integers: ");scanf("%d%d", &m, &n);while (n != 0) {remainder = m % n;m = n;n = remainder;}【printf("Greatest common divisor: %d\n", m);return 0;}4. [was #4]#include <>int main(void){~float commission, value;printf("Enter value of trade: ");scanf("%f", &value);while (value != 0.0f) {if (value < 2500.00f)commission = 30.00f + .017f * value;else if (value < 6250.00f)commission = 56.00f + .0066f * value;—else if (value < 20000.00f)commission = 76.00f + .0034f * value;else if (value < 50000.00f)commission = 100.00f + .0022f * value;else if (value < 500000.00f)commission = 155.00f + .0011f * value;elsecommission = 255.00f + .0009f * value;if (commission < 39.00f)~commission = 39.00f;printf("Commission: $%.2f\n\n", commission);printf("Enter value of trade: ");scanf("%f", &value);}return 0;}-6. [was #6]#include <>int main(void){int i, n;printf("Enter limit on maximum square: ");scanf("%d", &n);'for (i = 2; i * i <= n; i += 2)printf("%d\n", i * i);return 0;}8. [was #8]#include <>int main(void){`int i, n, start_day;printf("Enter number of days in month: ");scanf("%d", &n);printf("Enter starting day of the week (1=Sun, 7=Sat): "); scanf("%d", &start_day);/* print any leading "blank dates" */for (i = 1; i < start_day; i++)printf(" ");·/* now print the calendar */for (i = 1; i <= n; i++) {printf("%3d", i);if ((start_day + i - 1) % 7 == 0)printf("\n");}return 0;}:Chapter 7Answers to Selected Exercises3. [was #4] (b) is not legal.4. [was #6] (d) is illegal, since printf requires a string, not a character, as its first argument.10. [was #14] unsigned int, because the (int) cast applies only to j, not j * k. 12. [was #16] The value of i is converted to float and added to f, then the result is converted to double and stored in d.14. [was #18] No. Converting f to int will fail if the value stored in f exceeds the largest value of type int.Answers to Selected Programming Projects1. [was #2] short int values are usually stored in 16 bits, causing failure at 182. int and long int values are usually stored in 32 bits, with failure occurring at 46341. …2. [was #8]#include <>int main(void){int i, n;char ch;printf("This program prints a table of squares.\n");printf("Enter number of entries in table: ");&scanf("%d", &n);ch = getchar();/* dispose of new-line character following number of entries *//* could simply be getchar(); */for (i = 1; i <= n; i++) {printf("%10d%10d\n", i, i * i);if (i % 24 == 0) {printf("Press Enter to continue...");ch = getchar(); /* or simply getchar(); */》}}return 0;}5. [was #10]#include <>#include <>int main(void){{int sum = 0;char ch;printf("Enter a word: ");while ((ch = getchar()) != '\n')switch (toupper(ch)) {case 'D': case 'G':sum += 2; break;|case 'B': case 'C': case 'M': case 'P':sum += 3; break;case 'F': case 'H': case 'V': case 'W': case 'Y':sum += 4; break;case 'K':sum += 5; break;case 'J': case 'X':sum += 8; break;case 'Q': case 'Z':sum += 10; break;,default:sum++; break;}printf("Scrabble value: %d\n", sum);return 0;}6. [was #12]#include <>/int main(void){printf("Size of int: %d\n", (int) sizeof(int));printf("Size of short: %d\n", (int) sizeof(short));printf("Size of long: %d\n", (int) sizeof(long));printf("Size of float: %d\n", (int) sizeof(float));printf("Size of double: %d\n", (int) sizeof(double));printf("Size of long double: %d\n", (int) sizeof(long double));}return 0;}Since the type of a sizeof expression may vary from one implementation to another, it's necessary in C89 to cast sizeof expressions to a known type before printing them. The sizes of the basic types are small numbers, so it's safe to cast them to int. (In general, however, it's best to cast sizeof expressions to unsigned long and print them using %lu.) In C99, we can avoid the cast by using the %zu conversion specification. Chapter 8Answers to Selected Exercises1. [was #4] The problem with sizeof(a) / sizeof(t) is that it can't easily be checked for correctness by someone reading the program. (The reader would have to locate the declaration of a and make sure that its elements have type t.)2. [was #8] To use a digit d (in character form) as a subscript into the array a, we would write a[d-'0']. This assumes that digits have consecutive codes in the underlying character set, which is true of ASCII and other popular character sets.7. [was #10]const int segments[10][7] = {{1, 1, 1, 1, 1, 1},#{0, 1, 1},{1, 1, 0, 1, 1, 0, 1},{1, 1, 1, 1, 0, 0, 1},{0, 1, 1, 0, 0, 1, 1},{1, 0, 1, 1, 0, 1, 1},{1, 0, 1, 1, 1, 1, 1},{1, 1, 1},{1, 1, 1, 1, 1, 1, 1},{1, 1, 1, 1, 0, 1, 1}}; Answers to Selected Programming Projects|2. [was #2]#include <>int main(void){int digit_count[10] = {0};int digit;long n;printf("Enter a number: ");<scanf("%ld", &n);while (n > 0) {digit = n % 10;digit_count[digit]++;n /= 10;}printf ("Digit: ");for (digit = 0; digit <= 9; digit++)—printf("%3d", digit);printf("\nOccurrences:");for (digit = 0; digit <= 9; digit++)printf("%3d", digit_count[digit]);printf("\n");return 0;}5. [was #6]#include <>{#define NUM_RATES ((int) (sizeof(value) / sizeof(value[0])))#define INITIAL_BALANCEint main(void){int i, low_rate, month, num_years, year;double value[5];printf("Enter interest rate: ");…scanf("%d", &low_rate);printf("Enter number of years: ");scanf("%d", &num_years);printf("\nYears");for (i = 0; i < NUM_RATES; i++) {printf("%6d%%", low_rate + i);value[i] = INITIAL_BALANCE;}printf("\n");'for (year = 1; year <= num_years; year++) {printf("%3d ", year);for (i = 0; i < NUM_RATES; i++) {for (month = 1; month <= 12; month++)value[i] += ((double) (low_rate + i) / 12) / * value[i];printf("%7.2f", value[i]);}printf("\n");}!return 0;}8. [was #12]#include <>#define NUM_QUIZZES 5#define NUM_STUDENTS 5int main(void)*{int grades[NUM_STUDENTS][NUM_QUIZZES];int high, low, quiz, student, total;for (student = 0; student < NUM_STUDENTS; student++) {printf("Enter grades for student %d: ", student + 1);for (quiz = 0; quiz < NUM_QUIZZES; quiz++)scanf("%d", &grades[student][quiz]);}—printf("\nStudent Total Average\n");for (student = 0; student < NUM_STUDENTS; student++) {printf("%4d ", student + 1);total = 0;for (quiz = 0; quiz < NUM_QUIZZES; quiz++)total += grades[student][quiz];printf("%3d %3d\n", total, total / NUM_QUIZZES);}printf("\nQuiz Average High Low\n");¥for (quiz = 0; quiz < NUM_QUIZZES; quiz++) {printf("%3d ", quiz + 1);total = 0;high = 0;low = 100;for (student = 0; student < NUM_STUDENTS; student++) {total += grades[student][quiz];if (grades[student][quiz] > high)high = grades[student][quiz];if (grades[student][quiz] < low)@low = grades[student][quiz];}printf("%3d %3d %3d\n", total / NUM_STUDENTS, high, low); }return 0;}Chapter 9Answers to Selected Exercises[2. [was #2]int check(int x, int y, int n){return (x >= 0 && x <= n - 1 && y >= 0 && y <= n - 1);}4. [was #4]int day_of_year(int month, int day, int year){int num_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int day_count = 0, i;;for (i = 1; i < month; i++)day_count += num_days[i-1];/* adjust for leap years, assuming they are divisible by 4 */if (year % 4 == 0 && month > 2)day_count++;return day_count + day;}—Using the expression year % 4 == 0 to test for leap years is not completely correct. Centuries are special cases: if a year is a multiple of 100, then it must also be a multiple of 400 in order to be a leap year. The correct test isyear % 4 == 0 && (year % 100 != 0 || year % 400 == 0)6. [was #6; modified]int digit(int n, int k){int i;for (i = 1; i < k; i++)n /= 10;;return n % 10;}8. [was #8] (a) and (b) are valid prototypes. (c) is illegal, since it doesn't specify the type of the parameter. (d) incorrectly specifies that f returns an int value in C89; in C99, omitting the return type is illegal.10. [was #10](a)int largest(int a[], int n){int i, max = a[0];for (i = 1; i < n; i++):if (a[i] > max)max = a[i];return max;}(b)int average(int a[], int n){int i, avg = 0;$for (i = 0; i < n; i++)avg += a[i];return avg / n;}(c)int num_positive(int a[], int n){int i, count = 0;{for (i = 0; i < n; i++)if (a[i] > 0)count++;return count;}15. [was #12; modified]double median(double x, double y, double z) {double result;:if (x <= y)if (y <= z) result = y;else if (x <= z) result = z;else result = x;else {if (z <= y) result = y;else if (x <= z) result = x;else result = z;},return result;}17. [was #14]int fact(int n){int i, result = 1;for (i = 2; i <= n; i++)result *= i;。