BeautifulMan

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  16 随笔 :: 0 文章 :: 0 评论 :: 0 Trackbacks
复习题
1、给出每行之后quack的值
    int quack = 2;
    quack += 5;
    quack *= 10;
    quack -= 6;
    quack /= 8;
    quack %= 3;
答:
    quack = 2;
    quack = 7;
    quack = 70;
    quack = 64;
    quack = 8;
    quack = 2;
2、假定value是一个int类型的值,以下的循环会产生什么输出?
for(value = 36; value > 0; value /= 2)
      printf("%3d", value);
如果value是一个double类型的值而不是int类型的值,会有什么问题?
答:
 36 18 9 4 2 1
如果value是double类型,那么value变得小于1时判断条件仍会保持为真。循环会一直执行,直到由于浮点数下溢而产生0值。其次,此时%3d说明符也是不正确的。
3、表示出以下判断条件:
a.x大于5
b.scanf()尝试读入一个double值(名为x)并且失败
c.x的值为5
答:
a.x > 5;
b.scanf("%lf", &x) != 1;
c.x = 5;
4、表示出以下判断条件:
a.scanf()成功地读入了一个整数
b.x不等于5
c.x大于或等于20
答:
a.scanf("%d", &x) == 1;
b.x != 5;
c.x >= 20;
5、您怀疑以下的程序可能有问题。您能找出什么错误?
 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     int i, j, list(10);
 5 
 6     for(i = 1, i <= 10, i++)
 7     {
 8         list[i] = 2*i + 3;
 9         for(j = 1, j >= i, j++)
10             printf(" %d", list[j]);
11         printf("\n");
12 }
答:
第4行:应该是list[10]。
第6行:逗号应该为分号。
第6行:i的范围应该是从0到9,而不是从1到10。
第9行:逗号应该为分号。
第9行:>=应该是<=。否则,当i为1时,循环永远不会结束。
第11行:在第11行和第12行之间应该还有一个花括号。一个花括号结束复合语句,一个结束程序。在这两个花括号之间应该有这样一行代码:return 0;。
下面是一个正确的版本:
 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     int i, j, list[10];
 5 
 6     for(i = 0; i < 10; i++)
 7     {
 8         list[i] = 2*i + 3;
 9         for(j = 1, j <= i, j++)
10             printf(" %d", list[j]);
11         printf("\n");
12     }
13     return 0;
14 }
6、使用嵌套循环编写产生下列图案的程序:
$$$$$$$$
$$$$$$$$
$$$$$$$$
$$$$$$$$
答:
#include <stdio.h>
int main(void)
{
    int i, j;
    for(i = 0; i < 4; i++)
    {
        for(j = 0; j < 8; j++)
            printf("$");
        printf("\n");
    }
    return 0;
}
7、以下程序会打印出什么?
a.
    #include <stdio.h>
    int main(void)
    {
        int i = 0;

        while(++i < 4)
            printf("Hi! ");
        do
            printf("Bye! ");
        while(i++ < 8);
        return 0;
    }
b.
    #include <stdio.h>
    int main(void)
    {
        int i = 0;
        char ch;

        for(i = 0, ch = 'A'; i < 4; i++, ch += 2 * i)
            printf("%c", ch);
        return 0;
    }
答:
a.
Hi! Hi! Hi! Bye! Bye! Bye! Bye! Bye! 
b.
ACGM
8、假定输入为“Go west,young man!”,以下的程序会产生什么样的输出?(在ASCII序列中,!紧跟在空格字符后面)
a.
    #include <stdio.h>
    int main(void)
    {
        char ch;

        scanf("%c", &ch);
        while(ch != 'g')
        {
            printf("%c", ch);
            scanf("%c", &ch);
        }
        return 0;
    }
b.
    #include <stdio.h>
    int main(void)
    {
        char ch;

        scanf("%c", &ch);
        while(ch != 'g')
        {
            printf("%c", ++ch);
            scanf("%c", &ch);
        }
        return 0;
    }
c.
    #include <stdio.h>
    int main(void)
    {
        char ch;

        do
        {
            scanf("%c", &ch);
            printf("%c", ch);
        } while(ch != 'g');
        return 0;
    }
d.
    #include <stdio.h>
    int main(void)
    {
        char ch;

        scanf("%c", &ch);
        for(ch = '$'; ch != 'g'; scanf("%c", &ch))
            putchar(ch);
        return 0;
    }

a.
Go west,youn
b.
Hp!xftu-!zpvo
c.
Go west,young
d.
$o west,youn
9、以下程序会打印出什么?
#include <stdio.h>
int main(void)
{
    int n, m;

    n = 30;
    while(++n <= 33)
        printf("%d|", n);

    n = 30;
    do
        printf("%d|", n);
    while(++n <= 33);

    printf("\n***\n");

    for(n = 1; n*n < 200; n += 4)
        printf("%d\n", n);

    printf("\n***\n");

    for(n = 2, m = 6; n < m; n *= 2, m += 2)
        printf("%d %d\n", n, m);

    printf("\n***\n");

    for(n = 5; n > 0; n--)
    {
        for(m = 0; m <= n; m++)
            printf("=");
        printf("\n");
    }
    return 0;
}
答:
31|32|33|30|31|32|33|
***
1
5
9
13

***
2 6
4 8
8 10

***
======
=====
====
===
==
10、考虑以下声明:
double mint[10];
a.数组名是什么?
b.在数组中有多少元素?
c.在每个元素中存储着什么类型的值?
d.下面哪个对该数组正确地使用了scanf()?
    i.scanf("%lf", mint[2]);
    ii.scanf("%lf", &mint[2]);
    iii.scanf("%lf", &mint);
答:
a.mint
b.10
c.double类型
d.ii
11、Noah先生喜欢以2计数,所以他写了以下的程序来创建一个数组,并用整数2、4、6、8等等来填充它。如果有错误的话,这个程序的错误是什么?
#include <stdio.h>
#define SIZE 8
int main(void)
{
    int by_twos[SIZE];
    int index;

    for(index = 1; index <= SIZE; index++)
        by_twos[index] = 2 * index;
    for(index = 1; index <= SIZE; index++)
        printf("%d ", by_twos);
    printf("\n");
    return 0;
}
答:
(参考课后答案)
因为第一个元素的索引为0,所以循环的范围应该从0到SIZE-1,而不是从1到SIZE。但是这样改变会使第一个元素被赋值为0而不是2。所以要这样重写这个循环:
  for(index = 0; index < SIZE; index++)
        by_twos[index] = 2 * (index + 1);
类似地,也应该改变第二个循环的限制条件。其次,应该在数组名后使用数组索引:
   for(index = 0; index < SIZE; index++)
        printf("%d ", by_twos[index]);
错误的循环限制条件的一个危险的方面在于程序可以运行,但是因为它把数据放在不正确的地方,所以可能在未来的某个时刻不能运行,这样就形成了一种程序中的定时炸弹。
12、您想要写一个返回long值的函数。在您的函数定义中应该包含什么?
答:
函数应该把返回类型声明为long,并包含一个返回long值的return语句。
13、定义一个函数,该函数接受一个int参数,并以long类型返回参数的平方值。
答:把num的类型指派为long,这样可以确保运算是long运算而不是int运算。在int为16位的系统上,两个int值相乘的结果在返回之前会被截尾为一个int值,这样就可能丢失数据。
long square(int num)
{
    return ((long)num) * num;
}
14、以下程序会打印出什么?
#include <stdio.h>
int main(void)
{
    int k;

    for(k = 1, printf("%d: Hi!\n", k); printf("k = %d\n", k), k * k < 26; k += 2, printf("Now k is %d\n", k))
        printf("k is %d in the loop\n", k);
    return 0;
}
答:
1: Hi!
k = 1
k is 1 in the loop
Now k is 3
k = 3
k is 3 in the loop
Now k is 5
k = 5
k is 5 in the loop
Now k is 7
k = 7
编程练习
1、
#include <stdio.h>
int main(void)
{
    char array[26];
    int index;

    for(index = 0; index < 26; index++)
    {
        array[index] = 'a' + index;
    }
    for(index = 0; index < 26; index++)
    {
        printf("%2c", array[index]);
    }
    return 0;
}
2、
#include <stdio.h>
int main(void)
{
    int i, j;

    for(i = 0; i < 5; i++)
    {
        for(j = 0; j <= i; j++)
            printf("$");
        printf("\n");
    }
    return 0;
}
3、
#include <stdio.h>
int main(void)
{
    int row;
    char ch;

    for(row = 0; row < 6; row++)
    {
        for(ch = 'F'; ch >= 'F' - row; ch--)
            printf("%c", ch);
        printf("\n");
    }
    return 0;
}
4、
#include <stdio.h>
int main(void)
{
    char ch, row, space, ch1, ch2;
    printf("Please enter a up letter: \n");
    scanf("%c", &ch);

    for(row = 'A';row <= ch; row++)
    {
        for(space = row; space < ch; space++)
            printf(" ");
        for(ch1 = 'A';ch1 <= row; ch1++)
            printf("%c", ch1);
        for(ch2 = row - 1; ch2 >= 'A'; ch2--)
            printf("%c", ch2);
        printf("\n");
    }
    return 0;
}
5、
#include <stdio.h>
int main(void)
{
    int up, down, index;
    printf("Please enter a up number and down number: \n");
    scanf("%d %d", &down, &up);

    for(index = down; index <= up; index++)
        printf("%4d %4d %4d\n", index, index*index, index*index*index);
    return 0;
}
6、
#include <stdio.h>
#include <string.h>
int main(void)
{
    int index;
    char character[20];
    printf("Please enter a word: \n");
    scanf("%s", character);
    for(index = strlen(character) - 1; index >= 0; index--)
        printf("%c", character[index]);
    return 0;
}
7、
#include <stdio.h>
int main(void)
{
    double dou1, dou2;

    printf("Please two double numbers: \n");
    while(scanf("%lf %lf", &dou1, &dou2) == 2)
    {
        printf("(dou1 - dou2) / (dou1 * dou2) = %.2f\n", (dou1 - dou2) / (dou1 * dou2));
        printf("Please two double numbers: \n");
    }
    return 0;
}
8、
#include <stdio.h>
double computer(double n1, double n2);
int main(void)
{
    double dou1, dou2;

    printf("Please two double numbers: \n");
    while(scanf("%lf %lf", &dou1, &dou2) == 2)
    {
        printf("(dou1 - dou2) / (dou1 * dou2) = %.2f\n", computer(dou1, dou2));
        printf("Please two double numbers: \n");
    }
    return 0;
}
double computer(double n1, double n2)
{
    return (n1 - n2) / (n1 * n2);
}
9、
#include <stdio.h>
int main(void)
{
    int up, down, index;
    int sum;

    printf("Enter lower and upper integer limits: ");
    scanf("%d %d", &down, &up);

    do
    {
        sum = 0;
        for(index = down; index <= up; index++)
        {
            sum += index * index;
        }
        printf("The sums of the squares from %d to %d is %d\n", down, up, sum);
        printf("Enter next set of limits: ");
        scanf("%d %d", &down, &up);
    } while(up > down);
    printf("Done\n");
    return 0;
}
10、
#include <stdio.h>
int main(void)
{
    int value, array[8];
    int index;
    printf("please enter 8 integer numbers: \n");
    for(index = 0; index < 8; index++)
    {
        scanf("%d", &value);
        array[index] = value;
    }
    for(index = 7; index >= 0; index--)
        printf("%2d", array[index]);
    return 0;
}
11、(不知这样写对不对,啊!参考书上的例子——程序清单6.14)
#include <stdio.h>
int main(void)
{
    int count;
    double time, x;
    int limit;

    printf("Enter the number of terms you want: ");
    scanf("%d", &limit);
    for(time = 0, x = 1, count = 1; count <= limit; count++, x += 1.0)
    {
        time += 1.0/x;
        printf("time = %f when terms = %d.\n", time, count);
    }
    printf("------------------------------------------------------\n");
    for(time = 0, x = 1, count = 1; count <= limit; count++, x += 1.0)
    {
        if(count % 2 == 1)
        {
            time += 1.0/x;
        } else
        {
            time += -1.0/x;
        }
        printf("time = %f when terms = %d.\n", time, count);
    }
    return 0;
}
我根据运行结果来判断,第一个无限序列是发散的,第二个无限序列收敛于0.69(保留两位小数)
12、
#include <stdio.h>
int main(void)
{
    int index, array[8];
    array[0] = 2; // 我认为数组里面第一个元素应该是2的1次幂,然后依此类推知道最后一个元素为2的8次幂
    for(index = 1; index < 8; index++)
    {
        array[index] = array[index-1] * 2;
    }
    index = 0;
    do
    {
        printf("%5d", array[index++]);
    } while(index < 8);
    return 0;
}
13、
#include <stdio.h>
int main(void)
{
    double dou1[8], dou2[8], value, sum;
    int index;

    for(index = 0; index < 8; index++)
    {
        if(scanf("%lf", &value) == 1)
            dou1[index] = value;
        else
            break;
    }
    for(index = 0; index < 8; index++)
    {
        sum += dou1[index];
        dou2[index] = sum;
        /*for(i = index; i = index; i++) // 此循环能否再精简一点
            dou2[i] = sum;
*/
    }
    // 怎么才能使用一个循环来显示两个数组中的内容,第一行数组在一行中显示,而第二个数组中的每个元素在第一个数组的
    
// 对应元素之下进行显示呢???
    
// 我还是要用两个循环显示,哎!!!
    for(index = 0; index < 8; index++)
    {
        printf("%10.2f", dou1[index]);
    }
    printf("\n");
    for(index = 0; index < 8; index++)
    {
        printf("%10.2f", dou2[index]);
    }
    return 0;
}
14、
#include <stdio.h>
int main(void)
{
    char character[255], ch;
    int i, index = 0;
    do
    {
        scanf("%c", &ch);
        character[index++] = ch; // 换行符也存进去了
    } while(ch != '\n');
    for(i = index; i >= 0; i--)
    {
        printf("%c", character[i]);
    }
    return 0;
}
15、
#include <stdio.h>
#define SINGLE_INTEREST 0.10
#define COMPOUND_INTEREST 0.05
#define ORIGINAL_INVESTMENT 100
double power(int p);
double daphne_invest(int p);
double deirdre_invest(int p);
int main(void)
{
    int p;

    for(p = 1;;p++)
    {
        if(deirdre_invest(p) > daphne_invest(p))
            break;
    }
    printf("The required number of years: %d\n", p);
    printf("The investment amount of Daphne: %.2f\n", daphne_invest(p));
    printf("The investment amount of Deirdre: %.2f\n", deirdre_invest(p));
    return 0;
}
// 一个数的整数次幂
double power(int p)
{
    double pow = 1.0;
    int i;
    for(i = 1; i <= p; i++)
    {
       pow *= (1 + COMPOUND_INTEREST);
    }
    return pow;
}
//计算p年后daphne的投资额
double daphne_invest(int p)
{
    return ORIGINAL_INVESTMENT + ORIGINAL_INVESTMENT*SINGLE_INTEREST*p;
}
//计算p年后deirdre的投资额
double deirdre_invest(int p)
{
    return ORIGINAL_INVESTMENT*power(p);
}
16、
#include <stdio.h>
#define COMPOUND_INTEREST 0.08
#define ORIGINAL_INVESTMENT 100
double power(int p);
double spare_money(int p);
int main(void)
{
    int p;

    for(p = 1;;p++)
    {
        if(spare_money(p) <= 0)
            break;
    }
    printf("The required number of years: %d\n", p);
    return 0;
}
// 一个数的整数次幂
double power(int p)
{
    double pow = 1.0;
    int i;
    for(i = 1; i <= p; i++)
    {
       pow *= (1 + COMPOUND_INTEREST);
    }
    return pow;
}
//计算p年后Chuckie Lucky取出10万美元之后还剩余的钱
double spare_money(int p)
{
    int i;
    double sum = 1.0;
    for(i = 1; i <= p - 1; i++)
    {
        sum += power(i);
    }
    return ORIGINAL_INVESTMENT*power(p) - 10*sum;
}
运行结果为:
The required number of years:  21


posted on 2015-11-19 14:04 李阿昀 阅读(586) 评论(0)  编辑  收藏 所属分类: C Primer Plus 复习题与编程练习

只有注册用户登录后才能发表评论。


网站导航: