【发布时间】:2018-02-21 06:07:16
【问题描述】:
到目前为止,我的大部分功能都已正常工作。我现在被困在最后一步。我需要从我的主函数到我的“组织”函数中获取一个数组。我需要在特定范围内获取重复值,并将它们分别放入同一个数组元素中并计算它们。完成后,我将打印出带有星号的直方图。范围是-----bin 0. if score = 10 but
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
/*int histo(int x);*/
double dev(int count, int* scores, double mn);
double mean(int count, int* stats);
int main()
{
int scores[101];
int count = 0;
int bin[10];
double mn;
cout << "Enter a score (-1 to stop): ";
do
{
cin >> scores[count++];
} while (scores[count - 1] != -1);
count--;
mn = mean(count, scores);
cout << "mean: " << mean(count, scores) << endl;
cout << "dev: " << dev(count, scores, mn) << endl;
system("pause");
return 0;
}
int histo(int* scores)
{
int bins[10]{};
int counter = 0;
for (int i = 0; i < *scores; i++)
{
if (*scores < 10)
{
bins[counter++];
}
else if (*scores >= 10 && *scores < 20)
{
bins[counter++];
}
else if (*scores >= 20 && *scores < 30)
{
bins[counter++];
}
else if (*scores >= 30 && *scores < 40)
{
bins[counter++];
}
else if (*scores >= 40 && *scores < 50)
{
bins[counter++];
}
else if (*scores >= 50 && *scores < 60)
{
bins[counter++];
}
else if (*scores >= 60 && *scores < 70)
{
bins[counter++];
}
else if (*scores >= 80 && *scores < 90)
{
bins[counter++];
}
else if (*scores >= 90)
{
bins[counter++];
}
for (int j = 0; j < )
}
}
double dev(int count, int* scores, double mn)
{
double x = 0;
double y = 0;
double d = 0;
for (int i = 0; i < count; i++)
{
x = pow(scores[i] - mn, 2);
y += x;
}
d = sqrt(y / count);
return d;
}
double mean(int count, int* scores)
{
double total = 0;
for (int i = 0; i < count; i++)
{
total += scores[i];
}
return total / count;
}
我知道我已经用 if 语句杀死了。这就是我不确定该怎么做的地方。
【问题讨论】:
-
'else' 已经暗示分数大于或等于前面的语句。此外,取消引用分数指针并不表示数组的结尾。你现在拥有的几乎拥有它
-
如果您确实保持在分数范围内,您将超出垃圾箱的范围,因为您每次遇到任何语句时都会增加计数器。换句话说,您当前的解决方案充满了未定义的行为