如何在C++中获取二维数组中最少的一列数?
How to get the lowest number of one column in a two dimensional array in C++?
我有一个简单的程序从用户那里得到最低和最高温度,然后我必须让程序得到当天最低和最高温度的平均值。 我应该将 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];
}
}
但是第二个循环在第一个循环工作时不起作用,谁能告诉我为什么第二个循环不工作?
这里的问题是你有初始化low
0
。
首次初始化 2d 数组时,所有值都设置为 0。然后你有初始化low
temp[0][0]
,这意味着low
现在0
。除非您的最低高点实际上低于 0,否则它永远不会更新。
同样,如果最高低点低于 0,您还会注意到high
也无法正常工作。
修复它的一种方法是仅在用户输入所有数据后初始化high
和low
。并初始化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;
这将为您提供高列中的最低值和低列中的最高值。
相关文章:
- 查找矩阵C++中每一列和每一行的最小和最大元素
- 如何在C++中获取二维数组中最少的一列数?
- 如何允许用户选择何时停止三列中的每一列的旋转
- 从我的2d数组中的一列返回的字符串值被压缩为一个字符串(在Mac os上打开Windows txt文件)
- 在犰狳立方体中添加一列 1 的有效方法
- 在二维向量的每一列中查找最大值
- 只有一列适合其内容大小,而另一列则拉伸
- 如何将单独的数组分组在一起(类似于按一列对表进行排序)
- C++ 最后一列中的直方图错误
- 添加两个矩阵会打印一列垃圾数据 c++
- C 特征:通过参考将矩阵的一列传递到函数中
- 仅制作一列qtreewidget可编辑//故障排除
- 在 QTableView 中仅对一列进行排序,而不对其他列进行排序.独立列
- 按列对CSV文件进行排序,并与C 中的另一列进行比较
- 隔离C 中向量的一列
- 读取数据文件并将每一列分配给各个数组
- 按一列对二维数组进行气泡排序
- 可能的话,将QComboBox映射到MySQL表的一列
- 数据排序依据是哪一列,QTableWidget
- 每隔一行或每一列提取一个特征矩阵作为一个新矩阵