作为C语言初学者,以下内容为自己学习时所记,内容如有错误之处欢迎指出,非常感谢。 在此本人想致谢50no ,在此书的前面章节学习过程中曾多次参考50no 写的课后编程题答案 。
12.1
(a) 14 (b) 34 © 4 (d)真 (e) 假
12.2
此处粘贴原版答案
The statement is illegal because pointers cannot be added. Here’s a legal statement that has the desired effect:
middle = low + (high - low) / 2;
The value of (high - low) / 2 is an integer, not a pointer, so it can legally be added to low.
12.3
a[N] = {10, 9 , 8, 7, 6, 5, 4, 3, 2, 1}
12.4
此处粘贴原版答案
int *top_ptr;
void make_empty(void)
{
top_ptr = &contents[0];
}
bool is_empty(void)
{
return top_ptr == &contents[0];
}
bool is_full(void)
{
return top_ptr == &contents[STACK_SIZE];
}
12.5
b c d为真
假设int a[n] = {1, 2, 3}
其在内存中的储存假设为
a[n] | 地址 |
---|---|
a[0] | 2000 |
a[1] | 2004 |
a[2] | 2008 |
其中(a)中P为指针(其值为a[0]的地址——2000)类型为 int*,而a[0]的值为1 类型为int, 二者类型不同,值也不同。
12.6
int sum_array(const int *p, int n)
{
int sum;
sum = 0;
for (; p < p + n; p++)
sum += *p;
return sum;
}
12.7
bool search(const int a[], int n, int key)
{
bool i = false;
for (int j = 0 ; j < n; j++)
{
if (key == *(a + j))
i = true;
}
return i;
}
12.14
# include <cstdio>
# include <iostream>
# define ROW 7
# define COL 24
bool search(const int a[ROW][COL], int );
int main(void)
{
int key;
bool k;
int a[ROW][COL] = { 1, 2, 3, 4, 5 };
printf("Enter a number for search:");
scanf_s(" %d", &key);
k = search(a,key);
if (k)
{
printf("123");
}
else
printf("456");
system("pause");
return 0;
}
bool search(const int a[ROW][COL], int key)
{
for (int i = 0 ; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (key == *(a[i] + j))
return true;
}
}
return false;
}
更多推荐
C语言程序设计现代方法(第二版)十二章课后练习题部分答案
发布评论