C Primer Plus 第6版 编程练习答案(第六章)

C Primer Plus 第6版 各章编程练习答案 | 2021-04-11 12:41:59 | 阅读 143 次 | 评论(0)

6.1.c

#include <stdio.h>
int main(void)
{
	char letter[26];
	int i;
	
	for(i = 0; i < 26; i++)
	{
		letter[i] = 'a' + i;
		printf("%c ", letter[i]);
	}
	return 0;
}


6.2.c

#include <stdio.h>
int main(void)
{
	int rows, chars;
	
	for(rows = 0; rows < 5; rows++)
	{
		for(chars = 0; chars <= rows; chars++)
			printf("$");
		printf("\n");
	}
	return 0;
}


6.3.c

#include <stdio.h>
int main(void)
{
	int i, j;
	
	for(i = 0; i < 6; i++)
	{
		for(j = 0; j <= i; j++)
			printf("%c", 'F' - j);
		printf("\n");
	}
	return 0;
}


6.4.c

#include <stdio.h>
int main(void)
{
	char word = 'A';
	int i, j;
	
	for(i = 0; i < 6; i++)
	{
		for(j = 0; j <= i; j++)
			printf("%c", word++);
		printf("\n");
	}
	return 0;
}


6.5.c

#include <stdio.h>
int main(void)
{
	char letter;
	
	printf("Please enter a capital letter.\n");
	scanf("%c", &letter);
	
	const int n = letter - 'A';
	
	int space, j, row;
	for(space = n, row = 0; space >= 0; space--, row++)
	{
		for(j = 0; j < space; j++)
		{
			printf(" ");
		}
		for(j = 0; j <= row; j++)
		{
			printf("%c", 'A' + j);
		}
		for(j = row; j > 0; j--)
		{
			printf("%c", 'A' + j - 1);
		}		
		printf("\n");
	}
	return 0;
}


6.6.c

#include<stdio.h>
int main(void)
{
	int n,m;
	printf("请输入表格开始的数:\n");
	scanf("%d",&n);
	printf("请输入表格结束的数:\n");
	scanf("%d",&m);

	printf("\n");
	printf("原数\t平方\t立方\t\n");
	for(n=n; n<=m; n++)
		printf("%d\t%d\t%d\n",n,n*n,n*n*n);
	
	return 0;

}


6.7.c

#include <stdio.h>
#include <string.h>
int main(void)
{
	char word[10];
	printf("Please enter a word.\n");
	scanf("%s", word);
	int lenght = strlen(word);
	int i;
	for(i = lenght - 1; i >= 0; i--)
		printf("%c", word[i]);
	
	return 0;
}


6.8.c

#include <stdio.h>
int main(void)
{
	float value_1, value_2;
	printf("Please enter two floating point number(q to quit)\n");
	while(scanf("%f %f", &value_1, &value_2) == 2)
	{
		float result = (value_1 - value_2) / (value_1 * value_2);
		printf("result = %f\n", result);
		printf("Please enter two floating point number(q to quit)\n");
	}
	
	return 0;
}


6.9.c

#include <stdio.h>
float result(float v1, float v2);
int main(void)
{
	float value_1, value_2;
	printf("Please enter two floating point number(q to quit)\n");
	while(scanf("%f %f", &value_1, &value_2) == 2)
	{
		printf("result = %f\n", result(value_1, value_2));
		printf("Please enter two floating point number(q to quit)\n");
	}
	return 0;
}
float result(float v1, float v2)
{
	float result = (v1 - v2) / (v1 * v2);
	return result;
}


6.10.c

#include <stdio.h>
int main(void)
{
	int lower, upper;
	printf("Enter lower and upper integer limits:");
	while(scanf("%d %d", &lower, &upper) == 2 && upper > lower)
	{
		int i, sum;
		sum = 0;
		for(i = lower; i <= upper; i++)
			sum += i * i;
		printf("The sums of the squares from %d to %d is %d\n",
			lower * lower, upper * upper, sum);
		printf("Enter next set of limits:");
	}
	printf("Done");
	
	return 0;
}


6.11.c

#include <stdio.h>
#define N 8
int main(void)
{
	int i, num[N];
	printf("Please enter 8 numbers:\n");
	for(i = 0; i < N; i++)
		scanf("%d", &num[i]);
	printf("Reverse order printing 8 numbers:\n");
	for(i = N - 1; i >= 0; i--)
		printf("%d ", num[i]);
	return 0;
}


6.12.c

#include <stdio.h>
int main(void)
{
	int n;
	printf("Please enter a numbe (0 to quit): ");
	while(scanf("%d", &n) == 1 && n > 0)
	{
		double result_1 = 0.0;
		double result_2 = 0.0;
		int i;
		for(i = 1; i <= n; i++)
		{
			result_1 += 1.0 / i;
			if (i % 2 == 1)
                result_2 += 1.0 / i;
            else
                result_2 -= 1.0 / i;
		}
		        printf("1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + ...");
        printf("sum are %g\n", n, result_1);
        printf("1.0 - 1.0/2.0 + 1.0/3.0 - 1.0/4.0 + ...");
        printf("sum are %g\n", n, result_2);
        printf("Please enter a numbe (0 to quit): ");
	}
	return 0;
}


6.13.c

#include <stdio.h>
#define N 8

int main(void)
{
    int a[N], i;
    int val = 2;

    for (i = 0; i < N; i++)
    {
        a[i] = val;
        val *= 2;
    }
    i = 0;
    printf("Here are the results for array:\n", N);
    do
    {
        printf("%d ", a[i++]);
    } while (i < N);
    printf("\nDone.\n");

    return 0;
}


6.14.c

#include <stdio.h>
#define N 8

int main(void)
{
    int i;
    double a[N], b[N];

    printf("Please enter %d numbers:\n", N);
    
    for (i = 0; i < N; i++)
        scanf("%lf", &a[i]);
        
    b[0] = a[0];
    
    for (i = 1; i < N; i++)
        b[i] = a[i] + b[i - 1];
        
    printf("Here are the results for array a:\n");
    
    for (i = 0; i < N; i++)
        printf("%-3g", a[i]);
        
    printf("\nHere are the results for array b:\n");
    
    for (i = 0; i < N; i++)
        printf("%-3g", b[i]);

    printf("\nDone.\n");

    return 0;
}


6.15.c

#include <stdio.h>
#define LEN 255

int main(void)
{
    int i = 0;
    char input[LEN];

    printf("Please enter a string:\n");
    do
    {
        scanf("%c", &input[i]);
    } while (input[i] != '\n' && ++i && i < LEN);

    printf("Reversing print the string is:\n");
    for (i--; i >= 0; i--)
        printf("%c", input[i]);
    printf("\nDone.\n");

    return 0;
}


6.16.c

#include <stdio.h>
#define RATE_SIMP 0.10
#define RATE_COMP 0.05
#define INIT_AMT 100.0

int main(void)
{
    int years = 0;
    double daphne = INIT_AMT;
    double deirdre = INIT_AMT;

    do
    {
        daphne += RATE_SIMP * INIT_AMT;
        deirdre += RATE_COMP * deirdre;
        years++;
    } while (deirdre < daphne);
    printf("Investment values after %d years:\n", years);
    printf("Daphne: $%.2f\n", daphne);
    printf("Deirdre: $%.2f\n", deirdre);
    printf("Deirdre(invest) > Daphne(invest)\n");

    return 0;
}


6.17.c

#include <stdio.h>
#define TAX 0.08

int main(void)
{
    int i = 0;
    double Chuckie = 100.0;

    do
    {
        i++;
        Chuckie += Chuckie * TAX;
        Chuckie -= 10;
        printf("(%d)account:%g.\n", i, Chuckie);
    } while (Chuckie > 9);
    printf("After %d years:\n", ++i);
    printf("Chuckie has taken all of the money!\n");

    return 0;
}


6.18.c

#include <stdio.h>

int main(void)
{
    int i = 1;
    int friends = 5;

    while (friends < 150)
    {
        printf("At %d weeks, Rabnud has", i);
        printf("%4d friends.\n", friends);
        friends = 2 * (friends - i++);
    }
    printf("At %d weeks, over Dunbar's number(150).\n", i);

    return 0;
}

文章评论,共0条
作者仅允许登录用户评论,请后再评论
浏览1403次
文章归档
最新评论