双阵列的总和与e获得不正确的答案

Sum of double array gets incorrect answer with e

本文关键字:不正确 答案 阵列      更新时间:2023-10-16

我遇到了双元素总和的怪异错误,看起来像1E 002,当答案为double sum。

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 tt" << "Random Numbernn" << 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表示科学符号。它的存在不会使值"不正确"。

使用std::fixed流操纵器指定定点符号。

cout << fixed << sum;