导航

程序6.13和习题6.2

Posted on 2009-12-17 19:11  spock  阅读(119)  评论(0)    收藏  举报

#include<iostream>   
using namespace std;

const int MAX=5;

void main()  
{  
	double fish[MAX];
	cout<<"请输入鱼的重量,按下任意字母表示停止输入"<<endl;
	cout<<"最多输入"<<MAX<<"条鱼"<<endl;
	cout<<"第条鱼:";
	int i=0;
	while(i<MAX&&cin>>fish[i])
	{
		if(++i<MAX)
			cout<<""<<i+1<<"条鱼:";
	}
		double total=0.0;
		for(int j=0;j<i;j++)
			total+=fish[j];
		if(i==0)
			cout<<"没有鱼:("<<endl;
		else
			cout<<i<<"条鱼的平均重量是"<<total/i<<endl;
		cout<<"完毕!"<<endl;

}  

习题6.1

#include<iostream>   
using namespace std;

const int MAX=10;

void main()  
{  
	double fish[MAX];
	cout<<"请输入鱼的重量,按下任意字母表示停止输入"<<endl;
	cout<<"最多输入"<<MAX<<"条鱼"<<endl;
	cout<<"第条鱼:";
	int i=0,j=0;
	while(i<MAX&&cin>>fish[i])
	{
		if(++i<MAX)
			cout<<""<<i+1<<"条鱼:";
	}
		double total=0.0;
		int large=0;//large是比平均重量大的鱼
		for(j=0;j<i;j++)
			total+=fish[j];
		for(j=0;j<i;j++)
		{
			if(fish[j]>total/i)
				large++;
		}
		if(i==0)
			cout<<"没有鱼:("<<endl;
		else
		{
			cout<<i<<"条鱼的平均重量是"<<total/i<<endl;
			cout<<""<<large<<"条鱼超过了平均重量"<<endl;
		}
		cout<<"完毕!"<<endl;

}