C++ Primer Plus章节编程练习(第七章)

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

   调和平均数 = 2.0 * x * y / (x + y)

 1 #include<iostream>
 2 using namespace std;
 3 double harmonicAverage(double a, double b);
 4 int main(){
 5     double a, b;
 6     cout << "Enter two numbers: ";
 7     double result;
 8     cin >> a >>b;
 9     while(a!=0 && b!=0){
10         result = harmonicAverage(a, b);
11         cout << "Result: " << result << endl;
12         cin >> a >> b;
13     }
14     return 0;
15 }
16 double harmonicAverage(double a, double b){
17     return 2.0 * a * b / (a + b);
18 }

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

 1 #include<iostream>
 2 const int SIZE = 10;
 3 int count = 0;
 4 using namespace std;
 5 void setArray(double[]);
 6 void display(const double[]);
 7 double averageArr(const double[]);
 8 int main(){
 9     double grade[SIZE]; 
10     double average;
11     cout << "Enther grade: " << endl;
12     setArray(grade);
13     display(grade);
14     average = averageArr(grade);
15     cout << "Average: " << average;
16     return 0;
17 }
18 void setArray(double arr[]){
19     int i;
20     for(i = 0; i < SIZE; i++){
21         cout << "#" << i+1 << ": ";
22         cin >> arr[i];
23         if(arr[i] == 0)
24             break;
25 
26     }
27     count = i;
28 }
29 void display(const double arr[]){
30     cout << "The grades: " << endl;
31     for(int i = 0; i < count; i++)
32         cout << "#" << i+1 << ": " << arr[i] << endl;
33 }
34 double averageArr(const double arr[]){
35     double sum;
36     for(int i = 0; i < count; i++)
37         sum += arr[i];
38     return sum / count;
39 }

3、下面是一个结构的声明:

  struct box

  {

    char maker[40];

    float height;

    float width;

    float length;

    float volume;

  };

   a、编写一个函数,按值传递box结构,并显示每个成员的值。

   b、编写一个函数,传递box结构的地址,并将volume成员设置成为其他三维长度的乘积。

   c、编写一个运用这两个函数的简单程序。

 1 #include<iostream>
 2 using namespace std;
 3 struct box{
 4     char maker[40];
 5     float height;
 6     float width;
 7     float length;
 8     float volume;
 9 };
10 void display(box);
11 void setvolume(box *);
12 int main(){
13     box x = {"Ailipapa", 49.6, 37.2, 66.9};
14     setvolume(&x);
15     display(x);
16     return 0;
17 }
18 void setvolume(box * x){
19     cout << "Caculating ..." << endl;
20     x->volume = (x->height) * (x->width) * (x->length); 
21     cout << "done";
22 }
23 void display(box x){
24     cout << "Maker: " << x.maker << endl; 
25     cout << "Height: " << x.height << endl; 
26     cout << "Width: " << x.width << endl; 
27     cout << "Length: " << x.length << endl; 
28     cout << "Volume: " << x.volume << endl; 
29 }

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

 1 #include<iostream>
 2 using namespace std;
 3 long double probability(double numbers, double picks);
 4 int main(){
 5     long double pro1 = 1.0;
 6     long double pro2 = 1.0;
 7     pro1 = probability(47.0, 5);    //47个号码中选对5个的几率 
 8     pro2 = probability(27.0, 1);    //27个号码中选对1个的几率 
 9     cout << "Probability: " << pro1 * pro2; 
10     return 0;
11 }
12 long double probability(double numbers, double picks){
13     long double result = 1.0;
14     for( ; picks > 0; numbers--, picks--)
15         result *= picks / numbers;
16     return result;
17 }

5、定义一个递归函数,接受一个整数参数,并返回该参数的阶乘。在程序中对该函数进行测试,程序使用循环让用户输入不同的值,程序将报告这些值的阶乘。

 1 #include<iostream>
 2 using namespace std;
 3 long long factorial(int number);
 4 int main(){
 5     int number;
 6     long long result;
 7     cout << "Enter a number: ";
 8     while(cin >> number && number!=0){
 9         result = factorial(number);
10         cout << "Result: " << result << endl;
11         cout << "Enter a number: ";
12     }
13     return 0;
14 }
15 long long factorial(int number){
16     if(number == 1)
17         return 1;
18     else
19         return number * factorial(number-1);
20 }

6、编写一个程序,他使用下列函数:

   Fill_array( )将一个double数组的名称和长度作为参数。它提示用户输入double值,并将这些值存储到数组中。当数组被填满或用户输入了非数字时,输入将停止,并返回实际输入了多少数字。

   Show_array( )将一个double数组的名称和长度作为参数,并显示该数组的内容。

   Reverse_array( )将一个double数组的名称和长度作为参数,并将存储在数组中的值的顺序反转。

   程序将使用这些函数来填充数组,然后显示数组;反转数组,然后显示数组;反转数组中除第一个和最后一个元素之外的所有元素,然后显示数组。

 1 #include<iostream>
 2 using namespace std;
 3 const int SIZE = 10;
 4 int Fill_array(double arr[], int n);        //返回数组的长度 
 5 void Show_array(double arr[], int n);
 6 void Reverse_array(double arr[], int n);
 7 int main(){
 8     int length;
 9     double array[SIZE]; 
10     length = Fill_array(array, SIZE);    //填充数组,并返回实际输入了多少个数字 
11     Show_array(array, length);    //显示数组 
12     Reverse_array(array, length);
13     Show_array(array, length);
14     return 0;
15 }
16 int Fill_array(double arr[], int n){
17     int length = 0;
18     cout << "Enter numbers: " << endl;
19     while(length < n && cin >> arr[length])
20         length++;
21     return length;
22 }
23 void Show_array(double arr[], int length){
24     for(int i = 0; i < length; i++)
25         cout << "#" << i+1 << ": " << arr[i] << endl; 
26 }
27 void Reverse_array(double arr[], int length){
28     int i = 0;
29     int j = length - 1;
30     double temp;
31     while(i < j){
32         temp = arr[i];    
33         arr[i] = arr[j];
34         arr[j] = temp;
35         i++;
36         j--;
37     }
38 }

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

 

posted @ 2018-09-25 23:00  SChenqi  阅读(909)  评论(0编辑  收藏  举报