资源描述
自测练习奇数题答案 583
自测练习奇数题答案
第1章
1.1节
1.软件
1.2节
1.存储单元0:75.625
存储单元2:0.005
存储单元999:75.62
3.位,字节,存储单元,主存储器,辅助存储器,LAN,WAN
1.3节
1.a、b、c的值相加,把和存储到x;
y除以z,将结果存储到x;
c减b,然后加a,将结果存储到d;
z加1,并将结果存储到z;
celsius加273.15,并将结果存储到kelvin。
3.源程序,编译器,编辑器(字处理器)
1.4节
1.问题需求,分析,设计,实现,测试和验证,维护
1.5节
1.算法细化
(1) 获得以公里表示的距离。
(2) 把距离转化成英里。
2.1 用英里表示的距离是用公里表示的距离的0.621倍。
(3) 显示用英里表示的距离。
第2章
2.1节
1.a. void, double, return
b. printf
c. MAX_ENTRIES, G
d. time, xyz123, this_is_a_long_one
e. Sue's, part#2, "char", #include
3.预处理器,#define和#include
2.2节
1.a. 0.0103 1234500.0 123450.0
b. 1.3e+3 1.2345e+2 4.26e-3
3.double, int, char
2.3节
1.Enter two integers> 5 7
m = 10
n = 21
3.My name is Jane Doe.
I live in Ann Arbor, MI
and I have 11 years of programming experience.
2.4节
1./* This is a comment? */
/* This one seems like a comment doesn’t it */
2.5节
1.a. 22/7为3 7/22为0 22 % 7为1 7 % 22为7
b. 16 / 15 为1 15 / 16为0 16 % 15为1 15 % 16为15
c. 23 / 3 为7 3 / 23为0 23 % 3为2 3 % 23为3
d. 16 / -3 为?? -3 / 16为?? 16 % -3为?? -3 % 16为??
(??意为结果是变化的)
3.a. 3 g. 未定义 m. -3.14159
b. ?? h. 未定义 n. 1.0
c. 1 i. ?? o. 1
d. -3.14159 j. 3 p. 未定义
e. ?? k. -3.0 q. 3
f. 0.0 l. 9 r. 0.75
(??意为结果是变化的)
5.a. white为1.6666… c. orange为0 e. lime为2
b. green为0.6666… d. blue为-3.0 f. purple为0.0
2.6节
1.printf("Salary is %10.2f\n",salary);
3.x■is■■12.34■■i■is■100
i■is■100
x■is■12.3
2.7节
1.在调用scanf获得数据之前先调用printf显示提示。当调用scanf后有数据回应时调用printf。
提示用于交互式程序而不用于批处理程序。批处理程序应该回显输入,交互程序也可以回显输入。
第3章
3.1节
1.问题输入
double hours /* number of hours worked */
double rate /* hourly rate of pay */
问题输出
double gross /* gross salary */
算法
(1) 输入工作时间hours和每小时所付的工资率rate。
(2) 计算总工资gross。
2.1把hours * rate的值赋给gross。
(3) 显示总工资gross。
3.问题输入
double reg_hours /* number of regular hours worked */
double ot_hours /* number of overtime hours worked */
double rate /* hourly rate of pay */
问题输出
double gross /* gross salary */
算法
(1) 输入常规工作时数(reg_hours)、加班时数(ot_hours)和每小时所付工资比率rate。
(2) 计算总工资gross。
2.1 将reg_hours * rate的值赋给gross。
2.2 前一步gross的值再加上ot_hours * 1.5 * rate。
(3) 显示总工资gross。
3.2节
1.a. sqrt(u + v) * pow(w,2)
b. log(pow(x,y))
c. sqrt(pow(x – y, 3))
d. fabs(x * y – w / z)
3.3节
1.设计阶段
3.4节
1.HI MOM被用较大的印刷体字母垂直地输出。
3.5节
1.a. 3141.590000
b. 31.415900
c. ***********
* *
* 31.42 *
* *
***********
d. 3.141590
e. 3.141590
3.函数参数被用来在一个程序的各个模块之间、主程序与其模块之间传递信息。参数使函数更容易被其他函数或程序复用。带参数的函数是建造更大程序的基石。
第4章
4.2节
3.x = 3.0 y = 4.0 z = 2.0 flag = 0
! ( flag || ( y + z >= x – z ))
0 4.0 2.0 3.0 2.0
6.0 1.0
1
1
0
1.1(TRUE)
0(FALSE)
1(TRUE)
1(TRUE)
5.ans为2。
4.3节
1.a. not less
b. greater than
4.4节
1. if (x > y ){
x = x + 10.0;
printf("x Bigger\n");
} else {
printf("x Smaller\n");
printf("y is %.2f\n",y);
}
3. if (engine_type == 'J') {
printf("Jet engine");
speed_category = 1;
} else {
printf("Propellers");
speed_category = 2;
}
4.5节
1.额外的程序常量:
CAP_GALLONS 100 /* maximum number of gallons (in thousands) for basic fee */
额外的程序变量:
int excess /* number of gallons over CAP_GALLONS */
comp_use_charge的改写算法:
(1) used 为 current – previous
(2) if used > CAP_GALLONS
excess 为 used – GAP_GALLONS
use_charge 为 CAP_GALLONS * PRE_1000_CHG +
excess * PER_1000_CHG * 2
else
use_charge 为 used * PER_1000_CHG
4.7节
1.
语句部分
工 资
税
结 果
if (salary < 0.0 )
23500.00
?
23500.00 < 0.00 为假
else if (salary < 15000.00 )
23500.00 < 15000.00 为假
else if (salary < 30000.00 )
23500.00 < 30000.00 为真
tax = (salary – 15000.00)
求解为8500.00
* 0.18
求解为1530.00
+ 2250.00
3780.00
求解为3780.00
3. if ( pH > 7 )
if (pH < 12 )
printf("Alkaline");
else
printf("Very alkaline");
else if (pH == 7 )
printf("Neutral");
else if (pH > 2)
printf("Acidic");
else
printf("Very acidic");
4.8节
1. red
blue
yellow
第5章
5.1节
1.a. 计数循环
(1) 初始化sum为0。
(2) 设置lcv为0。
(3) while lcv<35
(4) 获得下一个考试成绩。
(5) 把考试成绩与sum相加。
(6) 把lcv的值加1。
3.控制文件结束的循环
(1) 初始化count为0。
(2) 获得第一个温度并存储输入状态。
(3) while 输入状态并不表示已到达文件末尾
(4) if温度>100, count加1;
(5) 获得下一个温度并存储输入状态。
5.2节
1.0 10
1 9
2 8
3 7
4 6
5 5
5.3节
1.a. Enter an integer> 5
5
25
125
625
b. Enter an integer> 6
6
36
216
1296
c. Enter an integer> 7
7
49
343
2401
一般地,该循环显示n,n2,n3和n4。
3.count = 0;
sum = 0;
while (count < 5) {
printf("Next number> ");
scanf("%d", &next_num);
sum += next_num;
count += 1;
}
printf("%d numbers were added; ", count);
printf("their sum is %d.\n",sum);
5.4节
1.当n = 8时:
语 句
add
sum
作 用
sum = 0;
0
初始化sum为0
odd = 1;
1
初始化odd为1
odd < n;
1<8为真
sum += odd;
1
sum = 0 + 1
odd +=2
3
odd = 1 + 2
odd < n;
3<8 为真
sum += odd;
4
sum = 1 + 3
odd +=2
5
odd = 3 + 2
odd < n;
5<8 为真
sum +=odd;
9
sum = 4 + 5
odd +=2
7
odd = 5 + 2
odd < n;
7<8为真
sum += odd;
16
sum = 9 + 7
odd += 2
9
odd = 7 + 2
odd < n;
9<8 为假
退出循环
printf("Sum of…
Output: Sum of positive odd numbers less than
8 is 16.
3.两个问题的答案都为0。
5.++i
--j;
n = i * j;
m = i + j;
j--;
p = i + j;
7. a. 1 10
2 8
3 6
4 4
5 2
b. j = 10;
for ( i = 0; i < 5; ++i) {
printf("%d %d\n", i+1,j);
j -= 2;
}
5.5节
1. 任何小于8 000桶的初始化供给。
3. Number of barrels currently in tank> 8350.8
8350.80 barrels are available.
Enter number of gallons removed> 7581.0
After removal of 7581.00 gallons (180.50 barrels),
8170.30 barrels are available.
Enter number of gallons removed> 7984.2
After removal of 7984.20 gallons (190.10 barrels),
Only 7980.20 barrels are left.
*** WARNING ***
Available Supply is less than 10 percent of tank's 80000.00-barrel capacity.
5.6节
1.步骤a——初始化循环控制变量。
步骤c——循环重复的条件。
步骤e——循环控制变量的更新。
5.7节
1.a. *
**
***
****
*****
b. ***
***
***
***
***
5.8节
1.while循环较好,因为do-while在每次迭代时对相同的条件检测两次。
5.9节
1.for (status = fscanf(hdd_file, "%d", &next_hdd);
status == 1;
status = fscanf(hdd_file, "%d",&next_hdd)) {
if (next_hdd > heat_deg_days) {
heat_deg_days = next_hdd;
coldest_mon = ct;
}
++ct;
}
5.10节
1.for ( count = 0; count <= n; ++count) {
printf("DEBUG* * * count = %d\n", count);
sum += count;
printf("DEBUG* * * sum = %d\n", sum);
}
第6章
6.1节
1. void
sum_n_avg(double n1, /* input numbers */
double n2,
double n3,
double *sump, /* output –sum of the three numbers */
double *avgp) /* output –average of the numbers */
3.
引 用
何处合法
数据类型
值
&many
main
int *
指向带灰底的单元
valp
sub
double *
指向带灰底的单元
code
main
char
'g'
&code
main
char *
指向不带底纹的单元
countp
sub
int *
指向带网格的单元
*countp
sub
int
14
*valp
sub
double
17.1
letp
sub
char *
指向不带底纹的单元
&x
main
double *
指向带灰底的单元
6.2节
1.
函数调用
num1
num2
num3
order(&num3, &num2);
8
12
10
order(&num2, &num1);
12
8
order(&num3, &num2);
10
8
对函数order的这种调用顺序将num1、num2、num3变成降序(从最大到最小)。
6.4节
1.void
onef(int dat, int *outlp, int *out2p)
{
int tmp;
twof(dat, &tmp, out2p);
...
}
void
twof(int indat, int *result1p, int *result2p)
6.5节
1.因为有很多值需要返回,这只能通过使用输出参数实现。
第7章
7.1节
1.表示误差出现在当一个double类型变量的尾数包含的位(二进制位)数不足以精确表示某个分数时;抵消误差发生在当两个在数量上相差悬殊的数进行某种运算时,其结果是较小的数丢失。
3. x y m n
10.5 7.2 5 2
a. x / (double)m 2.1
b. x / m 2.1
c. (double)(n * m) 10.0
d. (double)(n / m) + y 7.2
e. (double)(n / m) 0.0
7.2节
1.a. 3
b. 'E'
c. -1
7.3节
1.typedef enum
{monday, tuesday, wednesday, thursday, friday, saturday, sunday}
dat_t;
a. 0
b. 3
c. 0(FALSE)
d. friday
e. wednesday
f. 1(TRUE)
7.4节
1.2.0..3.0就是一个这样的区间。
第8章
8.1节
1.x3是一个有效的变量名,x[3]是对数组x第4个元素的引用。
3.double sq_root[11];
int cube[11];
1.之前
8.2节
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
16.0
12.0
6.0
8.0
2.5
12.0
14.0
-54.5
之后
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
16.0
12.0
6.0
8.0
12.0
14.0
14.0
-54.5
8.3节
1.#include <math.h>
#define MAX_SIZE 11
...
double cube[MAX_SIZE];
int sq_root[MAX_SIZE];
int i;
for (i = 0; i < MAX_SIZE; ++i) {
sq_root[i] = sqrt((double)i);
cube[i] = i * i * i;
}
8.4节
1.seg_len = sqrt(pow(x[i+1] – x[i], 2) + pow(y[i+1] – y[i], 2));
3.sum = 0;
for (i = 0; i < LIST_SIZE; i += 2)
sum += list[i];
8.5节
1.如果一个数组的几个元素都被某个函数处理,传递整个数组数据比传递单个的元素更好。
3./*
* Gets data to place in dbl_arr until value of sentinel
* is encountered in the input.
* Returns number of values stored through dbl_sizep.
* Stops input prematurely if there are more than dbl_max data
* values before the sentinel or if invalid data is encountered.
* Pre: sentinel and dbl_max are defined and dbl_max
* is the declared size of dbl_arr
* Post: returns 1 for no error, and 0 for any error conditions.
*/
int
fill_to_sentinel(int dbl_max, /* input – declared size of dbl_arr */
double sentinel, /* input – end of data value in input
list */
double dbl_arr[], /* output – array of data */
int *dbl_sizep) /* output – number of data values
stored in dbl_arr */
{
double data;
int i, status;
int result = 1;
/* Sentinel input loop */
i = 0;
status = scanf("%lf",&data);
while (status == 1 && data != sentinel && i < dbl_max)
{
dbl_arr[i] = data;
i++;
status = scanf("%lf",&data);
}
/* Issues error message on premature exit */
if (status != 1) {
printf("\n*** Error in data format ***\n");
printf("*** Using first %d data values ***\n", i);
result = 0;
} else if (data != sentinel) {
printf("\n*** Error: too much data before sentinel ***\n");
printf("*** Using first %d data values ***\n", i);
result = 0;
}
/* Sends back size of used portion of array, and error status */
*dbl_sizep = i;
return (result);
}
5. |
| $
| $ -
| $ ch is -.
8.6节
1.a. n-1作为匹配的位置返回。
b. 第一个匹配的位置被返回。
3.为实现降序排序,用index_of_max代替所出现的每个index_of_min,用largest代替所有的smallest来改写选择排序算法。
8.7节
1.a. int i, j, k;
for (i = 0; i < MAXCRS; ++i) {
printf("Processing course number %d: \n", i);
for (j = 0; j < 5; ++j) {
printf(" Campus %d\n", j);
for (k = 0; k < 4; ++k)
printf(" Enter number of ");
switch (k) {
case 0 :
printf("Freshmen > ");
break;
case 1 :
printf("Sophomores > ");
break;
case 2 :
printf("Juniors > ");
break;
case 3 :
printf("Seniors > ");
break;
}
scanf("%d", &enroll[i][j][k]);
}
}
}
b. int i, j,
jcnt; /* Number of juniors. */
jcnt = 0;
for (i = 0; i < MAXCRS; ++i)
for (j = 0; j < 5; ++j)
jcnt += enroll[i][j][2];
c. /*
* Compute the number of students in a course who have a
* specific rank.
* returns -1 if rank or course is out of range.
*/
int
find_students (int enroll[][5][4], int rank, int course)
{
int i, cnt = 0;
if ((rank >= 0 && rank <= 3)
&& (course >= 0 && course < MAXCRS))
for (i = 0; i < 5; ++i)
cnt += enroll[course][i][rank];
else
cnt = -1;
return (cnt);
}
用
printf("Number of sophomores in course 2 is %d\n",
find_students(enroll, 1, 2);
d. int i, j, total, upper;
total = 0;
upper = 0;
for (j = 0; j < 5; ++j) {
for (i = 0; i < MAXCRS; ++i)
upper += enroll[i][j][2] + enroll[i][j][3];
printf("Number of upperclass students on campus ");
printf("%d is %d.\n", j, upper);
total += upper;
}
printf("Total upperclass students on all campuses is %d.\n",total);
8.8节
1. a. sales[1][fall], sales[1][winter], sales[1][spring],
sales[1][summer]
b. sales[0][spring], sales[1][spring], sales[2][spring],
sales[3][spring], sales[4][spring]
c. sales[0][fall], sales[1][fall], sales[2][fall],
sales[3][fall], sales[4][fall]
sales[0][winter], sales[1][winter], sales[2][winter],
sales[3][winter], sales[4][winter]
sales[0][spring], sales[1][spring], sales[2][spring],
sales[3][spring], sales[4][spring]
sales[0][summer], sales[1][summer], sales[2][summer],
sales[3][summer], sales[4][summer]
第9章
9.1节
1.b
3. /* 0123456789012345678901234567890 */
char blanks[] = " ";
或
char blanks[30] = " ";
9.2节
1.Ad John Quincy Join
9.3节
1.John Adams
9.4节
1.a. if (strcmp(name1,name2) == 0)
printf("Names match.\n");
else
printf("Names do not match.\n");
b. if (strcmp(w1,w2) < 0)
strcpy(word, w1);
else
strcpy(word, w2);
展开阅读全文