求解决,求满足。
(新手上路。) 求好心人把下面几个问题代码给我让我看下流程和思路。
1:求1-100之间的奇数和。
2:求1-100之间的奇数个数。
3:求1-100之间奇数的平均值。
4:求1-100之间的偶数之和。
- 白痴帝发帖。
[ 本帖最后由 Love5201314 于 2013-9-25 08:32 编辑 ]
程序代码:#include <iostream>
#include <assert.h>
#define SIZE 100
void dealWithNums(int arrayNums[],int& nCnt,int& nSum,double& fFloat,int& nSumOther)
{
assert(arrayNums != NULL);
nCnt = nSum = nSumOther = 0;
for (int i = 0; i < SIZE; i++)
{
if (arrayNums[i]%2 == 0)
nSumOther += arrayNums[i];
else
{
nCnt++;
nSum += arrayNums[i];
}
}
fFloat = nSum*1.0/nCnt;
}
int main()
{
int arrayNums[SIZE];
int nCnt,nSum,nSumOther;
double fFloat;
for (int i = 0; i < SIZE; i++)
arrayNums[i] = i + 1;
dealWithNums(arrayNums,nCnt,nSum,fFloat,nSumOther);
std::cout<<"奇数个数 : "<<nCnt<<std::endl;
std::cout<<"奇数之和 : "<<nSum<<std::endl;
std::cout<<"奇数平均值 : "<<fFloat<<std::endl;
std::cout<<"偶数之和 : "<<nSumOther<<std::endl;
return 0;
}