【发布时间】:2016-03-16 17:03:55
【问题描述】:
在使用 STL 编程方面,我还是个新手,我想我已经掌握了窍门。但我对此有点困惑。我的目标是获取 5 个值,然后打印出我的值,打印其中的最大值,打印平均值,然后打印其中的最小值(我的问题)。似乎我的变量“low”被赋予了 0 的值,我不知道这是为什么。我已经测试过我的价值观是否被读入,据我所知,确实如此。因此,如果有人能告诉我为什么我似乎无法获得适当的最低值,我将不胜感激。感谢您的宝贵时间。
vector<double> vecList;
int x = 0;
double high = 0;
double low = 0;
double sum = 0;
cout << "Enter Integer Values, then press Ctrl(z) to Quit:" << endl;
for (int i=0; i < 5; i++)
{
cin >> x;
sum = sum + x;
vecList.push_back(x);
}
vector<double>::iterator intVecIter;
cout <<"List contains: ";
for (intVecIter = vecList.begin(); intVecIter != vecList.end(); ++intVecIter)
cout << *intVecIter << " ";
for (int i=0; i < 5; i++)
{
if(vecList[i] > high)
{
high = vecList[i];
}
// prints out "0"
if(low > vecList[i])
{
low = vecList[i];
}
}
cout << endl << "Largest: "<< fixed << setprecision(2) << high << endl;
cout << "Smallest: "<< fixed << setprecision(2) << low << endl;
cout << "Average: " << fixed << setprecision(2)<< (sum/5);
return 0;
【问题讨论】:
-
想想你是如何初始化
low...