“Monkey Business” C ++程序 - 99%完成 [英] "Monkey Business" C++ program - 99% done

查看:146
本文介绍了“Monkey Business” C ++程序 - 99%完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用自己的教科书教导自己的C ++,并感谢您的输入。我可以在网上找到程序的工作代码,但我想让我的代码工作,然后尝试完全不同的方法。

I'm trying to teach myself C++ using an old textbook and would appreciate your input. I can find working code for the program online, but I want to make my code work before trying completely different approaches.

我需要编写一个程序,使用3x7两三维数组,每周由3只猴子保持每日食物消耗。

I need to write a program that uses a 3x7 two-dimensional array to hold daily food consumption throughout a week by 3 monkeys.

我需要输出平均每日食物消费量,任何猴子的每周食物消耗量,以及任何猴子最大的每周食物消耗量。

I need to output average total daily food consumption, the least amount of weekly food consumption by any monkey, and the greatest weekly food consumption by any monkey.

一切工作到目前为止,除了我的getLeast功能,这听起来像 - 获得最少的每周食物消费在猴子中。但是,它输出零,而我的getMost函数似乎工作正常。

Everything works so far, except for my getLeast function, which does what it sounds like - getting the least weekly food consumption among the monkeys. However, it outputs zero, while my getMost function seems to work fine.

此外,我欢迎任何有关如何改进或简化我的代码的意见。感谢您阅读!

Additionally, I welcome any comments that can offer advice on how I can improve or streamline my code. Thanks for reading!

以下是我的代码:

#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 语句,以便它打印其所查看的所有数据,并且您将看到它在其计算中包含太多或太少的数据。

There is a bug in one of your functions, but it's not in getLeast(). Rather than telling you where it is, I'll tell you how you can find it by yourself. Start out in the function that seemingly misbehaves, namely getLeast(), and check if it actually receives the parameters you expect - inside the loop, use cout to print array[count]. Then, you'll see that getLeast() actually returns the right answer given the parameters it received! Figure out by hand calculation which parameters are wrong (it could be more than one). Which function is responsible for computing that/those parameters? Add some cout statements to that function so that it prints all the data it looks at, and you'll see that it actually includes too much or too little data in its calculation.

提示:错误是您的一个循环是不必要的;你重复几个猴子的东西,你只能为一个特定的猴子做它。

这篇关于“Monkey Business” C ++程序 - 99%完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆