如何找到每一行的最大值和最小值

我的练习任务是,如果最小值大于任何其他数组的最大值,则查找数组的索引。如果有多个,则仅打印最低的索引。例如:
1 2 3 4 5
6 7 8 9 10
因此输出为2,因为第二行的最小值高于另一行的最大值。但是我一直在寻找 每个 数组的最小值和最大值,因此我无法继续前进。

int numberOfTowns;
int numberOfDays;
cin >> numberOfTowns >> numberOfDays;
int temperature[100][100];
for (int i = 0; i < numberOfTowns; i++)
{
    int maxValue = temperature[i][0];
    int minValue = temperature[i][0];
    for (int j = 0; j < numberOfDays; j++)
    {
        cin >> temperature[i][j];

        if (temperature[i][j] > maxValue)
            maxValue = temperature[i][j];

        if (temperature[i][j] < minValue)
            minValue = temperature[i][j];

    }
        cout << "Max: " << maxValue << endl;
        cout << "Min: " << minValue << endl;
}
return 0;}

编辑:为澄清起见,numberOfTowns基本上表示行数,numberOfDays表示列数。
我的输入和输出看起来像这样:
(3是行数,5是列数)

3 5

10 15 12 10 10

最大值:15 最小值:0

11 11 11 11 20

最大值:20 最小值:0

18 16 16 16 20

最大值:20 最小值:0

所以我的Max正常工作,但是Min始终为0。有人可以帮忙吗? 附注:这是我的第一个问题,我对C ++有点陌生,对不起,如果我做错了什么。

tongzhi122015528 回答:如何找到每一行的最大值和最小值

在将任何内容写入该位置之前,请先用<style>初始化minValuemaxValue。而是使用输入值对其进行初始化:

temperature[i][0]
,

minValue从0开始。 应该从第一个值开始。

int numberOfTowns;
int numberOfDays;
cin >> numberOfTowns >> numberOfDays;
int temperature[100][100];
for (int i = 0; i < numberOfTowns; i++)
{
    int maxValue;
    int minValue;
    for (int j = 0; j < numberOfDays; j++)
    {
        cin >> temperature[i][j];

        if( j == 0 ) {
             maxValue = temperature[i][0];
             minValue = temperature[i][0];
             continue;
        }

        if (temperature[i][j] > maxValue)
            maxValue = temperature[i][j];

        if (temperature[i][j] < minValue)
            minValue = temperature[i][j];

    }
        cout << "Max: " << maxValue << endl;
        cout << "Min: " << minValue << endl;
}
return 0;}
本文链接:https://www.f2er.com/3044419.html

大家都在问