【发布时间】:2015-01-26 11:44:32
【问题描述】:
我目前有一个浮点数组声明并初始化了一系列值,例如3.2、4.4、9.8。 while 循环将数组的最高值元素添加到变量最高值中。这可以正常工作,并输出数组中的最高元素。我正在尝试找到一种方法来存储最高元素的索引,即如果值为 3.4,我还想将该值的索引存储在数组中。谁能指出我存储此索引值的基本方法。
int const MAXVALUES = 10;
int counter = 0;
float floatPointNumber[MAXVALUES] = { 1.3, 9.2, 2.2, 4.4, 2.5, 2.7, 9.2, 7.9, 9.2, 9.6 };
int index = -1;
float highestArrayVal = 0;
while (counter < MAXVALUES)
{
cout << floatPointNumber[counter] << endl;
while (floatPointNumber[counter]>highestArrayVal)
{
highestArrayVal = floatPointNumber[counter];
//index = floatPointNumber[counter];
break;
}
counter = counter + 1;
}
cout << "Highest Array Value " << highestArrayVal << endl;
cout << "Index of this element " << index << endl;
return 0;
【问题讨论】:
-
显示您的代码,我们会将这一行添加到您的循环中。 :)
-
如果您还没有这样做,请花点时间阅读the help pages,尤其是名为"What topics can I ask about here?" 和"What types of questions should I avoid asking?" 的部分。也请read about how to ask good questions。您可能还想了解如何创建Minimal, Complete, and Verifiable Example。
-
@user3057164 点击edit 链接。
-
当
floatPointNumber[counter]是最高数字时,您知道哪个变量恰好包含该索引吗?