#include<iostream>
using namespace std;intmain(){int a,b;doubleharmn(int a,int b);
cout<<"Please enter two digits\n";while(cin>>a>>b){if( a*b!=0)
cout<<"The harmonic mean of two digits is : "<<harmn(a,b);else{
cout<<"two difits have at least one zero,please input again\n";}}}doubleharmn(int a,int b){return(2.0*a*b)/(a+b);}
编写一个程序,请用户输入最多10次比赛成绩,分别编写3个数组函数进行输入、显示和计算平均值。
#include<iostream>constint Size=10;
using namespace std;int nu;intmain(){double*input(double score[Size]);voiddisplay(double*p);doubleaverage(double*p);double score[Size];double* p=input(score);
cout<<"Now display scores you have input\n";display(p);
cout<<"The average score is : "<<average(p);}double*input(double score[Size]){int i;
cout<<"Please enter scores\n";for(i=0;i<Size;i++){if(cin>>score[i]);elsebreak;}
nu=i;return score;}voiddisplay(double*p){for(int i=0;i<nu;i++)
cout<<p[i]<<endl;}doubleaverage(double*p){double total=0;for(int i=0;i<nu;i++)
total+=p[i];return total/nu;}
#include<iostream>
using namespace std;struct box
{char maker[40];float height;float width;float length;float volume;};intmain(){voidvolume(box * vv);voiddisplay(box vd);
box mybox;
cout<<"Enter the maker of your box : ";
cin>>mybox.maker;
cout<<"Enter the height of your box : ";
cin>>mybox.height;
cout<<"Enter the width of your box : ";
cin>>mybox.width;
cout<<"Enter the length of your box : ";
cin>>mybox.length;
box * p=&mybox;volume(p);display(mybox);}voidvolume(box * vv){
vv->volume=vv->height*vv->width*vv->length;}voiddisplay(box vd){
cout<<vd.maker<<endl
<<vd.height<<endl
<<vd.width<<endl
<<vd.length<<endl
<<vd.volume;}
#include<iostream>
using namespace std;intmain(){longdoubleprobability(int number1,int choice,int number2);
cout<<"Please input number1 and choice and number2:\n";int num,chi,nu;while(cin>>num>>chi>>nu && chi<=num){
cout<<"You have one chance "<<probability(num,chi,nu)<<"\nprobability of winning\n";
cout<<"Next to input or inputting q to quit\n";}}longdoubleprobability(int number1,int choice,int number2){longdouble pr=1;for(int i=0;i<choice;i++)
pr=pr*(i+1)/(number1-i);return pr/number2;}