C Primer Plus 第六版 第六章 程式設計練習答案
1.編寫一個程式,建立一個具有26個元素的陣列,並在其中儲存26個小寫字母。並讓該程式顯示該陣列的內容。
#include<stdio.h>
#define SIZE 26
int main()
{
char zimu[SIZE];
int i;
for(i = 0; i < SIZE; i++)
zimu[i] = 'a' + i;
for(i = 0; i < SIZE; i++)
printf("%c ",zimu[i]);
printf("\n");
return 0;
}
2.使用巢狀迴圈,按照下面的格式列印字元:
#include<stdio.h>
int main()
{
const int ROWS = 5;
const int CHARS = 5;
int row,ch;
for(row = ROWS; row > 0; row--)
{
for(ch = row; ch <= CHARS; ch++)
printf("$");
printf("\n");
}
return 0;
}
3.使用巢狀迴圈,按照下面的格式列印字母:
F
FED
FEDC
FEDCB
FEDCBA
請注意:如果您的系統不使用ASCⅡ或其他以數字順序編碼的碼,您可以把一個字元陣列初始化為字母表中的字母:
char lets[26]=”ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
然後就可以使用陣列索引來選用單個的字母,例如lets[0]是’A’,等等。
#include<stdio.h>
int main()
{
int i;
char ch;
for(i = 0; i < 6; i++)
{
for(ch = 'F'; ch >= ('F' - i); ch--)
printf("%c", ch);
printf("\n");
}
return 0;
}
4.使用巢狀迴圈,按照下面的格式列印字元:
A
BC
DEF
GHIJ
KLMNO
PQRSTU
#include<stdio.h>
#define N 6
int main()
{
char a = 'A';
int i,j;
for (i = 0; i < N; i++)
{
for (j = 0; j <= i; j++)
{
printf("%c", a++);
}
printf("\n");
}
return 0;
}
5.讓程式要求使用者輸入一個大寫字母,使用巢狀迴圈產生像下面這樣的金字塔圖案:
這種圖案要擴充套件到使用者輸入的字元。例如,前面的圖案是在輸入E時需要產生的。提示:使用一個外部迴圈來處理行,在每一行中使用三個內部迴圈,一個處理空格,一個以升序列印字母,一個以降序列印字母。如果您的系統不使用ASCII或類似的以嚴格數字順序表示字母的編碼,請參見在程式設計練習3中給出的建議。
#include<stdio.h>
int main(void)
{
int i,j,k;
char letter;
printf("Please input a capital:");
scanf("%c",&letter);
k=letter-'A'+1;
for(i=0;i<k;i++)
{
for(j=0;j<k-i;j++)
printf(" ");
for(j=0;j<=i;j++)
printf("%c",'A'+j);
for(j=i-1;j>=0;j--)
printf("%c",'A'+j);
printf("\n");
}
return(0);
}
6.編寫一個程式列印一個表,表的每一行都給出一個整數、它的平方以及它的立方。要求使用者輸入表的上限與下限。使用一個for迴圈。
#include <stdio.h>
int main( void )
{
int lower, upper, index;
int square, cube;
printf("Enter starting integer and ending integer: ");
while(scanf("%d %d", &lower, &upper) == 2)
{
printf("%5s %10s %15s\n", "num", "square", "cube");
for (index = lower; index <= upper; index++)
{
square = index * index;
cube = index * square;
printf("%5d %10d %15d\n", index, square, cube);
}
printf("Enter next pair (non-numeric to quit): \n");
}
return 0;
}
7.編寫一個程式把一個單詞讀入一個字元陣列,然後反向打印出這個詞。提示:使用strlen()(第4章)計算陣列中最後一個字元的索引。
#include<stdio.h>
int main()
{
char a[20];
int i;
printf("Please input a word: ");
scanf("%s",a);
for(i=strlen(a)-1;i>=0;i--)
printf("%c",a[i]);
printf("\n");
return 0;
}
8.編寫一個程式,要求輸入兩個浮點數,然後打印出用二者的差值除以二者的乘積所得的結果。在使用者鍵入非數字的輸入之前程式迴圈處理每對輸入值。
#include<stdio.h>
int main()
{
float a,b,c;
printf("Enter two float numbers: ");
while(scanf("%f %f", &a, &b) == 2)
{
c = (a - b)/(a * b);
printf("c = %f\n",c);
printf("Enter next pair (non-numeric to quit): \n");
}
return 0;
}
9.對練習7進行修改,讓它使用一個函式來返回計算值。
#include<stdio.h>
float jisuan(float u, float v);
int main()
{
float a,b;
printf("Enter two float numbers: ");
while(scanf("%f %f", &a, &b) == 2)
{
printf("the result is %f\n",jisuan(a, b));
printf("Enter next pair (non-numeric to quit): \n");
}
return 0;
}
float jisuan(float x, float y)
{
return ((x - y)/(x * y));
}
10.編寫一個程式,要求使用者輸入下限整數和一個上限整數,然後,依次計算從下限到上限的每一個整數的平方的加和,最後顯示結果。程式將不斷提示使用者輸入下限整數和上限整數並顯示出答案,直到使用者輸入的上限整數等於或小於下限整數為止。程式執行的結果示例應該如下所示:
Enter lower and upper integer limits: 5 9
Enter next set of limits:3 25
The sums of the squares from 9 t0 625 iS 5520
Done
#include<stdio.h>
int main()
{
int lower,upper,index;
printf("Enter lower and upper integer limits: ");
while(scanf("%d %d", &lower, &upper) == 2)
{
if(lower < upper)
{
int sum = 0;
for(index = lower; index <= upper; index++)
sum += index*index;
printf("The sums of the squares from %d to %d is %d \n", lower*lower, upper*upper, sum);
printf("Enter next set of limits: ");
scanf("%d %d", &lower, &upper);
}
else
break;
}
printf("Done\n");
return 0;
}
#include<stdio.h>
int main()
{
int lower,upper,index;
printf("Enter lower and upper integer limits: ");
scanf("%d %d", &lower, &upper);
while(lower < upper)
{
int sum = 0;
for(index = lower; index <= upper; index++)
sum += index*index;
printf("The sums of the squares from %d to %d is %d \n", lower*lower, upper*upper, sum);
printf("Enter next set of limits: ");
scanf("%d %d", &lower, &upper);
}
printf("Done\n");
return 0;
}
11.編寫一個程式把8個整數讀入一個數組中,然後以相反的順序列印它們。
#include <stdio.h>
#define SIZE 8
int main()
{
int a[SIZE], i;
printf("Enter %d interges.\n", SIZE);
for(i=0;i<SIZE;i++)
scanf("%d", &a[i]);
for(i=SIZE-1;i>=0;i--)
printf("%d ", a[i]);
printf("\n");
return 0;
}
13.編寫一個程式,建立一個8個元素的int陣列,並且把元素分別設定為2的前8次冪,然後打印出它們的值。使用for迴圈來設定值;為了變化,使用do while迴圈來顯示這些值。
#include <stdio.h>
#define SIZE 8
int main()
{
int a[SIZE], i;
a[0] = 1;
for(i=1;i<SIZE;i++)
{
a[i] = 2*a[i-1];
}
i = 0;
do
{
printf("%d ",a[i]);
i++;
}while(i < SIZE);
printf("\n");
return 0;
}
14.編寫一個程式,建立兩個8元素的double陣列,使用一個迴圈來讓使用者鍵入第一個陣列的8個元素的值。程式把第二個陣列的元素設定為第一個陣列元素的累積和。例如,第二個陣列的第4個元素應該等於第一個陣列的前4個元素的和,第二個陣列的第5個元素應該等於第一個陣列的前5個元素的和(使用巢狀迴圈可以做到這一點。不過利用第二個陣列的第5個元素等於第二個陣列的第4個元素加上第一個陣列的第5個元素這一事實,可以避免巢狀而只使用單個迴圈來完成這個任務)。最後,使用一個迴圈來顯示兩個陣列中的內容,第一個陣列在一行中顯示,而第二個陣列中的每個元素在第一個陣列的對應元素之下進行顯示。
#include<stdio.h>
#define SIZE 8
int main()
{
double a[SIZE], b[SIZE];
int i;
printf("Enter %d double number:\n", SIZE);
for(i=0;i<SIZE;i++)
scanf("%lf",&a[i]);
b[0] = a[0];
for(i=1;i<SIZE;i++)
b[i] = b[i-1] + a[i];
for(i=0;i<SIZE;i++)
printf("%8g ",a[i]);
printf("\n");
for(i=0;i<SIZE;i++)
printf("%8g ",b[i]);
printf("\n");
return 0;
}
15.編寫一個程式讀入一行輸入,然後反向列印該行。您可以把輸入儲存在一個char陣列中;假定該行不超過255個字元。回憶一下,您可以使用具有%c說明符的scanf()從輸入中一次讀入一個字元,而且當您按下回車鍵時會產生換行字元(\n)。
#include<stdio.h>
#define SIZE 20
int main()
{
char a[255];
int i;
printf("Enter a line: ");
for(i=0;i<SIZE;i++)
scanf("%c", &a[i]);
for(i=SIZE-1;i>=0;i--)
printf("%c", a[i]);
printf("\n");
return 0;
}