c++ primer plus 第6版 第七章练习题



#include <iostream>
#include <string>
//#include <array>
using namespace std;

int array_input(double ar[]);
double array_averge(double ar[], int n);
void array_show(double ar[], int n);

const int size = 10;

int main() {
	double ar[10] = { 0 };
	cout << "Please input score: \n";
	int num = array_input(ar);
	cout << "The averge score is :";
	cout << array_averge(ar, num) << endl;
	array_show(ar, num);
	return 0;
}

int array_input(double ar[])
{
	int i, count = 0;
	for (i = 0; i < size; i++)
	{
		cin >> ar[i];
		if (!cin)
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "input is not valid.\n";
			break;
		}

	}
	cout << "count = " << i << endl;
	return i;
}

double array_averge(double ar[], int n)
{
	double sum = 0.0;
	for (int i = 0; i < n; i++)
		sum += ar[i];
	return sum / n;
}

void array_show(double ar[], int n)
{
	cout << "The scores are : ";
	for (int i = 0; i < n; i++)
		 cout << ar[i] << " ";
	return;
}

运行结果:



#include <iostream>
#include <string>
using namespace std;

struct box {
	char maker[40];
	float height;
	float width;
	float length;
	float volumn;
};

void struct_show(struct box b);
void struct_address_show(struct box * b);

int main() {
	box bo = { "welcome", 2.0, 3.0, 4.0, 3.0 };
	struct_show(bo);
	struct_address_show(&bo);
	return 0;
}

void struct_show(struct box b)
{
	cout << "maker: " << b.maker << endl;
	cout << "height: " << b.height << endl;
	cout << "width: " << b.width << endl;
	cout << "length: " << b.length << endl;
	cout << "volumn: " << b.volumn << endl;
	return;
}

void struct_address_show(struct box * b)
{
	cout << "address transmit" << endl;
	cout << "maker: " << b->maker << endl;
	cout << "height: " << b->height << endl;
	cout << "width: " << b->width << endl;
	cout << "length: " << b->length << endl;
	cout << "volumn: " << b->height * b->length * b->width << endl;
	return;
}

运行结果:



#include <iostream>
#include <string>
using namespace std;

double proble(unsigned int numbers, unsigned int picks);
int main()
{
	unsigned int total, choice , total2, choice2;
	double choose5, choose1;

	cout << "Enter the total number of choice on the game\n";
	while ((cin >> total >> choice >> total2 >> choice2) && choice <= total)
	{
		choose5 = proble(total, choice);
		choose1 = proble(total2, choice2);
		cout << choose5 << endl;
		cout << "You have one chance in ";
		cout << 1.0 / choose5 / choose1 << endl;
	}
	return 0;
}

double proble(unsigned int numbers, unsigned int picks)
{
	long double result = 1.0;
	long double n;
	unsigned int p;

	for (n = numbers, p = picks; p > 0; n--, p--)
	{
		result = result * n / p;
	}

	return result;
}

运行结果:



char * left(const char * str, int n)*/
{
	if(n < 0)
		n = 0;
	char * p = new char[n+1];
	int i;
	for (i = 0; i < n && str[i]; i++)
		p[i] = str[i];  // copy characters
	while (i <= n)
		p[i++] = '\0';  // set rest of string to '\0'
	return p;
}


#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;

int Fill_array(double arr[], int n);
void Show_array(const double arr[], int n);
void Reverse_array(double arr[], int n);

int main()
{
	int size = 10;
	double m;
	double arr[size];
	int length = Fill_array(arr, size);
	cout << "length = "<< length << endl;
	Show_array(arr, length);
	cout << "Reverse the array\n";
	Reverse_array(arr, length);
	Show_array(arr, length);
	return 0;
}


int Fill_array(double arr[], int n)
{
	int i;
	cout << "Please imput double number:\n";
	for ( i = 0; i < n; i++)
	{
		cin >> arr[i];
		if (!cin)
			break;
	}
	return i;
}

void Show_array(const double arr[], int n)
{
	for (int i = 0; i < n; i++)
		cout << arr[i] << " " << endl;
}

void Reverse_array(double arr[], int n)
{
	double temp = 0;
	for (int i = 0, j = n - 1; i < j; i++, j--)
	{
		temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
}

显示结果:













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值