首页 > 解决方案 > 按从最小值到最大值的顺序显示二维数组中的值及其索引?

问题描述

如果我输入一个(大)数字,然后输入一个较小的数字,则首先显示较大的数字。有没有办法显示这两个值,每个最小值和最大值一次,按照最小值和最大值的顺序显示它们在数组中的索引?例如“2 号猴子吃的食物最少,第 2 天 3 只。1 号猴子吃的食物最多,第 5 天 10 只。

我不知所措......我已经重写了很多次,但结果比我下面的更糟糕。

//Find the greatest amount of food eaten, and the least amount of food eaten by a monkey.

#include <iostream>
#include <iomanip>
#include <limits>
constexpr auto COLUMN = 5;
constexpr auto ROW = 3;


///////////////////////////////////////////////////PROTOTYPE
void foodInput(double monkeyArray[ROW][COLUMN], double userInput);
void foodCalculator(double monkeyArray[ROW][COLUMN]);
void minMaxFoodEatenFinder(double monkeyArray[ROW][COLUMN]);



//////////////////////////////////////////////////PROGRAM START
int main()
{
    //VARIABLES GO HERE
    double monkeyArray[ROW][COLUMN];
    double userInput = 0.0;

    //START FUNCTION HERE
    foodInput(monkeyArray, userInput);
    foodCalculator(monkeyArray);
    minMaxFoodEatenFinder(monkeyArray);

    return 0;
}

//////////////////////////////////////////////FUNCTIONS GO HERE
void foodInput(double monkeyArray[ROW][COLUMN], double userInput) //Handles input into multidimensional array
{

    for (int monkeyNumber = 0; monkeyNumber < ROW; monkeyNumber++)//monkey loop executes
    {
        for (int day = 0; day < COLUMN; day++)//days of the week loop executes 5 times
        {
            std::cout << "Enter the pounds eaten by monkey number " << monkeyNumber + 1 << "\non day " << day + 1 << ": ";
            while (true)
            {
                std::cin >> userInput;
                if (std::cin.fail() || userInput <= 0)
                {
                    std::cin.clear();
                    std::cin.ignore(256, '\n');
                    std::cout << "Enter a non-negative amount: " << std::endl;
                }
                else
                {
                    monkeyArray[monkeyNumber][day] = userInput;
                    break;
                }
            }
        }
    }
}


void foodCalculator(double monkeyArray[ROW][COLUMN])//Averages the amount of food eaten per day
{


    for (int day = 0; day < COLUMN; day++)//for loop to average out the columns
    {
        double sum = 0.0;
        double average = 0.0;

        for (int monkeyNumber = 0; monkeyNumber < ROW; monkeyNumber++)//what all three monkeys ate on day #__, average it
        {
            sum = sum + monkeyArray[monkeyNumber][day];
        }

        average = (sum / 3.0);
        std::cout << "The average amount eaten on day " << day + 1 << " is " << std::fixed << std::setprecision(3) << average << " pounds." << std::endl;
    }
}

void minMaxFoodEatenFinder(double monkeyArray[ROW][COLUMN])//Find the biggest number and find the smallest number in the array
{
    int minimumNum = monkeyArray[0][0];
    int maximumNum = monkeyArray[0][0];

    for (int monkeyNumber = 0; monkeyNumber < ROW; monkeyNumber++)
    {

        for (int day = 0; day < COLUMN; day++)
        {

            if (monkeyArray[monkeyNumber][day] > maximumNum)//if you're bigger than the max, you're the new max
            {
                maximumNum = monkeyArray[monkeyNumber][day];
            }
            if (monkeyArray[monkeyNumber][day] < minimumNum)//if you're smaller than the min, you're the new min
            {
                minimumNum = monkeyArray[monkeyNumber][day];
            }
        }
    }


    for (int i = 0; i < ROW; i++)//display the monkey number, food amount, and day the food was eaten
    {
        for (int j = 0; j < COLUMN; j++)
        {
            if (monkeyArray[i][j] == minimumNum)
            {
                std::cout << "Monkey number: " << i + 1 << " ate the least amount of food, \n" << minimumNum << " on day " << j + 1 << std::endl;
            }
            if (monkeyArray[i][j] == maximumNum)
            {
                std::cout << "Monkey number: " << i + 1 << " ate the most amount of food, \n" << maximumNum << " on day " << j + 1 << std::endl;
            }
        }
    }
}

标签: c++arraysmultidimensional-array

解决方案


推荐阅读