c++ 2d数组到函数
c++ 2d arrays to funcions
代码应该使用2d数组来获取3只猴子在一周内吃的食物,并找到平均值,一天内吃的最少,一天内吃的最多。最小函数应该输出一只猴子在一天内吃的最少的东西,包括猴子的数量、吃的磅数和天数。最高函数也是一样。但是计算不正确,我不知道我是否做错了函数。当我运行它时,它打印出"第一天吃的最少的量是猴子1是1",然后打印出2、3、4等等。我试着把count放到循环外,但是count没有初始化
#include <iomanip>
#include <iostream>
using namespace std;
//Global Constants
const int NUM_MONKEYS = 3; // 3 rows
const int DAYS = 7; // 7 columns
//Prototypes
void poundsEaten(double[][DAYS],int, int);
void averageEaten(double [][DAYS], int, int);
void least(double [][DAYS], int, int);
void most(double [][DAYS], int, int);
int main()
{
//const int NUM_MONKEYS = 3;
//const int DAYS = 7;
double foodEaten[NUM_MONKEYS][DAYS]; //Array with 3 rows, 7 columns
poundsEaten(foodEaten, NUM_MONKEYS, DAYS);
averageEaten(foodEaten, NUM_MONKEYS, DAYS);
least(foodEaten, NUM_MONKEYS, DAYS);
most(foodEaten, NUM_MONKEYS, DAYS);
system("pause");
return 0;
}
void poundsEaten(double monkey[][DAYS], int rows, int cols)
{
for(int index = 0; index < rows; index++)
{
for(int count = 0; count < cols; count++)
{
cout << "Pounds of food eaten on day " << (count + 1);
cout << " by monkey " << (index + 1) << ": ";
cin >> monkey[index][count];
}
}
}
void averageEaten(double monkey[][DAYS], int rows, int cols)
{
for(int count = 0; count < cols; count++)
{
double total = 0;
double average;
for(int index = 0; index < rows; index++)
{
total += monkey[index][count];
average = total/rows;
}
cout << "The average food eaten on day " << (count + 1)
<<" is " << average << endl;
}
}
void least(double monkey[][DAYS], int rows, int cols)
{
double lowest = monkey[NUM_MONKEYS][DAYS];
for(int index = 0; index < rows; index++)
{
for(int count = 0; count < cols; count++)
{
if(monkey[index][count] > lowest)
lowest = monkey[index][count];
cout << "The lowest amount of food eaten was monkey number
<< " " << (index + 1)
<< " On day " << (count + 1) << " was " << lowest;
}
}
}
void most(double monkey[][DAYS], int rows, int cols)
{
double highest = monkey[NUM_MONKEYS][DAYS];
for(int index = 0; index < rows; index++)
{
for(int count = 0; count < cols; count++)
{
if(monkey[index][count] > highest)
highest = monkey[index][count];
cout << "The highest amount of food eaten was monkey number"
<< (index + 1) << " on day " << (count + 1) << " was "
<< highest;
}
}
}
来自least
函数:
if(monkey[index][count] > lowest)
lowest = monkey[index][count];
在比较中,我确定你指的是<
而不是>
。
您还应该保存索引并在循环之后打印,least
和most
都是如此。
相关文章:
- 第 i 个元素返回 i 的函数数组
- 使用嵌套函数数组是个好主意吗?
- 正确实现类函数数组
- 试图引用已删除函数数组的相等运算符
- 如何在静态函数中使用成员函数数组
- 无法创建函数数组
- 构造函数数组
- 使用开关或定义函数数组 - 更快
- 编写一个函数,该函数将覆盖主函数数组中的值
- 可以像'funs[1]();'一样调用的函数数组
- std::AVX内部函数数组
- 如果数组大小未知,如何将函数数组作为参数传递
- 从函数数组中获取值
- 使用元编程在编译时初始化函数数组
- 将C函数数组转换为C++函数指针数组
- 复制构造函数 [数组] C++
- 添加函数数组并随机选择其中一个
- 指针到私有成员函数数组
- 可以单独声明函数数组的元素
- (c++)将main中的随机值保存到函数数组中