复习题
编程练习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、
待续....