1. 程式人生 > >C Primer Plus 第六章 課後答案

C Primer Plus 第六章 課後答案

目錄

複習題

a.大於5

程式設計練習

$

$$

$$$

$$$$

$$$$$

F

FE

FED

FEDC

FEDCB

A

BC

DEF

GHIJ

KLMNO

A

ABA

ABCBA

Done

複習題

1.寫出執行完下列各行後quack的值是多少。後5行中使用的是第1行quack的值。

int quack = 2;

quack += 5;

quack *= 10;

quack -= 6;

quack /= 8;

quack %= 3;

7            70            64            8            2

2.假設value是int型別,下面迴圈的輸出是什麼?

for ( value = 36; value > 0; value /= 2)

printf("%3d", value);

如果value是double型別,會出現什麼問題 

 36 18  9  4  2  1

如果value是double型的,迴圈會持續到value下溢為0,但輸出的格式會導致輸出錯誤

3.用程式碼表示以下測試條件:

a.大於5

b.scanf()讀取一個名為double的型別值且失敗

c.X的值等於5

a. > 5

b. scanf("%d", &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.下面的程式有點問題,請找出問題所在

#include <stdio.h> 
int main(void) 
{                  /* 第3行 */ 
    int i, j, list(10);       /* 第4行 */ 
    for (i = 1, i <= 10, i++)    /* 第6行 */ 
    {                /* 第7行 */ 
        list[i] = 2*i + 3;      /* 第8行 */ 
    for (j = 1, j > = i, j++)  /* 第9行 */ 
        printf(" %d", list[j]); /* 第10行 */ 
    printf("\n");        /* 第11行 */ 
} 
#include <stdio.h> 
int main(void) 
{                  /* 第3行 */ 
    int i, j, list[10];       /* 第4行 */ 
    for (i = 1; i <= 10; i++)    /* 第6行 */ 
    {                /* 第7行 */ 
        list[i] = 2*i + 3;      /* 第8行 */ 
        for (j = 1, j <= i, j++)  /* 第9行 */ 
            printf(" %d", list[j]); /* 第10行 */ 
        printf("\n");              /* 第11行 */  
    }  
    return 0;    
} 

6.編寫一個程式列印下面的圖案,要求使用巢狀迴圈:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    for(int i = 0; i < 4; i++)
    {
        for(int j =0; j < 8; j++)
        {
            printf("$");
        }
        printf("\n");
    }
}

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; 
    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))
        printf("%c", 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; 
}
#include <stdio.h>
#define SIZE 8
int main(void)
{
    int by_twos[SIZE];
    int index;
    for (index = 0; index < SIZE; index++)
        by_twos[index] = 2 * (index + 1);
    for (index = 0; index < SIZE; index++)
        printf("%d ", by_twos[index]);
    printf("\n");
    return 0;
}

12.假設要編寫一個返回long型別值的函式,函式定義中應包含什麼

返回long型別值的return語句

13.定義一個函式,接受一個int型別的引數,並以long型別返回引數的平方值

long func(int n)
{
    return (long)n;
}

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.編寫一個程式,建立一個包含26個元素的陣列,並在其中儲存26個小寫字母。然後列印陣列的所有內容

#include <stdio.h>
#include <stdlib.h>


int main(void)
{
    char l[26];
    for(int i = 0; i < 26; i++)
    {
        l[i] = 'a' + i;
    }
    for(int i = 0; i < 26; i++)
    {
        printf("%c ", l[i]);
    }
    printf("\n");
    return 0;
}

2.使用巢狀迴圈,按下面的格式列印字元:

$

$$

$$$

$$$$

$$$$$

#include <stdio.h>
#include <stdlib.h>


int main(void)
{
    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < i + 1; j++)
        {
            printf("$");
        }
        printf("\n");
    }
    return 0;
}

3.使用巢狀迴圈,按下面的格式列印字母:

F

FE

FED

FEDC

FEDCB

FEDCBA

#include <stdio.h>
#include <stdlib.h>


int main(void)
{
    for(int i = 0; i < 6; i++)
    {
        char c = 'F';
        for(int j = 0; j < i + 1; j++)
        {
            printf("%c", c - j);
        }
        printf("\n");
    }
}

4.使用巢狀迴圈,按下面的格式列印字母:

A

BC

DEF

GHIJ

KLMNO

PQRSTU

#include <stdio.h>
#include <stdlib.h>


int main(void)
{

    char c = 'A';
    for(int i = 1; i < 7; i++)
    {
        for(int j = 0; j < i; j++, c++)
        {
            printf("%c", c);
        }
        printf("\n");
    }
}

5.編寫一個程式,提示使用者輸入大寫字母。使用巢狀迴圈以下面金字塔型的格式列印字母:

A

ABA

ABCBA

ABCDCBA

ABCDEDCBA

#include <stdio.h>
#include <stdlib.h>


int main(void)
{
    char c = 'A';
    for(int i = 1; i < 6; i++)
    {
        for(int j = 1 - i; j <= i - 1; j++)
        {
            printf("%c", c - abs(j));
        }
        printf("\n");
        c++;
    }
}

6.編寫一個程式列印一個表格,每一行列印一個整數、該數的平方、該數的立方。要求使用者輸入表格的上下限。使用一個for迴圈

#include <stdio.h>
#include <stdlib.h>


int main(void)
{
    int l, u;
    printf("input l and u:");
    scanf("%d %d", &l, &u);
    for(int i = l; i <= u; i++)
    {
        printf("%10d%10d%10d\n", i, i * i, i * i * i);
    }
}

7.編寫一個程式把一個單詞讀入一個字元陣列中,然後倒序列印這個單詞。

提示:strlen()函式(第4章介紹過)可用於計算陣列最後一個字元的下標

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char l[80];
    scanf("%s", &l);
    int n = strlen(l);
    for(int i = n - 1; i > -1; i--)
    {
        printf("%c", l[i]);
    }
    printf("\n");
}

8.編寫一個程式,要求使用者輸入兩個浮點數,並列印兩數之差除以兩數乘積的結果。在使用者輸入非數字之前,程式應迴圈處理使用者輸入的每對值

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    double a, b;
    printf("input a and b:");
    while(scanf("%lf %lf", &a, &b) == 2)
    {
        printf("%lf\n", (a - b) / (a * b));
        printf("input again(input two q to exit):");
    }
    return 0;
}

9.修改練習8,使用一個函式返回計算的結果

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

double func(double a, double b)
{
    return (a - b) / (a * b);
}

int main()
{
    double a, b;
    printf("input a and b:");
    while(scanf("%lf %lf", &a, &b) == 2)
    {
        printf("%lf\n", func(a, b));
        printf("input again(input two q to exit):");
    }
    return 0;
}

10.編寫一個程式,要求使用者輸入一個上限整數和一個下限整數,計算從上限到下限範圍內所有整數的平方和,並顯示計算結果。然後程式繼續提示使用者輸入上限和下限整數,並顯示結果,直到使用者輸入的上限整數小於下限整數為止。程式的執行示例如下:

Enter lower and upper integer limits: 5 9

The sums of the squares from 25 to 81 is 255

Enter next set of limits: 3 25

The sums of the squares from 9 to 625 is 5520

Enter next set of limits: 5 5

Done

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b;
    printf("Enter lower and upper integer limits:");
    while(scanf("%d %d", &a, &b) == 2 && a < b)
    {
        int sum = 0;
        for(int i = a; i <= b; i++)
        {
            sum += i * i;
        }
        printf("The sums of the squares from %d to %d is %d\n", a * a, b * b, sum);
        printf("Enter next set of limits: ");
    }
    printf("Done\n");
    return 0;
}

11.編寫一個程式,在陣列中讀入8個整數,然後按倒序列印這8個整數

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a[8];
    for(int i = 0; i < 8; i++)
    {
        scanf("%d", &a[i]);
    }
    for(int i = 7; i > -1; i--)
    {
        printf("%d ", a[i]);
    }
    printf("\n");
    return 0;
}

12.考慮下面兩個無限序列:

1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + ...

1.0 -  1.0/2.0 + 1.0/3.0 -  1.0/4.0 + ...

編寫一個程式計算這兩個無限序列的總和,直到到達某次數。提示:奇數個-1 相乘得-1,偶數個-1相乘得1。讓使用者互動地輸入指定的次數,當用戶輸入0或負值時結束輸入。檢視執行100項、1000項、10000項後的總和,是否發現每個序列都收斂於某值

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long long t;
    printf("input t:");
    while(scanf("%lld", &t) && t > 0)
    {
        double sum1 = 0, sum2 = 0;
        for(int i = 1; i <= t; i++)
        {
            sum1 += 1.0 / i;
            if(i & 1)
            {
                sum2 += 1.0 / i;
            }
            else
            {
                sum2 -= 1.0 / i;
            }
        }
        printf("%lf %lf\n", sum1, sum2);
        printf("input again:");
    }
}

13.編寫一個程式,建立一個包含8個元素的int型別陣列,分別把陣列元素設定為2的前8次冪。使用for迴圈設定陣列元素的值,使用do while迴圈顯示陣列元素的值

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a[8];
    for(int i = 0, j = 2; i < 8; i++, j *= 2)
    {
        a[i] = j;
    }
    int n = 0;
    do
    {
        printf("%d ", a[n]);
    }while(++n < 8);
    printf("\n");
    return 0;
}

14.編寫一個程式,建立兩個包含8個元素的double型別陣列,使用迴圈提示使用者為第一個陣列輸入8 個值。第二個陣列元素的值設定為第一個陣列對應元素的累積之和。例如,第二個陣列的第 4個元素的值是第一個陣列前4個元素之和,第二個陣列的第5個元素的值是第一個陣列前5個元素之和(用巢狀迴圈可以完成,但是利用第二個陣列的第5個元素是第二個陣列的第4個元素與第一個陣列的第5個元素之和,只用一個迴圈就能完成任務,不需要使用巢狀迴圈)。最後,使用迴圈顯示兩個陣列的內容,第一個陣列顯示成一行,第二個陣列顯示在第一個陣列的下一行,而且每個元素都與第一個陣列各元素相對應

#include <stdio.h>
#include <stdlib.h>

int main()
{
    double a[8], b[8];
    for(int i = 0; i < 8; i++)
    {
        scanf("%lf", &a[i]);
        if(i > 0)
        {
            b[i] = b[i - 1] + a[i];
        }
        else
        {
            b[i] = a[i];
        }
    }
    for(int i = 0; i < 8; i++)
    {
        printf("%.2lf ", b[i]);
    }
    printf("\n");
    return 0;
}

15.編寫一個程式,讀取一行輸入,然後把輸入的內容倒序打印出來。可以把輸入儲存在char型別的陣列中,假設每行字元不超過255。回憶一下,根據%c轉換說明,scanf()函式一次只能從輸入中讀取一個字元,而且在使用者按下Enter鍵時scanf()函式會生成一個換行字元(\n)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char a[256];
    scanf("%s", a);
    int n = strlen(a);
    for(int i = n - 1; i > -1; i--)
    {
        printf("%c", a[i]);
    }
    printf("\n");
    return 0;
}

16.Daphne以10%的單利息投資了100美元(也就是說,每年投資獲利相當於原始投資的10%)。Deirdre以 5%的複合利息投資了 100 美元(也就是說,利息是當前餘額的 5%,包含之前的利息)。編寫一個程式,計算需要多少年Deirdre的投資額才會超過Daphne,並顯示那時兩人的投資額

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int n = 0;
    double a = 100.0, b = 100.0;
    while(b <= a)
    {
        a += 10.0;
        b *= 1.05;
        n++;
    }
    printf("%d %lf %lf\n", n, a, b);
    return 0;
}

17.Chuckie Lucky贏得了100萬美元(稅後),他把獎金存入年利率8%的賬戶。在每年的最後一天, Chuckie取出10萬美元。編寫一個程式,計算多少年後Chuckie會取完賬戶的錢

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    double a = 1e6;
    int n = 0;
    while(a > 0)
    {
        n++;
        a *= 1.08;
        a -= 1e5;
    }
    printf("%d\n", n);
    return 0;
}

18.Rabnud博士加入了一個社交圈。起初他有5個朋友。他注意到他的朋友數量以下面的方式增長。第1周少了1個朋友,剩下的朋友數量翻倍;第2周少了2個朋友,剩下的朋友數量翻倍。一般而言,第N周少了N個朋友,剩下的朋友數量翻倍。編寫一個程式,計算並顯示Rabnud博士每週的朋友數量。該程式一直執行,直到超過鄧巴數(Dunbar’s number)。鄧巴數是粗略估算一個人在社交圈中有穩定關係的成員的最大值,該值大約是150

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int n = 5;
    for(int i = 1; n <= 150; i++)
    {
        printf("%d\n", n);
        n -= i;
        n *= 2;
    }
    return 0;
}