"Monkey Business" C++程序 - 完成 99%

"Monkey Business" C++ program - 99% done

本文关键字:完成 C++ Monkey Business 程序      更新时间:2023-10-16

我正在尝试用一本旧课本自学C++,非常感谢您的意见。我可以在网上找到该程序的工作代码,但在尝试完全不同的方法之前,我想让我的代码正常工作。

我需要编写一个程序,使用3x7二维数组来保存3只猴子一周内的每日食物消耗量。

我需要输出平均每日总食物消耗量,任何猴子每周最少的食物消耗量和任何猴子每周最多的食物消耗。

到目前为止,一切都正常,除了我的getLeast函数,它的作用听起来像是——在猴子中获得最少的每周食物消耗。然而,它输出零,而我的getMost函数似乎工作得很好。

此外,我欢迎任何可以就如何改进或精简代码提供建议的评论。感谢阅读!

这是我的代码:

#include<iostream>
using namespace std;
const int numROWS = 3;
const int numCOLS = 7;
void getData(int array[][numCOLS], int);
double getAverage(int array[][numCOLS], int);
int getRowSum(int array[][numCOLS], int);
double getAverage(int array[][numCOLS], int);
int getLeast(int, int, int);
int getMost(int, int, int);
int main()
{
    int monkeys[numROWS][numCOLS];
    int monkey1 = 0, monkey2 = 1, monkey3 = 2, monk1Tot, monk2Tot, monk3Tot, most, least;
    getData(monkeys,numROWS);
    monk1Tot = getRowSum(monkeys, monkey1);
    monk2Tot = getRowSum(monkeys, monkey2);
    monk3Tot = getRowSum(monkeys, monkey3);
    least = getLeast(monk1Tot, monk2Tot, monk3Tot);
    most = getMost(monk1Tot, monk2Tot, monk3Tot);
    cout << "The average daily food consumption by the monkeys was " << getAverage(monkeys, numROWS) << ". n";
    cout << "The least amount of food consumed within the week by a single monkey was " <<least << ". n";
    cout << "The greatest amount of food consumed within the week by a single monkey was " << most << ". n";
}
void getData(int monkeys[][numCOLS],int numROWS)
{
    for (int rows = 0; rows < numROWS; rows++)
        {
        cout << "Monkey " << (rows + 1) << "n";
        for (int cols = 0; cols < numCOLS; cols++)
            {
            cout << " Day " << (cols + 1) << ": ";
            cin >> monkeys[rows][cols];
            while (monkeys[rows][cols] < 0)
                {
                cout << "ERROR: Please enter a positive number: ";
                cin >> monkeys[rows][cols];
                }
            }
        cout << endl;
        }
}
int getRowSum(int monkeys[][numCOLS], int monkeyNum)
{
    int total = 0;
    for (int rows = 0; rows < monkeyNum; rows++)
    {
        for (int cols = 0; cols < numCOLS;cols++)
            total += monkeys[rows][cols];
    }
    return total;   
}
double getAverage(int monkeys[][numCOLS], int numROWS)
{
    double total = 0;
    for (int cols = 0; cols < numCOLS; cols++)
    {
        for (int rows = 0; rows < numROWS; rows++)
            total += monkeys[rows][cols];
    }
    return (total/(numCOLS));
}
int getMost(int monkey1, int monkey2, int monkey3)
{
    int array[3]{monkey1, monkey2, monkey3};
    int max = array[0];
    for (int count = 0; count < 3; count++)
    {
        if (array[count] > max)
        {
            max = array[count];
        }
    }
        return max;
}
int getLeast(int monkey1, int monkey2, int monkey3)
{
    int array[3]{monkey1, monkey2, monkey3};
    int least = array[0];
    for (int count = 0; count < 3; count++)
    {
        if (array[count] < least)
        {
            least = array[count];
        }
    }
        return least;
}

您的一个函数中有一个错误,但它不在getLeast()中。我不会告诉你它在哪里,而是告诉你如何自己找到它。从看似行为不端的函数(即getLeast())开始,检查它是否真的收到了您期望的参数——在循环中,使用cout打印array[count]。然后,您将看到getLeast()实际上在给定其接收到的参数的情况下返回了正确的答案通过手工计算找出哪些参数是错误的(可能不止一个)。哪个函数负责计算该参数?向该函数添加一些cout语句,以便它打印它所查看的所有数据,您会发现它在计算中实际上包含了太多或太少的数据。

提示:错误是您的一个循环是不必要的;你为几只猴子重复某件事,而你只应该为一只特定的猴子重复