1、编写一个程序,不断要求用户输入两个数,直到其中的一个为0。对于每两个数,程序将使用一个函数来计算它们的调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数。
#include<iostream>
using namespace std;
double average(double a, double b){
double c = 1 / (1 / a + 1 / b);
return c;
}
int main(){
double a;
double b;
cin >> a >> b;
while (a != 0 && b != 0){
double c = average(a, b);
cout << "调和平均数为: " << c << endl;
cin >> a >> b;
}
return 0;
}
2、编写一个程序,要求用户输入最多10个高尔夫球成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。请使用3个数组处理函数来分别进行输入、显示和计算平均成绩。
#include<iostream>
using namespace std;
double grade[10];
int num = 0;
void read(){
cout << "请输入高尔夫成绩(最多10个,输入非数字字符提前终止输入):" << endl;
while (cin >> grade[num]){
num++;
if (num >= 10)
break;
}
}
double average(double g[], int length){
double sum = 0;
for (int i = 0; i < length; i++){
sum += g[i];
}
double a = sum / length;
return a;
}
void showresult(){
cout << "您每次的成绩为:" << endl;
for (int i = 0; i < num; i++){
cout << grade[i] << "\t";
}
cout << endl;
cout << "您的平均成绩为:" << endl;
cout << average(grade, num) << endl;
}
int main(){
read();
showresult();
return 0;
}
3、下面是一个结构声明
struct box{
char maker[40];
float height;
float width;
float length;
float volume;
};
a. 编写一个函数,按值传递box结构,并显示每个成员的值。
b. 编写一个函数,传递box结构的地址,并将volume成员设置为其他三位长度的乘积。
c. 编写一个使用这两个函数的简单程序。
#include<iostream>
using namespace std;
struct box{
char maker[40];
float height;
float width;
float length;
float volume;
};
void show(const box b){
cout << "maker:\t" << b.maker << endl;
cout << "height:\t" << b.height << endl;
cout << "width:\t" << b.width << endl;
cout << "length:\t" << b.length << endl;
cout << "volume:\t" << b.volume << endl;
}
void changev(box *b){
b->volume = (b->height)*(b->length)*(b->width);
}
int main(){
box b = { "Hello world!", 3, 4, 5, 1 };
cout << "Before the change:" << endl;
show(b);
changev(&b);
cout << "After the change:" << endl;
show(b);
return 0;
}
5、定义一个递归函数,接受一个整数参数,并返回该参数的阶乘。在程序中对该函数进行测试,程序使用循环让用户输入不同的值,程序将报告这些值的阶乘。
#include<iostream>
using namespace std;
int factorial(int n){
if (n == 0)
return 1;
else
return factorial(n - 1)*n;
}
int main(){
int n;
cout << "请输入数字:" << endl;
while (cin >> n){
if (n < 0){
cout << "输入的数字必须为非负整数" << endl;
}
else{
cout << n << "的阶乘是\t" << factorial(n) << endl;
}
}
return 0;
}
6、编写一个程序,它使用下列函数:
Fill_array()将一个double数组的名称和长度作为参数。它提示用户输入double值,并将这些值存储到数组中。当数组被填满或用户输入非数字时,输入将停止,并返回实际输入了多少数字。
Show_array()将一个double数组的名称和长度作为参数,并显示数组的内容。
Reverse_array()将一个double数组的名称呢过和长度作为参数,并将存储在数组中的值的顺序反转。
#include<iostream>
using namespace std;
int Fill_array(double *array, int length){
cout << "请输入数字:" << endl;
int num = 0;
while (cin >> array[num]){
num++;
if (num >= length){
break;
}
}
return num;
}
void Show_array(const double array[], int length){
cout << "显示数组:\t";
for (int i = 0; i < length; i++){
cout << array[i] << "\t";
}
cout << endl;
}
void Reverse_array(double *array, int length){
cout << "反转" << endl;
int i = 0;
while (i < length - 1 - i){
double temp = array[i];
array[i] = array[length - 1 - i];
array[length - 1 - i] = temp;
i++;
}
}
int main(){
double a[8];
Fill_array(a, 8);
Show_array(a, 8);
Reverse_array(a, 8);
Show_array(a, 8);
}