"C Primer Plus" (sixth edition) answers (6.16)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43219615/article/details/99944674

To use the "C Primer Plus" C beginners learning to prepare. Some of the questions may be different, please check yourself.

  1. Write a program that creates an array contains 26 letters, and store the 26 lowercase letters. Then print the entire contents of the array.
#include <stdio.h>
int main(void)
{
	char c[26];
	int cnt = 0;
	
	/*C99支持for循环内定义变量 如果报错就把变量声明提到外面*/
	for(char i = 'a'; i <= 'z'; i++) {
		c[cnt++] = i;
	}

	for(int i = 0; i < cnt; i++) {
		printf("%c ", c[i]);
	}
	
	return 0;
}
  1. Nested loop, printing characters in the following format:
    Picture format
#include <stdio.h>
int main(void)
{
	/*第一个循环控制打印的行数 第二个循环控制每行的输出*/
	for(int i = 0; i < 5; i++) {
		for(int j = 0; j < i + 1; j++) {
			printf("$"); 
		}
		printf("\n");
	}
	
	return 0;
}
  1. Nested loop, the letter print in the following format:
    Picture format
    Note: If your system does not use ASCII or other coded in numerical order of the code, you can put a character array is initialized to letters of the alphabet:
       char lets [26 is] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      then use an array index to choose a single letter, for example, lets [0] is 'a', and the like.
#include <stdio.h>
int main(void)
{
	/*第一个循环控制行数 第二个循环控制输出 可以观察每行的输出和行数的关系*/
	for(int i = 1; i <= 6; i++) {
		for(int j = 'F'; j > 'F' - i; j--) {
			printf("%c", j);
		}
		printf("\n");
	} 
	
	return 0;
}
  1. Using nested loop, the letter print in the following format:
    Picture format
#include<stdio.h>
int main(void)
{
	char ch='A';
	
	for(int i=0; i < 6; i++)
	{
		for(int j=0; j <= i; j++)
			printf("%c", ch++);
		printf("\n");
	}
	
	return 0;
}
  1. Let the program requires the user to enter an uppercase letter, the pyramid image generating nested loop pattern such as the following:
    Picture format
      This pattern is extended to the character input by the user. For example, when the front of the input pattern is a need to generate E. NOTE: use an external loop to process rows, three in each row inner loop, a processing space, a letter printing in ascending order, a descending order print letters. If your system does not use ASCII or similar to the strict letter of the coded representation in numerical order, please see the recommendations given in the programming exercise 3.
#include <stdio.h>
int main(void)
{
	char c;
	
	scanf("%c", &c);
	for(int i = 'A'; i <= c; i++) {
		//打印空格 
		for(int j = i; j < c; j++) {
			printf(" ");
		}
		//升序打印字母 
		for(int j = 'A'; j <= i; j++) {
			printf("%c", j);
		}
		//降序打印字母 
		for(int j = i-1; j >= 'A'; j--) {
			printf("%c", j);
		}
		printf("\n");
	}
	
	return 0;
}
  1. Write a program that prints a table, each row of the table gives an integer, its square and its cube. User input table requires upper and lower limits. Use a for loop.
#include<stdio.h>
int main(void)
{
	int i_max;
	int i_min;
	
	printf("请输入上下限:");
	scanf("%d%d", &i_max, &i_min);
	for(int i=i_min; i<=i_max; i++)
	{
		printf("%d %d %d\n",i, i*i, i*i*i);
	}
	
	return 0;
}
  1. Write a program to read a word into a character array, then print out the reverse of the word. Tip: Use the strlen () array index of the last character (Chapter 4) is calculated.
#include<stdio.h>
#include<string.h>
int main(void)
{
	char word[40];
	int limit;
	
	printf("Enter a word:");
	scanf("%s",&word);
	limit = strlen(word);
	for(int i = limit-1; i>=0; i--)
		printf("%c", word[i]);
	
	return 0;
}
  1. Write a program requires two input floating-point number, and then print the result obtained by dividing the product of the two with the difference between the two. Before the user typed non-numeric input program loop processing for each input value pair.
#include<stdio.h>
int main(void)
{
	double one,two;
	printf("Enter q to quit:"); 
	
	while(scanf("%lf%lf", &one, &two) == 2)
	{
		printf("%-16f", (one-two) / (one*two));
	}
	printf("Done!");
	
	return 0;
}
  1. Exercise 8 to modify it to have a function that returns the calculated value.
#include<stdio.h>

double f(double one, double two) {
	return (one-two) / (one*two);
}

int main(void)
{
	double one,two;
	printf("Enter q to quit:"); 
	
	while(scanf("%lf%lf", &one, &two) == 2)
	{
		printf("%-16f", f(one, two));
	}
	printf("Done!");
	
	return 0;
}
  1. Write a program, require the user to input the lower limit and an upper limit integer integer, and sequentially calculates the square of each integer of from a lower limit to an upper limit of the additive, the final results are displayed. The program will continue to prompt the user to input the lower limit and upper integer integers and display the answer, the upper limit until the user input is an integer equal to or less than the lower limit integer up. Sample results should be run as follows:
    the Enter Integer Lower and Upper Limits:. 5. 9
    of The sums of from 25 to 81 Squares The IS 255
    the Enter Next SET of Limits: 25. 3
    of The sums of Squares The IS 625 to 5520. 9 from
    the Enter Limits of SET Next:. 5. 5
    the Done
#include <stdio.h>
int main(void)
{
	int lower, upper;
	
	printf("Enter lower and upper integer limits:");
	scanf("%d %d", &lower, &upper);
	
	while(upper > lower) {
		int sum = 0;
		for(int 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:");
		scanf("%d %d", &lower, &upper);
	} 
	
	printf("Done");
	return 0;
}
  1. Writing a program read into the eight integers in an array, and inverting output.
#include <stdio.h>
int main(void)
{
	int arr[8];
	
	printf("请输入八个整数:\n");
	for(int i = 0; i <= 7; i++)
	{
		scanf("%d", &arr[i]);
	}
	for(int i = 7; i >= 0; i--)
	{
		printf("%d ", arr[i]);
	}
	
	return 0; 
}
  1. Consider these two infinite series:
      1.0 + 1.0 / + 2.0 1.0 / 3.0 + 1.0 / + 4.0 ...
      1.0 to 1.0 / 2.0 + 1.0 / 3.0-1.0 / 4.0 + ...
      sum write a program to calculate the two sequences changing until it reaches a certain number of times. Allowing users to interactively enter this number. After a look at the sum of 20 times, 100 times and 500 times. Whether each sequence seems to converge to a certain value? Tip: odd number multiplied by -1 is -1, while the even number 1 is multiplied by -1.
#include <stdio.h>
double series1(int);
double series2(int);
int main(void)
{
	int count;
	
	printf("请输入要计算的次数:\n");
	scanf("%d", &count);
	printf("Result:\n%lf\t%lf\t%lf+%lf=%lf", series1(count), series2(count), series1(count), series2(count), series1(count)+series2(count));
	
	return 0;
}
double series1(int count)
{
	int i;
	double sum = 0;
	double temp;
	
	for(i = 1; i <= count; i++)
	{
		temp = 1.0 / i;
		sum += temp;
	}
	
	return sum;
}
double series2(int count)
{
	int i;
	double sum = 1;
	double temp = -1;
	
	for(i = 2; i <= count; i++)
	{
		temp = temp / i;
		sum += temp;
	}
	
	return sum;
}
  1. Write a program that creates an array of eight elements int, and the front element 8 are provided to power of 2, and then print out their values. Used to set values ​​for loop; For variations, uses do while loop to display these values.
#include <stdio.h>
#include <math.h>
int main(void)
{
	int a[8];
	
	for(int i = 0; i < 8; i++) {
		a[i] = pow(2, i+1);
	}
	
	int i = 0;
	do {
		printf("%d ", a[i]);
		i++;
	} while(i < 8);
	
	return 0;
}
  1. Write a program to create a double array of two elements 8, use a loop to allow users to type a value of eight elements of the first array. The program elements of the second array to the first array element and cumulative. For example, the fourth element of the second array should be equal to the first element of the first four and the array, the fifth element of the second array should be equal to the first five elements and the first array (with embedded sets of loops can do this, but with a second array of five elements equal to the five elements of the first array of the fact that the fourth element of the second array plus, but only to avoid nesting a single cycle to complete this task). Finally, the use of a loop to display the contents of two arrays, a first array on one line, while the second array each element is displayed under the corresponding element of the first array.
#include <stdio.h>
int main(void)
{
	double a[8];
	double _a[8];
	
	printf("请输入八个数字:\n");
	for(int i = 0; i <= 7; i++)
		scanf("%lf",&a[i]);
		
	for(int i = 0; i <= 7; i++)
	{
		for(int j = 0; j <= i; j++)
		{
			_a[i] += a[j];
		}
	}
	
	for(int i = 0; i <= 7; i++)
	{
		printf("%.2f ",a[i]);
	}
	
	printf("\n");
	
	for(int i = 0; i <= 7; i++)
	{
		printf("%.2f ",_a[i]);
	}
	
	return 0;
}
  1. Write a program that reads a line of input, then reverse print that line. You can enter stored in a char array; assumes that the line is not more than 255 characters. Recall that you can have scanf% c specifier () reads a character from the first input, and when you press the enter key generates a newline character (\ n).
#include <stdio.h>
#include <string.h>
int main(void)
{
	char arr[256];
	char temp;
	int i = 0;
	int maxLength;
	
	while((temp = getchar()) != '\n' && i < 256)
	{
		arr[i] = temp;	
		i++;
	}
	
	maxLength = strlen(arr);
	
	for(i = maxLength -1; i >= 0; i--)
	{
		printf("%c",arr[i]);
	}
	putchar(temp);
	
	return 0;	
} 
  1. Daphne 10% simple interest invested $ 100 (that is, the annual interest earned investment equal to 10% of the original investment). Deirdre places a 5% compound interest per year invested $ 100 (that is, the current balance of interest is 5%, including the previous interest). Write a program to calculate the required investment will be more than many years of Deirdre Daphne, and shows that time investment of two people.
#include <stdio.h>
int main()
{
	double daphne = 100;
	double deidre = 100;
	int year = 0;
	double RATE_SIMP = .1;
	double RATE_COMP  = .05;
	double INIT_AMT = 100;
	
	while(deidre <= daphne)
	{
		daphne += RATE_SIMP * INIT_AMT;
		deidre += RATE_COMP * deidre;
		year++;
	}
	printf("Investment values after %d years:\n", year);
	printf("Daphne: %.2f\tDeidre: %.2f", daphne, deidre);
	
	return 0;
}
  1. Chuckie Lucky won $ 1 million, he put it into a win by 8% per year of account. On the last day of each year, Chuckie withdraw $ 100,000. Write a program to calculate how many years Chuckie would empty his account.
#include<stdio.h>
int main(void)
{
	double d = 100;
	int year = 0;
	
	while(d > 0) {
		d += d * 0.08;
		d -= 10;
		year++;
	}
	
	printf("%d years later, Chuckie Lucky whill do that.", year); 
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43219615/article/details/99944674