簡體   English   中英

查找二維數組中的最大和最小數字不起作用

[英]Finding the highest and smallest number in a 2D array not working

所以我一直在尋找解決方案,但還沒有。 此外,大多數程序工作正常只是在計算最大數和最小數時,它顯示錯誤的日期、食物量和錯誤的 monkeyNum。

這是錯誤的示例:錯誤的 Output

雖然平均值是對的。 最大和最小的數字是完全錯誤的。 2 號猴子在第 4 天沒有吃掉 3 磅,這也不是那只猴子的最低量,也不是整個猴子家族的最低量。 在這種情況下,最低金額應為 0.01,但事實並非如此。 3 號猴子的最大值是正確的,但這不是二維數組中的最大值,最大值應該是 98,monkey #2 day #2

這是代碼中可能有誤的部分:

double MIN_Food(double MINf[][DAYS], int monkey, int &subscriptD, int &subscriptM) // Calculate the monkey the consumed the least amount of food
{
    int row, col;
    double min{};

    for (row = 0; row < monkey; row++)
    {
        min = MINf[0][0];
        for (col = 0; col < DAYS; col++)
        {
            if (MINf[row][col] < min)
            {
                min = MINf[row][col];
                subscriptD = col;           // Store day number
                subscriptM = row;           // Store monkey number
            }
        }
    }
    return min;
}
double MAX_Food(double MAXf[][DAYS], int monkey, int &subscriptD, int &subscriptM) // Calculate the monkey that consumed the most amount of food
{
    int row, col;
    double max{};

    for (row = 0; row < monkey; row++)
    {
        max = MAXf[0][0];
        for (col = 0; col < DAYS; col++)
        {
            if (MAXf[row][col] > max)
            {
                max = MAXf[row][col];
                subscriptD = col;
                subscriptM = row;
            }
        }
    }
    return max;
}

這是整個代碼:

#include <iostream>
using namespace std;
// Declare global variables
const int DAYS = 5;     // Columns
const int MONKEY = 3;   // Rows
// Prototype functions
void DayAvg(double[][DAYS], int);
double MIN_Food(double[][DAYS], int, int&, int&);
double MAX_Food(double[][DAYS], int, int&, int&);

int main()
{
    double Monkey_Consumption[MONKEY][DAYS];    // 2D Array to hold the data entered by the user.
    double leastF, mostF;                       // Hold the lowest and highest value within 2D array
    int Subscript_Day = 0,                      // Holds day of consumption
        Subscript_Monkey = 0;                   // Holds monkey who consumed
    DayAvg(Monkey_Consumption, MONKEY);

    leastF = MIN_Food(Monkey_Consumption, MONKEY, Subscript_Day, Subscript_Monkey); // Call the function the calculates de least amount of food consumed.

    cout << "Monkey number ";                   // Note: Had to format the cout statements this way because it wasn't displaying the right results the other way; probably IDE bug.
    cout << (Subscript_Monkey + 1); 
    cout << " ate the least amount of food,\n" << leastF;
    cout << " pounds, on day " << (Subscript_Day + 1) << endl;

    mostF = MAX_Food(Monkey_Consumption, MONKEY, Subscript_Day, Subscript_Monkey); // Call the function the calculates de higher amount of food consumed.
    cout << "Monkey number ";                   
    cout << (Subscript_Monkey + 1);
    cout << " ate the most amount of food,\n" << mostF;
    cout << " pounds, on day " << (Subscript_Day + 1);
}

void DayAvg(double DailyF[][DAYS], int monkey)  // Calculate avg amount of pounds consumed everyday
{
    for (int Monkey_Num = 0; Monkey_Num < MONKEY; Monkey_Num++) // Loop to update rows
    {
        for (int Day_Num = 0; Day_Num < DAYS; Day_Num++)        // Loop to update columns 
        {
            cout << "Enter the pounds eaten by monkey number " << (Monkey_Num + 1) << "\non day " << (Day_Num + 1);
            cout << ": ";
            cin >> DailyF[Monkey_Num][Day_Num];                 // Get data from user
        }
    }

    for (int col = 0; col < DAYS; col++)
    {
        double total = 0; // Variable to hold the sum of a single column
        for (int row = 0; row < MONKEY; row++) // Loop to sum the elements of each column
        {
            total += DailyF[row][col];
        }
        double average = total / MONKEY; // Calculate the average

        //Display results 
        cout << "The average amount eaten on day " << (col + 1) << " is " << average << " pounds.\n";
    }
}
double MIN_Food(double MINf[][DAYS], int monkey, int &subscriptD, int &subscriptM) // Calculate the monkey the consumed the least amount of food
{
    int row, col;
    double min{};

    for (row = 0; row < monkey; row++)
    {
        min = MINf[0][0];
        for (col = 0; col < DAYS; col++)
        {
            if (MINf[row][col] < min)
            {
                min = MINf[row][col];
                subscriptD = col;           // Store day number
                subscriptM = row;           // Store monkey number
            }
        }
    }
    return min;
}
double MAX_Food(double MAXf[][DAYS], int monkey, int &subscriptD, int &subscriptM) // Calculate the monkey that consumed the most amount of food
{
    int row, col;
    double max{};

    for (row = 0; row < monkey; row++)
    {
        max = MAXf[0][0];
        for (col = 0; col < DAYS; col++)
        {
            if (MAXf[row][col] > max)
            {
                max = MAXf[row][col];
                subscriptD = col;
                subscriptM = row;
            }
        }
    }
    return max;
}

您正在此循環中將max重置為 2D 數組第一個元素:

for (row = 0; row < monkey; row++)
{
    max = MAXf[0][0];  // why for every row?
    // ...

如果這樣做,您將丟失前幾行的max信息。 把它排除在循環之外,你應該沒問題。

你也需要為你的min function 做同樣的事情。

另外,我建議從這些函數中返回一個pair<int,int>來告訴你最小或最大元素在哪里 這樣您就可以避免將 2 個輸出參數傳遞給這些函數。

這也是我的問題,我所做的是將max= MAXF[0][0]更改為max= MAXF[row][col]

它對我有用,希望對您有所幫助:>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM