【c++面向过程实验6】函数
#include <iostream>
using namespace std
;
int aliquot(int n1
, int n2
, int n3
)
{
int number
= 0;
for (int i
= n1
; i
<= n2
; i
++) if (i
% n3
== 0) number
++;
cout
<< number
<< endl
;
return 0;
}
int input(int s
[])
{
int i
= 0;
while (i
!=20)
{
cin
>> s
[i
];
if (!s
[0]) return 0;
if (!s
[i
]) break;
i
++;
}
return i
;
}
int Average(int n
, int s
[])
{
int avg
= 0;
for (int i
= 0; i
< n
; i
++) avg
+= s
[i
];
avg
/= n
;
return avg
;
}
int Count(int avg
, int n
, int s
[])
{
int number
= 0;
for (int i
= 0; i
< n
; i
++)
{
if (s
[i
] > avg
) number
++;
}
cout
<< n
<< " " << avg
<< " ";
return number
;
}
int main()
{
cout
<< "1、从键盘输入数行数据,每行三个正整数n1、n2和n3,三个0表示输入结束。每输入完一行,输出n1和n2之间(包括n1和n2)能被n3整除的数的个数。要求用一个函数实现统计整除数个数的功能:" << endl
;
while (1)
{
int n1
, n2
, n3
;
cin
>> n1
>> n2
>> n3
;
if (!n1
&& !n2
&& !n3
) break;
aliquot(n1
, n2
, n3
);
}
cout
<< endl
;
cout
<< "2、从键盘输入数行数据,输出该行数据的个数、平均值和有几个数据大于该行的平均值:" << endl
;
while (1)
{
int s
[20] = { 0 };
int n
= input(s
);
if (!n
) break;
int avg
= Average(n
, s
);
cout
<< Count(avg
, n
, s
) << endl
;
}
}
对于某一个函数int func(int a),无论a在此函数中如何运转变化,它的变化仅限于此函数中,离开了这个函数就还是原来的值 但注意:如果是int a[]这样的数组,其值就会被彻底改变,这是因为在传递参数的时候,数组退化为了指针,详细的说明以后有时间会再写一个博客,并把链接附在下面(ps:记得提醒哦~)
转载请注明原文地址:https://ipadbbs.8miu.com/read-61804.html