所以我必须创建一个函数来平衡用户在一个数组中输入的数字,该数组最多可以包含10个数字,但是可以通过用户输入-1来停止第一个输入和第十个之间的任何位置/ p>
我不确定它是否类似于找到我做过的最高数字
我现在所做的是但我不知道如何让它对数字进行平均,因为它不会被一组数字除掉
cout << "The average of the results = " << calc_average(score) << "\n";
cout << "The lowest of the results = " << find_lowest(score) << "\n";
system("Pause");
}
double calc_average(double a[])
{
}
double find_highest(double a[])
{
double temp = 0;
for(int i=0;i<10;i++)
{
if(a[i]>temp)
temp=a[i];
}
return temp;
}
编辑:澄清一下,最大数字是用户可以输入的结果是10,这就是它达到10的原因。
答案 0 :(得分:0)
我现在所做的是但我不知道如何去做 平均数字,因为它不会被一组数字除以
您应该通过保留计数器来跟踪用户输入的数量。
然后你可以用它作为你的除数,得到平均值。
答案 1 :(得分:0)
试试这段代码......
double calc_average(double a[])
{
double fAverage = 0.0f;
double fCount = 0.0f;
double fTotal = 0.0f;
for(int i=0; i<10; i++)
{
if(a[i] < 0)
break;
fTotal += a[i];
fCount += 1.0f;
}
if( fCount > 0.0f )
fAverage = fTotal / fCount;
return fAverage;
}
答案 2 :(得分:0)
您可以在此处使用迭代器作为计数器和名为avg
的变量来存储平均值。最后只返回avg
。
double find_highest(double a[])
{
double avg, temp = 0;
int i;
for(i=0; i<10; i++)
{
if(a[i]>temp)
temp += a[i];
}
avg = temp/i;
return avg;
}
答案 3 :(得分:0)
以下代码应该适合您(尽管我可能会在某处出现一个错误)。秘密是for循环中的额外条件。
double calc_average(double a[])
{
int i;
float sum = 0.0;
for(i = 0; i < 10 && a[i] > -1; i++)
{
sum += a[i];
}
if(i > 0)
{
return sum / i;
}
else
{
return 0.0; /* Technically there is no average in this case. */
}
}