#include<iostream>usingnamespace std;doubletiaohe(double a,double b);intmain(){
double x, y, ret;
cout <<"Enter x and y (0 to quit):"<< endl;
cin >> x >> y;while(x !=0&& y !=0){
ret =tiaohe(x, y);
cout <<"x 和 y 的调和平均数为:"<< ret << endl;
cout <<"Enter next x and y (0 to quit):"<< endl;
cin >> x >> y;}system("pause");return0;}doubletiaohe(double x,double y){
return2.0* x * y /(x + y);}
2
#include<iostream>usingnamespace std;constint Max =10;intinput_grade(int grade[],int max);voidshow_grade(int grade[],int size);doubleaverage_grade(int grade[],int size);intmain(){
int grade[Max];int size =input_grade(grade,Max);show_grade(grade, size);double ave=average_grade(grade, size);system("pause");return0;}intinput_grade(int grade[],int max){
int i =0;
cout <<"Enter grades(less than 10 and char to quit):"<< endl;//把判断数组是否溢出放在前面,输入判断放在后面while(i<max && cin >> grade[i]){
++i;}return i;}voidshow_grade(int grade[],int size){
cout <<"grades: "<< endl;for(int i =0; i < size; i++){
cout << grade[i]<<" ";}
cout << endl;}doubleaverage_grade(int grade[],int size){
cout <<"average: "<< endl;double average;double sum =0;for(int i =0; i < size; i++){
sum += grade[i];}
average = sum / size;
cout << average << endl;return average;}