"Monkey Business" C++程序 - 完成 99%
"Monkey Business" C++ program - 99% done
我正在尝试用一本旧课本自学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
语句,以便它打印它所查看的所有数据,您会发现它在计算中实际上包含了太多或太少的数据。
提示:错误是您的一个循环是不必要的;你为几只猴子重复某件事,而你只应该为一只特定的猴子重复
相关文章:
- 模板参数替换失败,并且未完成隐式转换
- 用C++在单独的头文件中完成函数体
- 等待整个 omp 块完成,然后再调用第二个函数
- 不必要的C++代码最终会出现在我完成的程序中吗?
- C++一个线程如何正确通信其任务已完成?
- 如何使用libav编写.mov文件,如果文件未正确完成,则可以播放
- 修复完成输入后重复"enter names of teacher"?
- async_write完成处理程序最早何时完成?
- 而不是那么多的 if 语句,我想要一个逻辑,我可以用一个语句或优化的方式来完成
- 对于完成布尔值设置为 true 后未停止的循环
- 如何完成内存分配
- 如何根据C++在同一内存位置重新初始化 C# 中的对象(还是自动完成)?
- Bash 自动完成一个选项,而不运行应用程序
- 选择排序时交换函数调用的数量和完成的交换次数是否相同?
- Qt5 - 如何将"QList<T> *"指针作为信号参数传递,并在完成后将其删除?
- C++ IDE 不会推断/自动完成对模板类中的 std::array 下标表达式的成员访问
- 链接器输入文件未使用,因为链接未完成,我无法获得.o
- CLion、MinGW 和 SDL2:进程已完成,退出代码为 -1073741515 (0xC0000135)
- 从popen()读取的输出是否在pclose()之前完成
- 如何延迟生成已完成的项目序列并对其进行迭代