C++ PrimerPlus第六版第七章函数编程练习答案

  • Post author:
  • Post category:其他




1.

#include<iostream>
using namespace std;
double average(double x, double y);

int main()
{
   
    double x, y;
    while ((cin >>x && cin >> y)&& (x && y))
        cout << "调和平均数是:" << average(x, y) << endl;
    return 0;
}
double average(double x, double y)
{
   
    return 2.0 * x * y / (x + y);
}



2.

#include<iostream>
using namespace std;
const int SIZE = 10;
void input(int arr[], int n);
void show(int arr[], int n);
double ave(int arr[], int n);

int main()
{
   
	int data[SIZE];
	input(data, SIZE);
	show(data, SIZE);
	cout << "平均值是:" << ave(data, SIZE);

	return 0;
}
void input(int arr[], int n)
{
   
	cout << "请输入最多10个高尔夫成绩: \n";
	int i = 0, value;
	while (i < n && cin >> value)
		arr[i++] = value;
}
void show(int arr[], int n)
{
   
	int i = 0;
	cout << "所有成绩是: \n";
	while (i < n)
		cout << arr[i++] << " ";
	cout << '\n';
}
double ave(int arr[], int n)
{
   
	int sum = 0;
	for (int i = 0; i < n; ++i)
		sum += arr[i];

	return sum / n;
}



3.

#include<iostream>
using namespace std;
struct box
{
   
	char maker[40];
	float height;
	float width;
	float length;
	float volume;
};
void show(box pf);
void setvolume(box *pf);

int main()
{
   
	box pf;

	cout << "输入制造人的名字: ";
	cin.getline(pf.maker, 40);
	cout << "输入高度: ";
	cin >> pf.height;
	cout << "输入长度: ";
	cin >> pf.length;
	cout << "输入宽度: ";
	cin >> pf.width;
	setvolume(&pf);
	show(pf);

	return 0;
}
void show(box pf)
{
   
	cout << "制造人是: " << pf.maker << endl;
	cout << "高度是: " << pf.height << endl;
	cout << "宽度是: " << pf.width << endl;
	cout << "长度是: " << pf.length << endl;
	cout << "体积是: " << pf.volume << endl;
}
void setvolume(box *pf)
{
   
	pf->volume = pf->height * pf->length * pf->width;

}



4.

#include<iostream>
using namespace std;
long double probability(unsigned number, unsigned numbers);

int main()



版权声明:本文为qq_44800780原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。