【问题标题】:How can I find the highest and lowest values in this array?我怎样才能找到这个数组中的最高值和最低值?
【发布时间】:2014-11-19 02:21:16
【问题描述】:

我可以使用什么代码打印出用户在此数组中输入的最高值和最低值?这个程序需要取用户输入的平均值(我已经做过)。现在我需要做的就是让程序打印出用户输入的最高和最低值。

#include <iostream>
using namespace std;
int main()
{
float days = 0;
float temperatures [50];
float temptotal = 0;
float average = 0; 
cout << "Enter the number of days: ";
cin >> days;
if (days > 50)
{
    cout << "You may only enter temperatures for 50 days." << endl;
    return 0;
}
for (int i = 1; i <= days; i++)
{
    cout << "Enter the temperature for day number " << i << ": "; 
    cin >> temperatures[50];
    temptotal += temperatures[50]; 
}
average = (temptotal / days);
cout << "The temperature average is: " << average << endl;
return 0;
}

【问题讨论】:

  • 那么,如果你被要求从 50 个数字中找出最大或最小的数字,你会怎么做?

标签: c++ arrays input


【解决方案1】:

有很多方法可以做到这一点。但是一个简单的方法是检查添加到数组中的当前值是否是最低/最高的。 首先,你需要假设最小值对应一个非常大的数字,而最大值对应一个非常小的数字。

float min = 9999999999999;
float max = -9999999999999;

之后,您可以比较该值是低于最小值还是高于最大值。由于初始值与应有的相反,因此将第一个值设置为最小值和最大值,然后将其他值与该值进行比较。通过这样做,您将只保留最低和最高值。

if(temperatures[i] > max)
    max = temperatures[i];
if(temperatures[i] < min)
    min = temperatures[i];

如果你注意到了,我使用了temparatures[i]。如果您总是在索引 50 处添加用户输入的值,那么您总是会覆盖最后输入的值。 此外,数组的索引通常从 0 开始而不是 1,因此您的 for 循环将如下所示:for (int i = 0; i &lt; days; i++)

我编辑了你的代码,这就是我最终得到的结果:

#include <iostream>
using namespace std;
int main()
{
    float days = 0;
    float temperatures [50];
    float temptotal = 0;
    float average = 0;
    cout << "Enter the number of days: ";
    cin >> days;
    if (days > 50)
    {
        cout << "You may only enter temperatures for 50 days max." << endl;
        return 0;
    }
    float min = 9999999999999;
    float max = -9999999999999;
    for (int i = 0; i < days; i++)
    {
        cout << "Enter the temperature for day number " << i << ": ";
        cin >> temperatures[i];
        temptotal += temperatures[i];

        if(temperatures[i] > max)
            max = temperatures[i];
        if(temperatures[i] < min)
            min = temperatures[i];
    }

    average = (temptotal / days);
    cout << "The temperature average is: " << average << endl;
    cout << "The highest temperature is: " << max << endl;
    cout << "The lowest temperature is: " << min << endl;


    return 0;
}

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    你不会只保留一个带有最大值和最小值的局部变量吗?您正在浏览所有输入的项目吗?
    `

    float max=0;
    float min=999;
    for (int i = 1; i <= days; i++)
    {
        cout << "Enter the temperature for day number " << i << ": "; 
        cin >> temperatures[50];
        temptotal += temperatures[50]; 
        if temperatures[50] > max
            max = temperatures[50];
        if temperatures[50] < min
            min = temperatures[50];
    }
    

    请原谅我的编码,因为这不是我的语言。我也不完全明白你的代码。也许您并不是要一直使用温度[50]。也许你的意思是温度[i]?

    祝你好运。

    【讨论】:

      【解决方案3】:

      请看这里:C++ function to find max value in an array of doubles? 本质上,您所做的是:

      1) 添加

          #include <algorithm>
      

      在代码的顶部

      2) 在您的代码中使用std::max_elementstd::min_element,例如像这样

          double highest = *std::max_element(temperatures, temperatures + days);
          double lowest = *std::min_element(temperatures, temperatures + days);
      

      您可能想在此处查找这些方法: http://www.cplusplus.com/reference/algorithm/max_element/http://www.cplusplus.com/reference/algorithm/min_element/ (尽管请注意,许多人看不起 cplusplus.com,认为它不是 C++ 的最佳信息来源,但我想在这种情况下它很好)

      或者,作为 Eugene Sh。指出并且 Brett 表明,您可以通过存储当前最高(最低)值并将其与每个新输入值进行比较来自己完成。

      另外,正如 Brett 所注意到的,您希望在阅读循环中将 temperatures[50] 更改为 temperatures[i]。但是还有另一个更正:这个循环应该从0 运行到小于days

      for (int i=0; i < days; i++)
      

      因为数组是从 0 开始索引的,并且在值的最大数量 (50) 的情况下,您将填充数组的元素从 049(即 days-1)包含在内,而不是从 150

      【讨论】:

      • 您也可以通过一次调用std::minmax_element 来获取这两个值。
      • @Blastfurnace:谢谢,不知道!我希望我能投票支持你的评论:(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      相关资源
      最近更新 更多