gpt4 book ai didi

c++ - 二维数组中的值未正确分配

转载 作者:行者123 更新时间:2023-11-28 02:10:20 25 4
gpt4 key购买 nike

我束手无策,我确信这是一个简单的错误。我已经用谷歌搜索了这个并且我已经多次看到这个类似的问题,但是我的代码看起来与我所看到的相似所以我仍然无法弄清楚我做错了什么。

我正在为一个类(class)布置家庭作业,该类(class)要求用户提供一周内 3 只猴子吃掉的食物量,然后将这些条目存储在一个二维数组中。

请原谅我糟糕的变量名,一旦我将其分解为不同的函数,我将更改它们,但我想让它先在 main 中运行。我已经对其进行了测试,我的总和、平均、最少和大多数语句都有效,但由于某种原因,我输入数组的数据正在跳过数字或覆盖数字(我也在下面发布了输出)。

当我运行代码时: #include//用于 cin、cout、endl #包括 #包括 使用命名空间标准;

    const int DAYS_WEEK = 7;
const int MONKEYS = 3;


int main()
// main function
{
// One dimensional array just to prove I could do it. Also it holds the names of the days of the week, for the cout statement below that asks for input
string dayOfWeek[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
//2d array that will store the food eaten by each monkey as it is entered in by the user below
double foodEaten[DAYS_WEEK][MONKEYS];
//value to store the sum of all the food eaten by all monkeys
double total = 0;
//count to keep track of how many times the sum loop runs below, so I can use it as the divisor to find the average
int count = 0;
//value to hold the average once found
double average = 0;
//value to hold the least amount of food eaten
double least = 0;
//value to hold the highest amount of food eaten
double most = 0;

//This nested loop asks for input from the user and should input the values entered into the two dimensional array
for ( int monkey = 0; monkey < MONKEYS; monkey++ )
{
for ( int day = 0; day < DAYS_WEEK; day++ )
{
cout << "Enter pounds of food eaten by monkey "
<< (monkey + 1)
<< " on " << dayOfWeek[day] << ": " ;
cin >> foodEaten[monkey][day];
//This will double check that the user hasn't entered a negative number and if they have throw them back into the loop
while ( foodEaten[monkey][day] < 0 )
{
cout << "Enter a non-negative amount: ";
cin >> foodEaten[monkey][day];
}
}
cout << endl;
}

//This should display the table of how much food was eaten after it is all entered
cout << setw(6) << "Monkey"
<< setw(5) << "Sun"
<< setw(5) << "Mon"
<< setw(5) << "Tue"
<< setw(5) << "Wed"
<< setw(5) << "Thu"
<< setw(5) << "Fri"
<< setw(5) << "Sat" << endl;
for ( int monkeyLord = 0; monkeyLord <= 2; monkeyLord++)
{
cout << setw(6) << (monkeyLord + 1) << setw(5) << foodEaten[monkeyLord][0] << setw(5) << foodEaten[monkeyLord][1] << setw(5) << foodEaten[monkeyLord][2] << setw(5) << foodEaten[monkeyLord][3] << setw(5) << foodEaten[monkeyLord][4] << setw(5) << foodEaten[monkeyLord][5] << setw(5) << foodEaten[monkeyLord][6] << endl;
}

//This should sum all the amounts of food eaten by the monkeys
for ( int monkeyTotal = 0; monkeyTotal <= 2; monkeyTotal ++)
{
for ( int dayTotal = 0; dayTotal <= 6; dayTotal ++)
{
total = total + foodEaten[monkeyTotal][dayTotal];
count++;
}
}

//This should find the average amount of food eaten
average = total/count;
cout << "The average food eaten per day by all monkeys :" << setw(6) << average << " pounds" << endl;

//This shoud find the least amount of food eaten
least = foodEaten[0][0];
for ( int monkeyLeast = 0; monkeyLeast <= 2; monkeyLeast ++ )
{
for ( int dayLeast = 0; dayLeast <= 6; dayLeast ++ )
{
if ( foodEaten[monkeyLeast][dayLeast] < least )
least = foodEaten[monkeyLeast][dayLeast];
}
}
cout << "The least amount of food eaten by any monkey :" << setw(6) << least << " pounds" << endl;

//This should find the highest amount of food eaten
most = foodEaten[0][0];
for ( int monkeyMost = 0; monkeyMost <= 2; monkeyMost ++ )
{
for ( int dayMost = 0; dayMost <= 6; dayMost ++ )
{
if ( foodEaten[monkeyMost][dayMost] > most )
most = foodEaten[monkeyMost][dayMost];
}
}
cout << "The largest amount of food eaten by any monkey :" << setw(6) << most << " pounds" << endl;
return 0;
}

出于某种原因,这是我的输出结果:

Enter pounds of food eaten by monkey 1 on Sun: 1Enter pounds of food eaten by monkey 1 on Mon: 2Enter pounds of food eaten by monkey 1 on Tue: 3Enter pounds of food eaten by monkey 1 on Wed: 4Enter pounds of food eaten by monkey 1 on Thu: 5Enter pounds of food eaten by monkey 1 on Fri: 6Enter pounds of food eaten by monkey 1 on Sat: 7Enter pounds of food eaten by monkey 2 on Sun: 8Enter pounds of food eaten by monkey 2 on Mon: 9Enter pounds of food eaten by monkey 2 on Tue: 10Enter pounds of food eaten by monkey 2 on Wed: 11Enter pounds of food eaten by monkey 2 on Thu: 12Enter pounds of food eaten by monkey 2 on Fri: 13Enter pounds of food eaten by monkey 2 on Sat: 14Enter pounds of food eaten by monkey 3 on Sun: 15Enter pounds of food eaten by monkey 3 on Mon: 16Enter pounds of food eaten by monkey 3 on Tue: 17Enter pounds of food eaten by monkey 3 on Wed: 18Enter pounds of food eaten by monkey 3 on Thu: 19Enter pounds of food eaten by monkey 3 on Fri: 20Enter pounds of food eaten by monkey 3 on Sat: 21Monkey  Sun  Mon  Tue  Wed  Thu  Fri  Sat     1    1    2    3    8    9   10   15     2    8    9   10   15   16   17   18     3   15   16   17   18   19   20   21The average food eaten per day by all monkeys     :12.7143 poundsThe least amount of food eaten by any monkey      :     1 poundsThe largest amount of food eaten by any monkey     :    21 pounds--------------------------------Process exited after 19.28 seconds with return value 0Press any key to continue . . .

您可以看到,从第一行的星期三开始,数据并未反射(reflect)我输入的内容,第二行再次发生这种情况,但不是第三行。

感谢您的帮助。

最佳答案

您需要访问

foodEaten[day][monkey]

不是

foodEaten[monkey][day]

关于c++ - 二维数组中的值未正确分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35946282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com