C Primer Plus Chapter 9 Functional Programming Exercise Answers (self-made)

// Page 237, 9.11 Programming Exercise

9.11.1. Design a function min(x, y), which returns the smaller value of two double type values.

// 9.11.1

#include <stdio.h>
double min(double x, double y);
int main()
{
	printf("Please enter two real numbers that you want:\n");
	double x, y;
	scanf("%lf %lf", &x, &y);
	printf("min number = %lf\n", min(x, y));
	
	return 0;
}
double min(double x, double y)
{
	double ret;
	if(x > y)
		ret = y;
	else
		ret = x;
	return ret;
}

9.11.2 Design a function chline(ch, i, j) to print the specified character j line and i column.

// 9.11.2

#include <stdio.h>
void chline(char ch, int a, int b);
int main()
{
	printf("Please enter the character that you want:\n");
	char ch;
	scanf("%c", &ch);
	printf("Then enter the numbers:\n");
	int i, j;
	scanf("%d %d", &j, &i);
	chline(ch, i, j);
	
	return 0;
	
}
void chline(char ch, int a, int b)
{
	char star[b][a];
	int i, j;
	for(i = 0; i < b; i++){
		for(j = 0; j < a; j++){
			star[i][j] = ch;
			printf("%c", star[i][j]);
		}
		putchar('\n');
	}
	return;
}

9.11.3 Write a function that takes three parameters: a character and two integers. The character parameter is the character to be printed, the first integer specifies the number of times the character is printed in one line, and the second integer specifies the number of lines to print the specified character. (Same as the previous question, change the way of asking)

// 9.11.3

#include <stdio.h>
void func(char, int, int);
int main()
{
	char c;
	int i, j;
	printf("Please enter the character that you want:\n");
	scanf("%c", &c);
	printf("Please enter two integers that you want:\n");
	scanf("%d %d", &i, &j);
	
	func(c, i, j);
	
	return 0;
}
void func(char c, int i, int j)
{
	char star[j][i]; 
	
	int a, b;
	for(a = 0; a < j; a++){
		for(b = 0; b < i; b++){
			star[a][b] = c;
			printf("%c", star[a][b]);
		}
		putchar('\n');
	}
	return;
}

9.11.4 The harmonic mean of two numbers is calculated as follows: first obtain the reciprocal of the two numbers, then calculate the average of the two reciprocals, and finally take the reciprocal of the calculated result. Write a function that takes two arguments of type double and returns the harmonic mean of these two arguments.

// 9.11.4

#include <stdio.h>
double harmonic(double, double);
int main()
{
	double a, b;
	printf("Please enter two real numbers that you want:\n");
	scanf("%lf %lf", &a, &b);
	printf("harmonic mean = %lf\n", harmonic(a, b));
	
	return 0;
}
double harmonic(double a, double b)
{
	 a = 1.0 / a;
	 b = 1.0 / b;
	double average = (a + b) / 2.0;
	 average = 1.0 / average;
	
	return average;
}

9.11.5 Write and test a function large_of( ), which replaces the values ​​of two variables of type double with the larger value. For example, larger_of(x, y) reassigns the larger of x and y to both variables. (pointer)

// 9.11.5

#include <stdio.h>
void larger_of(double *, double *);
int main()
{
	double a, b;
	printf("Please enter 2 real numbers that you want:\n");
	scanf("%lf %lf", &a, &b);
	larger_of(&a, &b);
	printf("%lf %lf", a, b);
	
	return 0;
}
void larger_of(double *i, double *j)
{
	if(*i > *j)
		*j = *i;
	else
		*i = *j;
	return;
}

9.11.6 Write and test a function that takes the addresses of 3 double variables as parameters, puts the minimum value in the first variable, the middle value in the second variable, and the maximum value in the third variable.

// 9.11.6

#include <stdio.h>
void ptr(double *, double *, double *);
int main()
{
	double a = 1.1;
	double b = 2.2;
	double c = 3.3;
	
	ptr(&a, &b, &c);
	printf("a= %lf b=%lf c=%lf\n", a, b, c);
	
	return 0;
}
void ptr(double *i, double *j, double *k)
{
	*i = 4.4;
	*j = 5.5;
	*k = 6.6;
	
	return;
}

9.11.7 Write a function that reads characters from standard input until end-of-file is encountered. The program should report whether each character is a letter. If so, also report the letter's numerical position in the alphabet. For example, both c and c have position 3 in the alphabet. Combine a function that takes a character as an argument and returns a numeric position if the character is a letter, otherwise -1 (input character, output letter's position in the alphabet)

You need to use the ctype.h file header to call the isalpha() function

// 9.11.7

#include <stdio.h>
#include <ctype.h>
int table(char);
int main()
{
	char num;
	printf("Please enter the character that you want:\n");
	scanf("%c", &num);
	printf("character's location = %d", table(num));
	
	return 0;
}
int table(char num)
{
	int ret;
	if(isalpha(num) == 2){     // 小写字母判断为2
		ret = num - 'a' + 1;
	}else if(isalpha(num) == 1){ //  大写字母判断为1
		ret = num - 'A' + 1;
	}else ret = -1;
	
	return ret;
}

9.11.8 In the program listing 6.20 in Chapter 6, the power() function returns a double type number raised to the power of a positive integer. Improve this function so that it correctly computes negative powers. In addition, the function handles 0 to any power of 0 as 0 and any number to the power of 0 as 1 (the function should report 0 to the power of 0 as undefined, so treat the value as 1). To use a loop and test the function in the program.

// 9.11.8

#include <stdio.h>
double power(double, int);
int main()
{
	double x;
	int pow;
	printf("Please enter a real number and an integer that you want:\n");
	scanf("%lf %d", &x, &pow);
	printf("%lf to the power %d is %lf\n", x, pow, 	power(x, pow));
	
	return 0;
}
double power(double x, int pow)
{
	double number = 1.0;
	double ret;
	int i;
	if(pow > 0){
		for(i=0; i<pow; i++){
			number *= x;
			ret = number;
		}
	}else if(pow < 0){
			for(i=0; i<(-pow); i++){
			number *= x;
			ret = 1.0 / number;
		}
	}else{
		printf("Power equals to 0, it's not identified.\n");
		ret = 1;
	}
	
	return ret;
}

9.11.9 Rewriting Programming Exercise 8 Using Recursive Functions

// 9.11.9

#include <stdio.h>
double power(double, int);
int main()
{
	double x;
	int pow;
	printf("Please enter a real number and an integer that you want:\n");
	scanf("%lf %d", &x, &pow);
	
	if(pow >= 0)
	printf("%lf to power %d is %lf\n", x, pow, power(x, pow));
	else
	printf("%lf to power %d is %lf\n", x, pow, (1.0 / power(x, pow)));
	
	return 0;
}
double power(double x, int pow)
{
	double ret;
	double num;
	if(pow != 0){
		if(pow > 0)
		ret = x * power(x, pow-1);	
		if(pow < 0)
		ret = x * power(x, pow+1);
	}else
		ret = 1;
	
	return ret;
}

9.11.10 In order to make the to_binary( ) function in Listing 9.8 more general, write a to_base_n ( ) function that accepts two parameters, and the second parameter is in the range of 2~10, and then the second parameter specifies the to print the value of the first parameter. For example, to_base_n (129, 8) displays 201, the octal number of 129. Test the function in a complete program.

// 9.11.10

#include <stdio.h>
void to_base_n(int, int);
int main()
{
	int num, base;
	printf("Please enter the number and the base that you want:\n");
	scanf("%d %d", &num, &base);
	printf("The number %d based on %d is:\n", num, base);
	putchar('\n');
	to_base_n(num, base);
	putchar('\n');
	
	return 0;
}
void to_base_n(int num, int base)
{
	int ret;
	ret = num % base;
	if(num >= base)
		to_base_n(num / base, base);
	printf("%d", ret);
	
	return;
}

9.11.11 Write and test the Fibonacci( ) function, which uses loops instead of recursion to calculate Fibonacci numbers.

// 9.11.11

#include <stdio.h>
void fibonacci(int);
int main()
{
	int n;
	printf("Please enter the number that you want:\n");
	scanf("%d", &n);
	fibonacci(n);
	
}
void fibonacci(int n)
{
	int fib[n] = {1, 1, };
	int i;
	for(i=0; i<n; i++){
		if(i < 2)
			printf("1 ");
		if(i >= 2){
			fib[i] = fib[i-1] + fib[i-2];
			printf("%d ", fib[i]);
	 }
	}
	return;
}

Guess you like

Origin blog.csdn.net/fengqy1996/article/details/123311086