1. 编写一个程序,不断要求用户输入两个数,直到其中的一个为0。对于每两个数,程序将使用一个函数来计算它们的调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下:
调和平均数=2.0*x*y/(x+y)

//C++ Primer Plus Edition 5//Chapter 7 Homework 1#include<iostream>using namespace std;double heaver(int m, int n);int main() { double m, n, ans; cout << "Please input two numbers: "; cin >> m >> n; while (m != 0 && n != 0) { ans = heaver(m, n); cout << "The answer is " << ans << endl; cout << "Please input two numbers: "; cin >> m >> n; } cout << "Done!" << endl; system("pause"); return 0;}double heaver(int m, int n) { double res; res = 2.0*m*n / (m + n); return res;}

2. 编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。请使用3个数组处理函数来分别进行输入、显示和计算平均成绩。

//C++ Primer Plus Edition 5//Chapter 7 Homework 2#include<iostream>using namespace std;const int MAXSIZE = 10;int fill_array(double scores[], int limit);void show_array(const double ar[], int n);double aver_array(double arc[], int n);int main() { double scores[MAXSIZE]; double average; cout << "Please enter ten scores:" << endl; int size = fill_array(scores, MAXSIZE); cout << "The scores are:" << endl; show_array(scores, size); average = aver_array(scores, size); cout << "The average score is " << aver_array(scores, size) << endl; system("pause"); return 0;}int fill_array(double scores[], int limit) { double temp; int i; for (i = 0; i < limit; i++) { cout << "Enter Score #" << i << ": "; cin >> temp; if (!cin) { cin.clear(); while (cin.get() != '\n') continue; break; } else if (temp < 0) break; scores[i] = temp; } return i;}void show_array(const double ar[], int n) { for (int i = 0; i < n; i++) { cout << "Score #" << (i + 1) << ": " << ar[i] << endl; }}double aver_array(double arc[], int n) { double total = 0; double average; for (int i = 0; i < n; i++) { total += arc[i]; } average = total / n; return average;}

3.下面是一个结构声明:
//省略
a. 编写一个函数,按值传递box结构,并显示每个成员的值。
b. 编写一个函数,传递box结构的地址,并将volume成员设置为其他三维长度的乘积。
c. 编写一个使用这两个函数的简单程序。

//C++ Primer Plus Edition 5//Chapter 7 Homework 3#include<iostream>using namespace std;struct box { char maker[40]; float height; float width; float length; float volume;};void show_box(box member);void set_box(box *pmember);int main() { box b = { "Yu Shuai",3,2,1,1 }; show_box(b); set_box(&b); show_box(b); system("pause"); return 0;}void show_box(box member) { cout << "Box's Maker: " << member.maker << endl; cout << "Box's height: " << member.height << endl; cout << "Box's width: " << member.width << endl; cout << "Box's length: " << member.length << endl; cout << "Box's volume: " << member.volume << endl;}void set_box(box *pmember) { (*pmember).volume = (*pmember).height*(*pmember).length*(*pmember).width;}

4. 许多州的彩票发行结构都使用如程序清单7.4所示的简单彩票玩法的变体。在这些玩法中,玩家从一组被称为域号码(field number)的号码中选择几个。例如,可以从域号码1~47中选择5个号码;还可以从第二个区间(如1~27)选择一个号码(称为特选号码)。要赢得头奖,必须正确猜中所有的号码。中头奖的几率是选中所有域号码的几率与选中号码几率的乘积。例如,在这个例子中,中头奖的几率是从47个号码中正确选取5个号码的几率与从27个号码中正确选择1个号码的几率的乘积。请修改程序清单7.4,以计算中得这种彩票头奖的几率。

//C++ Primer Plus Edition 5//Chapter 7 Homework 4#include<iostream>const int n1 = 5;const int num1 = 47;const int n2 = 1;const int num2 = 27;long double odds(unsigned num, unsigned n);int main(){using namespace std;double odds1 = odds(num1, n1);double odds2 = odds(num2, n2);cout << "中头奖的几率为: " << odds1*odds2 << endl;system("pause");return 0;}long double odds(unsigned num, unsigned n){using namespace std;long double res = 1.0;for (num, n; n > 0; n--, num--)res = res * n / num;return res;}

 

5. 定义一个递归函数,接受一个整数参数,并返回该参数的阶乘。前面讲过,3的阶乘写作3!,等于3*2!,依此类推;而0!被定义为1。通用的计算公式是,如果n大于零,则n!=n*(n-1)!。在程序中对该函数进行测试,程序使用循环让用户输入不同的值,程序将报告这些值的阶乘。

//C++ Primer Plus Edition 5//Chapter 7 Homework 5#include<iostream>using namespace std;int factorial(int n);int main() { int n, ans; cout << "Please input an integer: "; cin >> n; ans = factorial(n); cout << "The answer is " << ans << endl; system("pause"); return 0;}int factorial(int n) { int res = 1; if (n > 1) res = n*factorial(n-1); return res;}

 

6.编写一个程序,它使用下列函数:

Fill_array()将一个double数组的名称和长度作为参数。它提示用户输入double值,并将这些值存储到数组中。当数组被填满或用户输入了非数字时,输入将停止,并返回实际输入了多少个数字。
Show_array()将一个double数组的名称和长度作为参数,并显示该数组的内容。
Reverse_array()将一个double数组的名称和长度作为参数,并将存储在数组中的值的顺序反转。
程序将使用这些函数来填充数组,然后显示数组;反转数组,然后显示数组;反转数组中除第一个和最后一个元素之外的所有元素,然后显示数组。

//C++ Primer Plus Edition 5//Chapter 7 Homework 6#include<iostream>int fill_array(double ar[], int limit);void show_array(double ar[], int size);void reverse_array(double ar[], int size);using namespace std;const int MAXSIZE = 20;int main(){ int n; double shuzu[MAXSIZE]; int size = fill_array(shuzu, MAXSIZE); show_array(shuzu, size); reverse_array(shuzu, size); cout << "反转结果:" << endl; show_array(shuzu, size); system("pause"); return 0;}int fill_array(double ar[], int limit){ double temp; int i; for (i = 0; i<limit; i++) { cout << "Enter value#" << (i + 1) << " \n"; cin >> temp; if (!cin) { cin.clear(); cout << "bad input:\n"; break; } else ar[i] = temp; } return i;}void show_array(double ar[], int size){ for (int i = 0; i<size; i++) cout << "Value #" << (i + 1) << ": " << ar[i] << endl;;}void reverse_array(double ar[], int size){ int i, j; for (i = 1, j = size - i - 1; i<j; i++, j--) { double temp = ar[i]; ar[i] = ar[j]; ar[j] = temp; }}

 

7. 修改程序清单7.7中的3个数组处理函数,使之使用两个指针参数来表示区间。fill_array()函数不返回实际读取了多少个数字,而是返回一个指针,该指针指向最后被填充的位置;其他的函数可以将该指针作为第二个参数,以标识数据结尾。

//C++ Primer Plus Edition 5//Chapter 7 Homework 7#include<iostream>using namespace std;const int Max = 5;double* fill_array(double *ar, double *limit);void show_array(const double *ar, double *n);void revalue(double r, double *ar, double *n);int main() { double properties[Max]; double *size = fill_array(properties, properties+Max); show_array(properties, size); cout << "Enter revaluation factor: "; double factor; cin >> factor; revalue(factor, properties, size); show_array(properties, size); cout << "Done.\n"; system("pause"); return 0;}double *fill_array(double *ar, double *limit) { double temp; double *p; for (p=ar; p < limit; p++) { cout << "Enter value: "; cin >> temp; if (!cin) { cin.clear(); while (cin.get() != '\n') continue; cout << "Bad input;input process terminated.\n"; break; } else if (temp < 0) break; *p = temp; } return p;}void show_array(const double *ar, double *n) { for (const double *p = ar; p < n; p++) { cout << "Property: $"; cout << *p << endl; }}void revalue(double r, double *ar, double *n) { for (ar; ar < n; ar++) (*ar) *= r;}

第8~9题未做。