我试图在2d数组中的行之间找到最大值和最小值——最小值代码似乎是正确的,但不起作用
im trying to find maximun and minimum between the rows in 2d array---- the minimum value code seems correct but not working
#include <iostream>
using namespace std;
int main()
{
int total, average, maximum, mininum;
int a = 0;
int b = 0;
int c = 0;
int x = 0;
int y = 0;
int tab[5][5] = { { 111, 2, 30, 4, 500 }, { 10, 20, 30, 40, 50 },
{34, 54, 78, 9, 23,},
{ 65, 12, 98, 3, 78 }, { 34, 89, 23, 98, 45 } };
string name[5] = { "EMP1:EREN", "EMP2:ARMIN", "EMP3:MIKASA", "EMP4:LEVI", "EMP5:CONNIE" };
for (int c = 0; c < 5; c++) {
for (int a = 0; a < 5; a++) {
//cout<<total<<"n";
cout << tab[c][a] << "n";
3 total = total + tab[c][a];
b = tab[c][a];
for (tab[c][a] = 1; tab[c][a] >= 300; tab[c][a]++);
if (tab[c][a] >= x) {
x = tab[c][a];
}
for (tab[c][a] = 1; tab[c][a] < 5; tab[c][a]++);
if (tab[c][a] < b) {
y = tab[c][a];
}
}
average = total / 5;
cout << "I.D number " << name[c] << " nThe total amount of sale:" << total;
cout << "nThe average sales is:" << average;
cout << "nThe Maximum sales is:" << x;
cout << "nThe Minimum sales is:" << y << "n";
}
}
你的代码大多是c风格的,你的逻辑不是很清楚,我只是用accumulate
、minmax_element
把它变成了现代的cpp风格,我们也可以把二维数组改成vector
或std::array
,留给你。
使用的STL算法:
- 累加求和
- 用于将最小值和最大迭代器放在一起的minmax_element
#include <algorithm>
#include <iostream>
#include <numeric>
using namespace std;
int main() {
constexpr size_t kColomns = 5;
constexpr size_t kRows = 5;
int tab[kRows][kColomns] = {{111, 2, 30, 4, 500},
{10, 20, 30, 40, 50},
{34, 54, 78, 9, 23},
{65, 12, 98, 3, 78},
{34, 89, 23, 98, 45}};
string name[kRows] = {"EMP1:EREN", "EMP2:ARMIN", "EMP3:MIKASA", "EMP4:LEVI",
"EMP5:CONNIE"};
for (int c = 0; c < kRows; c++) {
auto total = std::accumulate(&tab[c][0], &tab[c][kColomns], 0);
auto average = total / kColomns;
auto [min_iter, max_iter] =
std::minmax_element(&tab[c][0], &tab[c][kColomns]);
cout << "I.D number " << name[c] << " nThe total amount of sale:" << total;
cout << "nThe average sales is:" << average;
cout << "nThe Maximum sales is:" << *min_iter;
cout << "nThe Minimum sales is:" << *max_iter << "n";
}
return 0;
}
输出:
I.D number EMP1:EREN
The total amount of sale:647
The average sales is:129
The Maximum sales is:2
The Minimum sales is:500
I.D number EMP2:ARMIN
The total amount of sale:150
The average sales is:30
The Maximum sales is:10
The Minimum sales is:50
I.D number EMP3:MIKASA
The total amount of sale:198
The average sales is:39
The Maximum sales is:9
The Minimum sales is:78
I.D number EMP4:LEVI
The total amount of sale:256
The average sales is:51
The Maximum sales is:3
The Minimum sales is:98
I.D number EMP5:CONNIE
The total amount of sale:289
The average sales is:57
The Maximum sales is:23
The Minimum sales is:98
在线演示
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用递归的数组的最小值.这是怎么回事
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- txt 文件中浮点数的最大和最小值
- 余数运算符的等效操作,用于处理低于允许的最小值
- 需要使用模板查找数组的第二个最小和最小值
- CUDA - 将 float3 数组的 (x,y,z) 分量的最小值/最大值分开?
- 查找包含 N 个元素的数组的最小值和最大值
- 错误C++在Visual Studio 2019中使用numeric_limits的长双精度最小值/最大值
- 是否存在用于 C 或 C++ 中常见数学运算(例如最小值、最大值和平均值)的可导入库?
- 如何在 c++ 中创建对的优先级队列.这会弹出具有最小值的元素.默认的弹出最大值
- 我正在努力在随机数组中查找最小值,有人知道为什么我的代码不起作用?C++
- 正确的代码以打印交换的最大值和最小值
- 为什么我的C 代码无法计算整数的最大和最小值
- 如何简化此代码(在无符号 int 的基础上生成最小值和最大值之间的随机 int)