当前位置:嗨网首页>书籍在线阅读

10-第9章复习题答案

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

A.9 第9章复习题答案

1.形式参数是定义在被调函数中的变量。实际参数是出现在函数调用中的值,该值被赋给形式参数。可以把实际参数视为在函数调用时初始化形式参数的值。

2.a. void donut(int n)

b. int gear(int t1, int t2)

c. int guess(void)

d. void stuff_it(double d, double * pd)

3.a. char n_to_char(int n)

b. int digits(double x, int n)

c. double which(double p1, double * p2)

d. int random(void)

4.

int sum(int a, int b)
{
   return a + b;
}

5.用 double 替换 int 即可:

double sum(double a, double b)
{
   return a + b;
}

6.该函数要使用指针:

void alter(int * pa, int * pb)
{
   int temp;
   temp = *pa + *pb;
   *pb = *pa - *pb;
   *pa = temp;
}

或者:

void alter(int * pa, int * pb)
{
   *pa += *pb;
   *pb = *pa - 2 * *pb;
}

7.不正确。 num 应声明在 salami() 函数的参数列表中,而不是声明在函数体中。另外,把 count++ 改成 num++

8.下面是一种方案:

int largest(int a, int b, int c)
{
   int max = a;
   if (b > max)
     max = b;
   if (c > max)
     max = c;
   return max;
}

9.下面是一个最小的程序, showmenu()getchoice() 函数分别是 ab 的答案。

#include <stdio.h>
/* 声明程序中要用到的函数 */
void showmenu(void); 
int getchoice(int, int);
int main()
{
   int res;
   showmenu();
   while ((res = getchoice(1, 4)) != 4)
   {
     printf("I like choice %d.\n", res);
     showmenu();
   }
   printf("Bye!\n");
   return 0;
}
void showmenu(void)
{
   printf("Please choose one of the following:\n");
   printf("1) copy files      2) move files\n");
   printf("3) remove files     4) quit\n");
   printf("Enter the number of your choice:\n");
}
int getchoice(int low, int high)
{
   int ans;
   int good;
   good = scanf("%d", &ans);
   while (good == 1 && (ans < low || ans > high))
   {
     printf("%d is not a valid choice; try again\n", ans);
     showmenu();
     scanf("%d", &ans);
   }
   if (good != 1)
   {
     printf("Non-numeric input. ");
     ans = 4;
   }
   return ans;
}