復(fù)習(xí)題
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是一個(gè)int類型的值,以下的循環(huán)會(huì)產(chǎn)生什么輸出?
for(value = 36; value > 0; value /= 2)
printf("%3d", value);
如果value是一個(gè)double類型的值而不是int類型的值,會(huì)有什么問(wèn)題?
答:
36 18 9 4 2 1
如果value是double類型,那么value變得小于1時(shí)判斷條件仍會(huì)保持為真。循環(huán)會(huì)一直執(zhí)行,直到由于浮點(diǎn)數(shù)下溢而產(chǎn)生0值。其次,此時(shí)%3d說(shuō)明符也是不正確的。
3、表示出以下判斷條件:
a.x大于5
b.scanf()嘗試讀入一個(gè)double值(名為x)并且失敗
c.x的值為5
答:
a.x > 5;
b.scanf("%lf", &x) != 1;
c.x = 5;
4、表示出以下判斷條件:
a.scanf()成功地讀入了一個(gè)整數(shù)
b.x不等于5
c.x大于或等于20
答:
a.scanf("%d", &x) == 1;
b.x != 5;
c.x >= 20;
5、您懷疑以下的程序可能有問(wèn)題。您能找出什么錯(cuò)誤?
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行:應(yīng)該是list[10]。
第6行:逗號(hào)應(yīng)該為分號(hào)。
第6行:i的范圍應(yīng)該是從0到9,而不是從1到10。
第9行:逗號(hào)應(yīng)該為分號(hào)。
第9行:>=應(yīng)該是<=。否則,當(dāng)i為1時(shí),循環(huán)永遠(yuǎn)不會(huì)結(jié)束。
第11行:在第11行和第12行之間應(yīng)該還有一個(gè)花括號(hào)。一個(gè)花括號(hào)結(jié)束復(fù)合語(yǔ)句,一個(gè)結(jié)束程序。在這兩個(gè)花括號(hào)之間應(yīng)該有這樣一行代碼:return 0;。
下面是一個(gè)正確的版本:
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、使用嵌套循環(huán)編寫產(chǎn)生下列圖案的程序:
$$$$$$$$
$$$$$$$$
$$$$$$$$
$$$$$$$$
答:
#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、以下程序會(huì)打印出什么?
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!”,以下的程序會(huì)產(chǎn)生什么樣的輸出?(在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、以下程序會(huì)打印出什么?
#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.數(shù)組名是什么?
b.在數(shù)組中有多少元素?
c.在每個(gè)元素中存儲(chǔ)著什么類型的值?
d.下面哪個(gè)對(duì)該數(shù)組正確地使用了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計(jì)數(shù),所以他寫了以下的程序來(lái)創(chuàng)建一個(gè)數(shù)組,并用整數(shù)2、4、6、8等等來(lái)填充它。如果有錯(cuò)誤的話,這個(gè)程序的錯(cuò)誤是什么?
#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;
}
答:
(參考課后答案)
因?yàn)榈谝粋€(gè)元素的索引為0,所以循環(huán)的范圍應(yīng)該從0到SIZE-1,而不是從1到SIZE。但是這樣改變會(huì)使第一個(gè)元素被賦值為0而不是2。所以要這樣重寫這個(gè)循環(huán):
for(index = 0; index < SIZE; index++)
by_twos[index] = 2 * (index + 1);
類似地,也應(yīng)該改變第二個(gè)循環(huán)的限制條件。其次,應(yīng)該在數(shù)組名后使用數(shù)組索引:
for(index = 0; index < SIZE; index++)
printf("%d ", by_twos[index]);
錯(cuò)誤的循環(huán)限制條件的一個(gè)危險(xiǎn)的方面在于程序可以運(yùn)行,但是因?yàn)樗褦?shù)據(jù)放在不正確的地方,所以可能在未來(lái)的某個(gè)時(shí)刻不能運(yùn)行,這樣就形成了一種程序中的定時(shí)炸彈。
12、您想要寫一個(gè)返回long值的函數(shù)。在您的函數(shù)定義中應(yīng)該包含什么?
答:
函數(shù)應(yīng)該把返回類型聲明為long,并包含一個(gè)返回long值的return語(yǔ)句。
13、定義一個(gè)函數(shù),該函數(shù)接受一個(gè)int參數(shù),并以long類型返回參數(shù)的平方值。
答:把num的類型指派為long,這樣可以確保運(yùn)算是long運(yùn)算而不是int運(yùn)算。在int為16位的系統(tǒng)上,兩個(gè)int值相乘的結(jié)果在返回之前會(huì)被截尾為一個(gè)int值,這樣就可能丟失數(shù)據(jù)。
long square(int num)
{
return ((long)num) * num;
}
14、以下程序會(huì)打印出什么?
#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
編程練習(xí)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、(
不知這樣寫對(duì)不對(duì),啊!參考書上的例子——程序清單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;
}
我根據(jù)運(yùn)行結(jié)果來(lái)判斷,第一個(gè)無(wú)限序列是發(fā)散的,第二個(gè)無(wú)限序列收斂于0.69(保留兩位小數(shù))12、
#include <stdio.h>
int main(void)
{
int index, array[8];
array[0] = 2; // 我認(rèn)為數(shù)組里面第一個(gè)元素應(yīng)該是2的1次冪,然后依此類推知道最后一個(gè)元素為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++) // 此循環(huán)能否再精簡(jiǎn)一點(diǎn)
dou2[i] = sum;*/
}
// 怎么才能使用一個(gè)循環(huán)來(lái)顯示兩個(gè)數(shù)組中的內(nèi)容,第一行數(shù)組在一行中顯示,而第二個(gè)數(shù)組中的每個(gè)元素在第一個(gè)數(shù)組的
// 對(duì)應(yīng)元素之下進(jìn)行顯示呢???
// 我還是要用兩個(gè)循環(huán)顯示,哎!!!
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; // 換行符也存進(jìn)去了
} 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;
}
// 一個(gè)數(shù)的整數(shù)次冪
double power(int p)
{
double pow = 1.0;
int i;
for(i = 1; i <= p; i++)
{
pow *= (1 + COMPOUND_INTEREST);
}
return pow;
}
//計(jì)算p年后daphne的投資額
double daphne_invest(int p)
{
return ORIGINAL_INVESTMENT + ORIGINAL_INVESTMENT*SINGLE_INTEREST*p;
}
//計(jì)算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;
}
// 一個(gè)數(shù)的整數(shù)次冪
double power(int p)
{
double pow = 1.0;
int i;
for(i = 1; i <= p; i++)
{
pow *= (1 + COMPOUND_INTEREST);
}
return pow;
}
//計(jì)算p年后Chuckie Lucky取出10萬(wàn)美元之后還剩余的錢
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;
}
運(yùn)行結(jié)果為:
The required number of years: 21