c primer plus 4th edition Programing Exercises answer(chapter 6)

c primer plus 第四版课后习题答案
chapter 6
1
Write a program that creates an array with 26 elements and stores the 26 lowercase letters in it. Also have it show the array 
#include<stdio.h>
int main(void)
{
char alphabet[26];
int i;
char begin='a';
for(i=0;i<26;i++,begin++)
alphabet[i]=begin;
for(i=0;i<26;i++)
printf("%c ",alphabet[i]);


}
2
/*
Use nested loops to produce the following pattern:
$
$$
$$$
$$$$
$$$$$
*/
#include<stdio.h>
int main(void)
{
for(int i=1;i<=5;i++){
for(int j=0;j<i;j++)
printf("$");
printf("\n");
}
}
3
/*
Use nested loops to produce the following pattern:
F
FE
FED
FEDC
FEDCB
FEDCBA
*/
#include<stdio.h>
int main(void)
{
char begin;
for(int i=1;i<=6;i++)
{
for(int j=0, begin='F';j<i;begin--,j++)
printf("%c",begin);
printf("\n");
}
}
4
/*
Have a program request the user enter an uppercase letter. Use nested loops to produce a pyramid pattern like this:
    A
   ABA
  ABCBA
 ABCDCDA
ABCDEDCBA
The pattern should extend to the character entered. For example, the preceding pattern would result from an input value of E. 
*/
#include<stdio.h>
#include<ctype.h>
int main(void)
{
printf("input an uppercase letter:\n");
char input,a,ina,space;
int i,j;
while((input=getchar())>'Z'||input<'A')
printf("not an uppercase letter!please input again!\n");
printf("here is the alphabet pyramid!\n");
for(a='A';a<=input;a++){
ina='A';
space=input-a;
while(space-->0)
putchar(' ');
if(ina==a){
putchar(ina);
}else{
while(ina<a)
putchar(ina++);
while(ina>='A')
putchar(ina--);
}
putchar('\n');
}
}
5,
// Write a program that prints a table with each line giving an integer, its square, and its cube. Ask the user to input the lower and upper limits for the table. Use a for loop.
#include<stdio.h>
int main(void)
{
printf("input two number:the lower and upper limits\nlike this:1 2\n");
int lower,upper;
int status;
while((status=scanf("%d%d",&lower,&upper))!=2){
if(!status)
getchar();
printf("not valid ,please try again!\n");
printf("input two number:the lower and upper limits\nlike this:1 2\n");
}
printf("the lower is %d and the upper is %d\nHere is the table:\n",lower,upper);
printf("%10s|%10s|%10s\n","base","square","cube");
for(;lower<=upper;lower++){
printf("%10d|%10d|%10d\n",lower,lower*lower,lower*lower*lower);
}
}
6
// Write a program that reads a single word into a character array and then prints the word backward. 
这个题目我第一遍答案总是有莫名其妙的字符出现,考虑是数组初始化的问题,所以先摸索数组初始化的方式
#include<stdio.h>
#include<string.h>
int t1[10];//全局变量,数组会被初始化为0;
int main(void)
{
int t2[10]={};//局部变量,这种模式,相当于给初始化了0.
int t3[10];//局部变量,未初始化,不确定数组里有什么.
for(int i=0;i<10;i++)
printf("%d\n",t1[i]);
printf("----t2---\n");
for(int i=0;i<10;i++)
printf("%d\n",t2[i]);
printf("----t3---\n");
for(int i=0;i<10;i++)
printf("%d\n",t3[i]);
return 0;
}
下面是答案
#include<stdio.h>
#include<string.h>
int main(void)
{
char input[40]={};
for(int i=0;i<40&&(input[i++]=getchar())!='\n';);
for(int i=strlen(input);i>0,putchar(input[--i]););
return 0;
}
7
Write a program that requests two floating-point numbers and prints the value of their difference divided by their product. Have the program loop through pairs of input values until the user enters non-numeric input.
我理解的这边的"the value of their difference " 是差的绝对值的意思.
#include<stdio.h>
int main(void)
{
float input1,input2;
int status;
printf("input two number or q to quit!\n");
while(status=scanf("%f%f",&input1,&input2)==2){
float dif=input1-input2;
if(dif<0)
dif*=-1;
float product=input1*input2;
float result;
result=dif/product;
printf("the result is %.2e\n",result);
}
printf("GoodBye!");
return 0;
}
8,
Modify exercise 7 so that it uses a function to return the value of the calculation.
#include<stdio.h>
float myFunc(float n1,float n2);
int main(void)
{
float input1,input2;
int status;
printf("input two number or q to quit!\n");
while(status=scanf("%f%f",&input1,&input2)==2)
printf("the result is %.2e\n",myFunc(input1,input2));
printf("GoodBye!");
return 0;
}
float myFunc(float input1,float input2){
float dif=input1-input2;
if(dif<0)
dif*=-1;
float product=input1*input2;
return dif/product;
}
9,
Write a program that reads eight integers into an array and then prints them in reverse order
#include<stdio.h>
float myFunc(float n1,float n2);
int main(void)
{
int input[8]={};
printf("input 8 integers or q to quit!\n");
int i=0;
while(i<8&&scanf("%d",&input[i++]));
while(i>0)
printf("\n%d ",input[--i]);
return 0;
}
10,
Consider these two infinite series:
s=1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + ...
1.0 - 1.0/2.0 + 1.0/3.0 - 1.0/4.0 + ...
Write a program that evaluates running totals of these two series up to some limit of number of terms. Have the user enter the limit interactively. Look at the running totals after 20 terms, 100 terms, 500 terms. Does either series appear to be converging to some value? Hint: –1 times itself an odd number of times is –1, and –1 times itself an even number of times is 1.
#include<stdio.h>
int main(void){
printf("enter the limit:\nor q to quit!\n");
for(int input;scanf("%d",&input);){
int flag=-1;
double r1=0.0;
double r2=0.0;
for(int i=1;i<=input;i++){
r1+=1.0/i;
flag*=-1;
r2+=flag*(1.0/i);
}
printf("the fist result is :%f\nthe second result is :%f\n",r1,r2);
printf("another input:\n");
}
}
11
Write a program that creates an eight-element array of int and sets the elements to the first eight powers of 2 and then prints the values. Use a for loop to set the values, and, for variety, use a do while loop to display the values.
#include<stdio.h>
int main(void)
{
int value=1;
int valueArr[8]={};
for(int i=0;i<8;i++){
value*=2;
valueArr[i]=value;
}
int i=0;
do{
printf("%d ",valueArr[i++]);
}while(i<8);
return 0;
}
12
Write a program that creates two eight-element arrays of double and uses a loop to let the user enter values for the eight elements of the first array. Have the program set the elements of the second array to the cumulative totals of the elements of the first array. For example, the fourth element of the second array should equal the sum of the first four elements of the first array, and the fifth element of the second array should equal the sum of the first five elements of the first array. (It's possible to do this with nested loops, but by using the fact that the fifth element of the second array equals the fourth element of the second array plus the fifth element of the first array, you can avoid nesting and just use a single loop for this task.) Finally, use a loop to display the contents of the two arrays, with the first array displayed on one line and with each element of the second array displayed below the corresponding element of the first array.
#include<stdio.h>
int main(void){
printf("input 8  numbers \n");
double fArr[8],sArr[8];
int i=0;
while(i<8&&(scanf("%lf",&fArr[i++])==1));
i=0;
while(i<8)
printf("%6.2lf,",fArr[i++]);
double res=0;
putchar('\n');
for(i=0;i<8;i++){
res+=fArr[i];
sArr[i]=res;
printf("%6.2lf,",sArr[i]);
}
}
13
Write a program that reads in a line of input and then prints the line in reverse order. You can store the input in an array of char; assume that the line is no longer than 255 characters. Recall that you can use scanf() with the %c specifier to read a character at a time from input and that the newline character (\n) is generated when you press the Enter key.
#include<stdio.h>
#include<string.h>
int main(void)
{
char chArr[255]={};
int i=0;
do{
scanf("%c",&chArr[i]);
}while(i<255&&chArr[i++]!='\n');
int len=strlen(chArr)-1;
// printf("%d\n",len);
i=0;
while(len)
printf("%c",chArr[--len]);
}
14
Daphne invests $100 at 10% simple interest. (That is, every year, the investment earns an interest equal to 10% of the original investment.) Deirdre invests $100 at 5% interest compounded annually. (That is, interest is 5% of the current balance, including previous addition of interest.) Write a program that finds how many years it takes for the value of Deirdre's investment to exceed the value of Daphne's investment. Also show the two values at that time.
#include<stdio.h>
int main(){
//interest1,2
//base investment1,2
//total 1,2
double i1,i2,b1,b2,t1,t2;
i1=0.1;
i2=0.05;
b1=100;
b2=100;
int year=1;
t1=b1+b1*i1;
t2=b2+b2*i2;
while(t1>=t2){
t1+=b1*i1;
t2+=t2*i2;
year++;
}
printf("after %d year,Deirdre's investment to exceed the value of Daphne's investment.\nand there value of investment is :\nDaphne:%.2f\nDeirdre:%.2f\n",year,t1,t2);
return 0;
}


15
//Chuckie Lucky won a million dollars, which he places in an account that earns 8% a year. On the last day of each year, Chuckie withdraws $100,000. Write a program that finds out how many years it takes for Chuckie to empty his account.


#include<stdio.h>
int main(){
const double base=1000000.0;
const double rate=0.08;
const double withdraw=100000;
int year=1;
double balance=base;
while(balance>0){
balance+=balance*rate;
balance-=withdraw;
year++;
// printf("%d,%.2f\n",year,balance);
}
printf("After %d years ,Chuckie empties his account\n",year);
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值