如何在C++中获取二维数组中最少的一列数?

How to get the lowest number of one column in a two dimensional array in C++?

本文关键字:一列 C++ 获取 二维数组      更新时间:2023-10-16

我有一个简单的程序从用户那里得到最低和最高温度,然后我必须让程序得到当天最低和最高温度的平均值。 我应该将 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;

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

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];
}
}

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

这里的问题是你有初始化low0

首次初始化 2d 数组时,所有值都设置为 0。然后你有初始化lowtemp[0][0],这意味着low现在0。除非您的最低高点实际上低于 0,否则它永远不会更新。

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

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

由于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;
}

我认为问题是你的低变量初始化。

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];

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

int high=INT_MIN,low=INT_MAX;

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