double array的总和得到不正确的答案e
问题描述:
我得到奇怪的错误与双元素和,它看起来像1e + 002,当答案是双和。double array的总和得到不正确的答案e
const int ARRAY_SIZE =20;
//Index variable
int i;
//For finding average
double sum=0.00;
cout.precision(2);
srand((unsigned)time(0));
double main_array[ARRAY_SIZE];
//Header
cout << "Element number \t\t" << "Random Number\n\n" << endl;
//Assigning random values into array.
for (i=0; i< ARRAY_SIZE; i++)
{
//Randomizer
double ran =(rand()/((float)RAND_MAX/(10-0)));
main_array[i] = ran;
sum+=main_array[i];
答
字母e
表示scientific符号。它的存在不会使值“不正确”。使用std::fixed
流操纵器指定定点表示法。
cout << fixed << sum;
+0
非常感谢你:) – Satnam
1E + 002 = 1×10^2 = 100,如果有帮助 –
你的算法似乎是随机的。你期望什么答案,为什么你认为生成的答案是不正确的? – user2079303