标签:using 声明 传递 用户输入 显示 pap 结束 for color
1、编写一个程序,不断要求用户输入两个数,直到其中的一个为0,对于每两个数,程序将使用一个函数来计算它们的调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下:
调和平均数 = 2.0 * x * y / (x + y)
1 #include<iostream> 2 using namespace std; 3 double harmonicAverage(double a, double b); 4 int main(){ 5 double a, b; 6 cout << "Enter two numbers: "; 7 double result; 8 cin >> a >>b; 9 while(a!=0 && b!=0){ 10 result = harmonicAverage(a, b); 11 cout << "Result: " << result << endl; 12 cin >> a >> b; 13 } 14 return 0; 15 } 16 double harmonicAverage(double a, double b){ 17 return 2.0 * a * b / (a + b); 18 }
2、编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。请使用三个数组处理函数来分别进行输入、显示和计算平均成绩。
1 #include<iostream> 2 const int SIZE = 10; 3 int count = 0; 4 using namespace std; 5 void setArray(double[]); 6 void display(const double[]); 7 double averageArr(const double[]); 8 int main(){ 9 double grade[SIZE]; 10 double average; 11 cout << "Enther grade: " << endl; 12 setArray(grade); 13 display(grade); 14 average = averageArr(grade); 15 cout << "Average: " << average; 16 return 0; 17 } 18 void setArray(double arr[]){ 19 int i; 20 for(i = 0; i < SIZE; i++){ 21 cout << "#" << i+1 << ": "; 22 cin >> arr[i]; 23 if(arr[i] == 0) 24 break; 25 26 } 27 count = i; 28 } 29 void display(const double arr[]){ 30 cout << "The grades: " << endl; 31 for(int i = 0; i < count; i++) 32 cout << "#" << i+1 << ": " << arr[i] << endl; 33 } 34 double averageArr(const double arr[]){ 35 double sum; 36 for(int i = 0; i < count; i++) 37 sum += arr[i]; 38 return sum / count; 39 }
3、下面是一个结构的声明:
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
a、编写一个函数,按值传递box结构,并显示每个成员的值。
b、编写一个函数,传递box结构的地址,并将volume成员设置成为其他三维长度的乘积。
c、编写一个运用这两个函数的简单程序。
1 #include<iostream> 2 using namespace std; 3 struct box{ 4 char maker[40]; 5 float height; 6 float width; 7 float length; 8 float volume; 9 }; 10 void display(box); 11 void setvolume(box *); 12 int main(){ 13 box x = {"Ailipapa", 49.6, 37.2, 66.9}; 14 setvolume(&x); 15 display(x); 16 return 0; 17 } 18 void setvolume(box * x){ 19 cout << "Caculating ..." << endl; 20 x->volume = (x->height) * (x->width) * (x->length); 21 cout << "done"; 22 } 23 void display(box x){ 24 cout << "Maker: " << x.maker << endl; 25 cout << "Height: " << x.height << endl; 26 cout << "Width: " << x.width << endl; 27 cout << "Length: " << x.length << endl; 28 cout << "Volume: " << x.volume << endl; 29 }
4、
标签:using 声明 传递 用户输入 显示 pap 结束 for color
原文地址:https://www.cnblogs.com/SChenqi/p/9704151.html