C++ Primer Plus第五版 第七章 编程练习答案

本文提供C++ Primer Plus第五版第七章的编程练习详细解答,涵盖循环控制、函数调用等核心概念,帮助读者巩固C++编程基础。
摘要由CSDN通过智能技术生成
/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/28 
From : C++ Primer Plus第五版第七章编程练习 第1题  
Problem : 编写一个程序,不断要求用户输入两个数,直到其中一个为0。对于每两个数,程序将使用一个函数来计算它们的 
调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下: 
调和平均数 = 2.0 * x * y / (x + y) 
*******************************************************************************************************************/ 
#include<iostream>
using namespace std;
double harmonic_mean(double x,double y);
int main()
{
	cout << "Enter two numbers (0 represent end): ";
	double x,y;
	while(cin >> x >> y && x*y!=0)
	{
		cout << "The harmonic mean of " << x << " and " << y << " is " << harmonic_mean(x,y) << endl;
		cout << "Enter two numbers (0 represent end): ";
	}
	cout << "Bye! " <<endl;
	system("pause");
	return 0;
}
double harmonic_mean(double x,double y)
{
	return 2.0*x*y/(x+y);
}


/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/28 
From : C++ Primer Plus第五版第七章编程练习 第2题  
Problem : 编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在 
一行上显示所有成绩,然后报告平均成绩。请使用3个数组处理函数来分别进行输入、显示和计算平均成绩。请使用3个数组 
处理函数来分别  
*******************************************************************************************************************/ 
#include<iostream>
using namespace std;
const int SIZE = 10;
int input_scords(double scords[], int size);
void show(const double scords[],int size);
double mean(const double scords[],int size);

int main()
{
	cout << "Enter " << SIZE << " scords: ";
	double scords [10]={0};
	int num;
	num= input_scords(scords, SIZE);
	show(scords,num);
	cout << "The mean scords is " << mean(scords,num) <<endl;
	system("pause");
	return 0;
}
int input_scords(double scords[], int SIZE)
{
	double temp;
	int i=0;
	for(i=0;i<SIZE;i++)
	{
		cin>>temp;
		if(!cin)         //bad input
		{
			cin.clear();
			while(cin.get()!='\n')
				continue;
			cout<<"Bad input:input process terminated.\n";
			break;
		}
		else if(temp<0)
			break;
		scords[i]=temp;
	}
	return i;
}

void show(const double scords[],int size)
{
	for (int i=0; i<size ;i++)
	{
		cout << scords[i] << "  ";
	}
	cout << endl;
}

double mean(const double scords[],int size)
{
	double sum=0;
	for (int i=0; i<size ;i++)
		sum+=scords[i];
	double mean_scords=sum/size;
	return mean_scords;
}

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/28 
From : C++ Primer Plus第五版第七章编程练习 第3题  
Problem : 下面是一个结构声明: 
struct box 
{ 
char maker[40]; 
float height; 
float width; 
float length; 
float volume; 
}; 
a.编写一个函数,按值传递box结构,并显示每个成员的值 
b.编写一个函数,传递box结构的地址,并将volume成员设置为其他三维长度的乘积。 
c.编写一个使用这两个函数的简单程序。 
*******************************************************************************************************************/ 
#include<iostream>
using namespace std;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值