C++ 如何在C+中获得二维数组中一列的最低数目+;?

C++ 如何在C+中获得二维数组中一列的最低数目+;?,c++,arrays,c++11,multidimensional-array,c++17,C++,Arrays,C++11,Multidimensional Array,C++17,我有一个简单的程序可以从用户那里得到最低和最高温度,然后我必须让程序得到当天最低和最高温度的平均值。 我应该把7天,低,高放在一个二维数组temp`temp[7][2]={0};中;。 所以我做了这个程序,它运行得很好,这就是代码 //declare arr & varaibles int temp[7][2]={0}; // temperature array double totalHigh=0,totalLow=0; //total low temp d

我有一个简单的程序可以从用户那里得到最低和最高温度,然后我必须让程序得到当天最低和最高温度的平均值。 我应该把7天,低,高放在一个二维数组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;
//声明arr&varaibles
int temp[7][2]={0};//温度阵列
双倍totalHigh=0,totalLow=0//低温总天数和高温总天数
int high=temp[0][0],low=temp[0][0];//分配数组中的第一行和第一列,以获取最高和最低数字

对于(int row=0;row,这里的问题是init
low
0

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

类似地,如果最高低点低于0,您也会注意到
高点
也不能正常工作


一种解决方法是,在用户已经输入了所有数据之后,您只需初始化
high
low
。而初始化
high=temp[0][0],low=temp[0][1]

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

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];
auto low=temp[0][1];
对于(int j=1;j temp[j][1])
低温=温度[j][1];
它应该正常工作。我认为low的起始值太低,这就是为什么找不到最高温度的最低值

int high=INT_MIN,low=INT_MAX;

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

由于使用了
c++17
标记,我建议使用更好的现代(STLish)解决方案:

#包括
#包括
#包括
#包括
#包括
#包括
int main(){
病媒温度(7);
对于(int i=0;istd::你能分享一个示例输入、你得到的输出和预期的输出吗?你看过标准库中的工具了吗?我觉得我宁愿使用2D结构而不是
pair
来摆脱lambdas,但我不知道这是否仍然是
c++17
方法anymore@Ranoiaetep是什么“二维结构"?如果你指的是一个有两个成员的结构,那么即使你也必须使用lambda。避免lambda的一种方法是使用两个向量,比如说
低温
高温
。我指的是嵌套向量,比如
std::vector
std::array
@Ranoiaetep是的,如果你愿意,你可以这样做。我试图强调
s的使用td::accumulate
std::minmax_元素
。为什么要编写已经在标准库中实现的代码呢!
int high=INT_MIN,low=INT_MAX;