復習題
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