【问题标题】:How to get the lowest number of one column in a two dimensional array in C++?如何在 C++ 中获取二维数组中一列的最小数量?
【发布时间】:2020-10-20 11:54:06
【问题描述】:

我有一个简单的程序可以从用户那里获得最低和最高温度,然后我必须让程序获得当天最低和最高温度的平均值。 我应该把 7days,low,high 放在一个名为 temp `temp[7][2]={0}; 的二维数组中。 所以我做了这个程序,它运行良好,这就是代码;

    //declare arr & varaibles 
    int temp[7][2]={0}; // temperature array 
    double totalHigh=0,totalLow=0; //total low temp days & total high temp days
    int high=temp[0][0],low=temp[0][0];// assign the first row & column in array to get the highest & lowest number
    
    for(int row=0;row<7;row+=1){ // for loop to get through every row 
        cout << "Day " << row+1 << endl; 
        cout << "Low: "; cin >> temp[row][0]; // first column is for low temp days
        totalLow += temp[row][0] ; // get the total low days from the user
        cout << "High: "; cin >> temp[row][1]; // second column is for high temp days
        totalHigh += temp[row][1]; // get the total high days from the user
    }

    for(int j=0;j<7;j+=1){ // go through first column to get the high number of it
        if(high < temp[j][0]){
            high = temp[j][0];
        }
    }

    for(int j=0;j<7;j+=1){ // go though second column to get the low number of it
        if(low > temp[j][1]){
            low = temp[j][1];
        }
    }

    //display on screen 
    cout << fixed<< setprecision(2);
    cout << "Low: "<< totalLow/7 << endl; //get the average of low
    cout << "High: " <<totalHigh/7 << endl;// get the average of high
    cout << "Lowest Of High column: " << low << endl;
    cout << "Highst Of Low column: " << high << endl;

但我也应该得到最高数量的低列和最低数量的高列。 我得到了最低数量的高列,但是当我进行循环以获得最低数量时它不起作用。 这是遍历每一列并获取它们的两个循环的代码;

    for(int j=0;j<7;j+=1){ // go through first column to get the high number of it
        if(high < temp[j][0]){
            high = temp[j][0];
        }
    }

    for(int j=0;j<7;j+=1){ // go though second column to get the low number of it
        if(low > temp[j][1]){
            low = temp[j][1];
        }
    }

但是第二个循环不工作,而第一个循环工作,谁能告诉我为什么第二个循环不工作?

【问题讨论】:

  • 你能分享一个示例输入、你得到的输出和预期的输出吗?
  • 您是否查看过标准algorithm 库中的工具,例如std::minmax_element

标签: c++ arrays c++11 multidimensional-array c++17


【解决方案1】:

这里的问题是您将low 初始化为0

当您第一次初始化二维数组时,所有值都设置为 0。然后您将 low 初始化为 temp[0][0],这意味着 low 现在是 0。除非你的最低价实际上低于 0,否则它永远不会被更新。

同样,如果您的最高低点低于 0,您也会注意到 high 也无法正常工作。

您可以修复它的一种方法是,您只在用户输入所有数据后才初始化highlow。并初始化high = temp[0][0], low = temp[0][1]

【讨论】:

    【解决方案2】:

    我认为问题在于您的低变量初始化。

    low=temp[0][0];
    

    你可以这样修改这个循环:

    auto low = temp[0][1];
    for(int j=1;j<7;j+=1) // go though second column to get the low number of it
        if(low > temp[j][1])
            low = temp[j][1];
    

    它应该可以正常工作。我认为low的起始值太低,这就是为什么无法找到最高温度的最低值的原因。

    【讨论】:

      【解决方案3】:
      int high=INT_MIN,low=INT_MAX;
      

      这将为您提供最高列的最低值和最低列的最高值。

      【讨论】:

        【解决方案4】:

        由于c++17 标签,我建议这是更好和现代(STLish)的解决方案:

        #include <algorithm>
        #include <iomanip>
        #include <iostream>
        #include <numeric>
        #include <utility>
        #include <vector>
        
        int main() {
        
            std::vector<std::pair<int, int>> temp(7);
            
            for (int i = 0; i < temp.size(); ++i) {
                std::cout << "Day " << i + 1 << std::endl;
                std::cout << "Low: ";
                std::cin >> temp.at(i).first;
                std::cout << "High: ";
                std::cin >> temp.at(i).second;
            }
            
            std::cout << std::fixed << std::setprecision(2);
            
            std::cout << "Average Low: " << (std::accumulate(temp.begin(), temp.end(), 0.0F, [](auto &a, auto &b) { return a + b.first; })) / temp.size() << std::endl;
            std::cout << "Average High: " << (std::accumulate(temp.begin(), temp.end(), 0.0F, [](auto &a, auto &b) { return a + b.second; })) / temp.size() << std::endl;
            
            auto [low_low, high_low]   = std::minmax_element(temp.begin(), temp.end(), [](auto &a, auto &b) { return a.first < b.first; });
            auto [low_high, high_high] = std::minmax_element(temp.begin(), temp.end(), [](auto &a, auto &b) { return a.second < b.second; });
            
            std::cout << "Lowest low temperature: " << (*low_low).first << std::endl;
            std::cout << "Highest low temperature: " << (*high_low).first << std::endl;
            std::cout << "Lowest high temperature: " << (*low_high).second << std::endl;
            std::cout << "Highest high temperature: " << (*high_high).second << std::endl;
            
            return 0;
        }
        

        【讨论】:

        • 我觉得我宁愿使用 2D 结构而不是 pair 来摆脱 lambda,但我不知道这是否仍然是 c++17 方法了
        • @Ranoiaetep 什么是“二维结构”?如果你的意思是一个有两个成员的结构,那么即使你也必须使用 lambdas。避免 lambda 的一种方法是使用两个向量,例如 low_temphigh_temp
        • 我的意思是嵌套向量,例如std::vector&lt;std::vector&lt;int&gt;&gt;std::array&lt;std::array&lt;int, 7&gt;, 2&gt;
        • @Ranoiaetep 是的,如果你愿意,你可以这样做。我试图强调std::accumulatestd::minmax_element 的使用。为什么要编写已经在标准库中实现的代码!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-14
        • 2020-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-09
        相关资源
        最近更新 更多