简体   繁体   English

在For循环数组C ++中找到最大和最小值的值

[英]Finding Value At Which There is the Largest and Smallest Value in a For-loop Array C++

Alright, I have my code (shown below) which basically gets the largest and smallest temperature values for the entire year. 好吧,我有我的代码(如下所示),该代码基本上获得了全年的最大和最小温度值。 Now that works, but where I'm having trouble is coming up with a solution to put out the incremented value "j" when the largest value is found in the array. 现在可以了,但是我遇到麻烦的地方是想出一个解决方案,当在数组中找到最大值时,将增量值“ j”放出来。 I need this because the main goal of the program is to output the month in which the largest and smallest value is found. 我需要这样做是因为该程序的主要目标是输出找到最大值和最小值的月份。 And I already have a function written in this code that will convert the incremented value into a string month. 而且我已经有一个用此代码编写的函数,它将递增的值转换为字符串月份。

TLDR: I need to find out how to know at what value of "j" my program finds the maximum and minimum values. TLDR:我需要找出如何知道我的程序以什么值“ j”找到最大值和最小值。

So, if you guys could offer any insight into how I might accomplish this that would be fantastic! 因此,如果你们能对我如何实现这一目标提供任何见解,那将是太棒了!

     //Gathering Largest Temperature:
    for(int j = 0; j < size; j++)
    {
        if(yearData[j].highTemperature > highestTemperature)
           highestTemperature = yearData[j].highTemperature;
    }

    //Gathering Smallest Temperature:
    for(int j = 0; j < size; j++)
    {
        if(yearData[j].lowTemperature < lowestTemperature)
            lowestTemperature = yearData[j].lowTemperature;
    }
//Gathering Largest Temperature:
auto highestTemperature = yearData[0].highTemperature;
int highestTemperatureDate = 0;
for(int j = 0; j < size; j++)
{
    if(yearData[j].highTemperature > highestTemperature)
    {
       highestTemperature = yearData[j].highTemperature;
       highestTemperatureDate = j;
    }
}

//Gathering Smallest Temperature:
auto lowestTemperature = yearData[0].lowTemperature;
int smallestTemperatureDate = 0;
for (int j = 0; j < size; j++)
{
    if (yearData[j].lowTemperature < lowestTemperature)
    {
        lowestTemperature = yearData[j].lowTemperature;
        smallestTemperatureDate = j;
    }
}

I added an int for the smallest and highest temperatures 我为最小和最高温度添加了一个整数

With std, you might do: 使用std,您可以执行以下操作:

//Gathering Largest Temperature:
auto max = std::max_element(std::begin(yearData), std::end(yearData),
                            [](const auto& lhs, const auto& rhs){
                                return lhs.highTemperature < return rhs.highTemperature;
                            });

auto max_index = std::distance(std::begin(yearData), max);

// Gathering Smallest Temperature:
auto min = std::min_element(std::begin(yearData), std::end(yearData),
                            [](const auto& lhs, const auto& rhs){
                                return lhs.lowTemperature < return rhs.lowTemperature;
                            });
auto min_index = std::distance(std::begin(yearData), min);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM