其他章节答案
//Project 1
#include
#define SIZE 26
int main(void)
{
char letter[SIZE];
char l = 'a';
int n;
for (n = 0;n < SIZE;n++,l++)
{
letter[n] = l;
}
for (n = 0;n < SIZE;n++)
{
printf("%c",letter[n]);
}
return 0;
}
//Project 2
#include
int main()
{
int r,l;
for (r = 1;r <= 5;r++)
{
for (l = 1;l <= r;l++)
printf("$");
printf("\n");
}
return 0;
}
//Project 3
#include
int main()
{
int r,l;
char letter;
for (r = 1;r <= 6;r++)
{
for (l = 1,letter = 'F';l <= r;l++,letter--) //此处注意letter每次循环都需要进行重置
{
printf("%c",letter);
}
printf("\n");
}
return 0;
}
//Project 4
#include
int main()
{
int r,l;
char letter = 'A';
for (r = 1;r <= 6;r++)
{
for (l = 1;l <= r;l++,letter++)
{
printf("%c",letter);
}
printf("\n");
}
return 0;
}
//Project 5
#include
int main()
{
int r,l,n;
char letter;
char A = 'A';
printf("Please enter a uppercase letter:");
scanf("%c",&letter);
n = letter - A;
for (r = 1,A = 'A';r <= n + 1;r++)
{
for (l = 1;l <= n + 1 - r;l++)
{
printf(" ");
}
for (l = 1,A = 'A';l <= r;l++,A++)
{
printf("%c",A);
}
/*注意这里要减2,因为上一步的A实际已经达到了A+r */
for (l = 1,A = A - 2;l <= r - 1;l++,A--)
{
printf("%c",A);
}
printf("\n");
}
return 0;
}
//Project 6
#include
int main(void)
{
int small,large,n;
printf("Please enter the upper limit:");
scanf("%d",&large);
printf("Please enter the lower limit:");
scanf("%d",&small);
for (n = small;n <= large;n++)
{
printf("| %4d | %4d | %4d |\n",n,n * n,n * n * n);
}
return 0;
}
//Project 7
#include
int main(void)
{
int n;
char word[40];
printf("Please enter a word:");
scanf("%s",word);
/*字符可以以字符串的形式输入数组,但是注意格式应由字符%c改为字符串%s*/
/*数组名前不用夹地址符&*/
for (n = (strlen(word) - 1);n >= 0;n--)
{
printf("%c",word[n]);
}
return 0;
}
//Project 8
#include
int main(void)
{
float a,b;
printf("Please enter two floats:");
while(scanf("%f%f",&a,&b) != 2)
{
printf("Please enter two correct floats:");
scanf("%*s");
}
printf("%f",(a - b) / (a * b));
return 0;
}
//Priject 9
#include
float project(float a,float b);
int main(void)
{
float a,b;
printf("Please enter two floats:");
while(scanf("%f%f",&a,&b) != 2)
{
printf("Please enter two correct floats:");
scanf("%*s");
}
printf("%f",project(a,b));
return 0;
}
float project(float a,float b)
{
float n;
n = (a - b) / (a * b);
return n;
}
//Project 10
#include
int main(void)
{
int large,small,square,n;
printf("Enter lower and upper integer limits:");
scanf("%d%d",&small,&large);
while(small < large)
{
for (n = small,square = 0;n <= large;n++)
/*这个地方注意square要进行重置*/
{
square = square + n*n;
}
printf("The sums of the squares from %d to %d is %d\n",small*small,large*large,square);
printf("Enter next set of limits:");
scanf("%d%d",&small,&large);
}
printf("Done");
return 0;
}
//Project 11
#include
int main(void)
{
int n;
char integer[8];
printf("Please enter eight integers:");
scanf("%d%d%d%d%d%d%d%d",&integer[0],&integer[1],&integer[2],&integer[3],&integer[4],&integer[5],&integer[6],&integer[7]);
for (n = (strlen(integer) - 1);n >= 0;n--)
{
printf("%d ",integer[n]);
}
return 0;
}
//Project 12
#include
int main(void)
{
int n;
float a1,a2,a3,sum1,sum2,sum3,sum;
printf("Please enter the number of times:");
scanf("%d",&n);
while(n > 0)
{
for (a1 = 1.0,sum1 = 0;a1 <= n;a1 = a1 + 1.0)
sum1 = sum1 + 1.0 / a1;
for (a2 = 1.0,sum2 = 0;a2 <= n;a2 = a2 + 2.0)
sum2 = sum2 + 1.0 / a2;
for (a3 = 2.0,sum3 = 0;a3 <= n;a3 = a3 + 2.0)
sum3 = sum3 - 1.0 / a3;
sum = sum1 + sum2 + sum3;
printf("the sum is %.1f\n",sum);
printf("Please enter the next number of times(<= 0 to quit):");
scanf("%d",&n);
}
printf("Done");
return 0;
}
//Project 13
#include
int main(void)
{
int n,number[8],a;
for (n = 0,a = 1;n < 8;n++)
{
a = a * 2;
number[n] = a;
}
n = 0;
do
{
printf("%d ",number[n]);
n++;
}
while(n < 8);
return 0;
}
//Project 14
#include
int main()
{
double a[8],b[8],sum;
int n;
printf("Please enter eight double numbers:\n");
while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6],&a[7]) != 8)
{
printf("Please enter the correct numbers of eight double types:\n");
scanf("%*c");
}
for (n = 0,sum = 0;n <= 7;n++)
{
sum = sum + a[n];
b[n] = sum;
}
for (n = 0;n <= 7;n++)
{
printf("%8.2lf ",a[n]);
}
printf("\n");
for (n = 0;n <= 7;n++)
{
printf("%8.2lf ",b[n]);
}
printf("\n");
return 0;
}
//Project 15
#include
int main(void)
{
char a[254];
int n,N;
scanf("%s",a);
N = strlen(a);
for (n = N-1;n >= 0;n--)
{
printf("%c",a[n]);
}
return 0;
}
//Project 16
#include
#define INTEREST1 0.1
#define INTEREST2 0.05
#define PRINCIPAL 100
int main(void)
{
float Daphne_m = PRINCIPAL;
float Deirdre_m = PRINCIPAL;
int years = 0;
do
{
Daphne_m = Daphne_m + PRINCIPAL * INTEREST1;
Deirdre_m = Deirdre_m + Deirdre_m * INTEREST2;
years++;
}
while(Deirdre_m < Daphne_m);
printf("Daphne's investment amount is %f.\nDeirdre's investment amount is %f.\nThe years is %d.",Daphne_m,Deirdre_m,years);
return 0;
}
//Project 17
#include
#define INTEREST 0.08
#define PRINCIPAL 100
#define DIFFERENCE 10
int main(void)
{
int years = 0;
float money = PRINCIPAL;
do
{
money = money + money * INTEREST - DIFFERENCE;
years++;
}
while(money > 0);
printf("In the %d years Chuckie will take out the money");
return 0;
}
//Project 18
#include
#define DUMBAR 150
#define INFER 5
int main(void)
{
int n,frend;
for (n = 1,frend = INFER;frend <= DUMBAR;n++)
{
frend = (frend - n) * 2;
printf("Weeks:%d.\nFrends:%d.\n\n",n,frend);
}
return 0;
}