题目:7.1
编写一个程序,不断要求用户输入两个数,直到其中的一个为0。对于每两个数,程序将使用一个函数来计算它们的调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下:调和平均数=2.0 * x * y / (x+ y)
代码:
int harmonic_average(int x, int y)
{
return 2.0 * x * y / (x + y);
}
void Func()
{
int x;
int y;
cout << "请输入两个数:";
while (cin >> x && cin >> y && x != 0 && y != 0)
{
cout << "调和平均数为:" << harmonic_average(x, y) << endl;
cout << "请输入两个数:";
}
}
题目:7.2
编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在一行上显示所有成绩, 然后报告平均成绩。请使用3个数组处理函数来分别进行输入、显示和计算平均成绩。
代码:
#define MAX_SIZE 10
void Input(int *arr,int &count)
{
//输入66表示结束
count = 0;
for (int i = 0; i < MAX_SIZE; i++)
{
cout << "请输入第" << i + 1 << "位的成绩:";
cin >> arr[i];
if (arr[i] == 666)
{
break;
}
count++;
}
}
void Show(int *arr,int count)
{
cout << "--------------数据内容如下-----------------------" << endl;
for (int i = 0; i < count; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int Average(int *arr,int count)
{
int total = 0;
for (int i = 0; i < count; i++)
{
total += arr[i];
}
total = total / count;
cout << "平均数为:" << total << endl;
return total;
}
题目:7.3
下面是一个结构体的声明:
struct box {
char maker[40];
float height;
float width;
float length;
float volume;
}
a. 编写一个函数,按值传递
box
结构体,并显示每个成员的值。 b. 编写一个函数,传递box
结构体的地址,并将 volume 成员设置为其他三维长度的乘积。 c. 编写一个使用这两个函数的简单程序。
代码:
struct box {
char maker[40];
float height;
float width;
float length;
float volume;
};
void Show_box(box project)
{
cout << "maker=" << project.maker << endl;
cout << "height=" << project.height << endl;
cout << "width=" << project.width << endl;
cout << "length=" << project.length << endl;
cout << "volume=" << project.volume << endl;
}
void Set_Volume(box& project)
{
project.volume = project.height * project.width * project.length;
}
void Use_box()
{
box project1;
box project2;
strcpy_s(project1.maker, "盒子1");
strcpy_s(project2.maker, "盒子2");
project1.length = 2;
project1.width = 5;
project1.height = 4;
project2.length = 3;
project2.width = 3;
project2.height = 9;
Set_Volume(project1);
Set_Volume(project2);
Show_box(project1);
Show_box(project2);
}
题目:7.4
许多州的彩票发行机构都使用如程序清单7.4所示的简单彩票玩法的变体。在这些玩法中,玩家从一组被称为域号码(field number)的号码中选择几个。例如,可以从域号码1~47中选择5个号码;还可以从第二个区间(如1~27)选择一个号码(称为特选号码)。要赢得头奖,必须正确猜中所有的号码。中头奖的几率是选中所有域号码的几率与选中特选号码几率的乘积。例如,在这个例子中,中头奖的几率是从47个号码中正确选取5个号码的几率与从27个号码中正确选择1个号码的几率的乘积。请修改程序清单7.4,以计算中得这种彩票头奖的几率。
代码:
long double probability(unsigned numberAll, unsigned numberNeed)
{
//算numberNeed!
long double probabilityNeed = 1;
for (int i = 1; i <= numberNeed; i++)
{
probabilityNeed *= i;
}
//算A47,5
long double probabilityAll=1;
for (int i = numberAll; i > numberAll-numberNeed; i--)
{
probabilityAll *= i;
}
return probabilityNeed/probabilityAll;
}
int main(void)
{
double field_number = 47, field_need= 5;
double special_number = 27, special_need = 1;
cout << "获奖的概率为:" <<
probability(field_number, field_need) * probability(special_number, special_need) <<
endl;
system("pause");
return 0;
}
题目:7.5
题: 定义一个递归函数,接受一个整数参数,并返回该参数的阶乘。
代码:
long factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n-1);
}
题目:7.6
编写一个程序,它使用下列函数:
Fill_array()
将一个 double 数组的名称和长度作为参数。它提示用户输入 double 值,并将这些值存储到数组中。 当数组被填满或用户输入了非数字时,输入将停止,并返回实际输入了多少个数字;Show_array()
将一个 double 数组的名称和长度作为参数,并显示该数组的内容;Reverse-array()
将一个 double 数组的名称和长度作为参数,并将存储在数组中的值的顺序反转。程序将使用这些函数来填充数组,然后显示数组;反转数组,然后显示数组;反转数组中除第一个和最后一个元素之外的所有元素,然后显示数组。
代码:
int Fill_array(double* array, int size)
{
int count = 0;
cout << "请输入double类型的值:(非数字可提前结束)";
while (cin >> array[count])
{
if (count ==size-1)
{
cout << "数组已全部初始化" << endl;
return size;
}
else
{
cout << "请输入double类型的值:(非数字可提前结束)";
}
count++;
}
return count ;
}
void Show_array(double* array, int count)
{
cout << "---------------------当前数据如下------------------" << endl;
for (int i = 0; i < count; i++)
{
cout << array[i] << " ";
}
cout << endl;
}
void Reverse_array(double* array, int count)
{
int left = 0;
int right = count - 1;
while (left < right)
{
double temp = array[left];
array[left] = array[right];
array[right] = temp;
left++;
right--;
}
}
//反转数组中除第一个和最后一个元素之外的所有元素,然后显示数组。
void Reverse_fist_last(double* array, int count)
{
if (count <= 2)
{
return;
}
int left = 1;
int right = count - 2;
while (left < right)
{
double temp = array[left];
array[left] = array[right];
array[right] = temp;
left++;
right--;
}
}
int main()
{
double array[10];
int count = Fill_array(array, 10);
cout << "count=" << count << endl;
Show_array(array, count);
Reverse_array(array, count);
Show_array(array, count);
Reverse_fist_last(array, count);
cout << "反转数组中除第一个和最后一个元素之外的所有元素" << endl;
Show_array(array, count);
system("pause");
return 0;
}
标签:cout,号码,int,double,float,C++,plus,数组,习题 From: https://blog.csdn.net/qq_71286244/article/details/139409578