我在我的智慧結尾,我確信這是一個簡單的錯誤。我用google搜索了這個,我也看到過這個類似的問題,但是我的代碼看起來和我見過的類似,所以我仍然無法弄清楚我做錯了什麼。在二維數組中無法正確指定值
我正在爲家庭作業分配一個班,要求用戶在一週內吃了3只猴子吃的食物量,然後將這些條目存儲在2d數組中。
請原諒我可怕的變量名稱,一旦我把它分解成不同的函數,我會改變它們,但我想首先讓它運行。我測試了它,我的總和,平均,最少和大多數語句都工作,但由於某種原因,我輸入數組的數據是跳過數字或覆蓋數字(我也在下面發佈了輸出)。
當我運行的代碼: 的#include //爲CIN,COUT,ENDL使用命名空間std 的#include 的#include ;
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: 1 Enter pounds of food eaten by monkey 1 on Mon: 2 Enter pounds of food eaten by monkey 1 on Tue: 3 Enter pounds of food eaten by monkey 1 on Wed: 4 Enter pounds of food eaten by monkey 1 on Thu: 5 Enter pounds of food eaten by monkey 1 on Fri: 6 Enter pounds of food eaten by monkey 1 on Sat: 7 Enter pounds of food eaten by monkey 2 on Sun: 8 Enter pounds of food eaten by monkey 2 on Mon: 9 Enter pounds of food eaten by monkey 2 on Tue: 10 Enter pounds of food eaten by monkey 2 on Wed: 11 Enter pounds of food eaten by monkey 2 on Thu: 12 Enter pounds of food eaten by monkey 2 on Fri: 13 Enter pounds of food eaten by monkey 2 on Sat: 14 Enter pounds of food eaten by monkey 3 on Sun: 15 Enter pounds of food eaten by monkey 3 on Mon: 16 Enter pounds of food eaten by monkey 3 on Tue: 17 Enter pounds of food eaten by monkey 3 on Wed: 18 Enter pounds of food eaten by monkey 3 on Thu: 19 Enter pounds of food eaten by monkey 3 on Fri: 20 Enter pounds of food eaten by monkey 3 on Sat: 21 Monkey 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 21 The average food eaten per day by all monkeys :12.7143 pounds The least amount of food eaten by any monkey : 1 pounds The largest amount of food eaten by any monkey : 21 pounds -------------------------------- Process exited after 19.28 seconds with return value 0 Press any key to continue . . .
你可以看到開始的第一行數據不鏡像我輸入的內容的星期三和它的再次發生第二排,但不是第三排。
感謝您的任何幫助。
再看看'foodEaten'的聲明,然後再看看你如何使用它。宣言中猴子的日期和地點在哪裏與您的使用方式相比如何? –
更改'foodEaten [monkey] [day];'到'foodEaten [day] [monkey];' – Rabbid76
謝謝,這是超級有用的。我沒有意識到我正在初始化列和行。我打算將它初始化爲行然後列。 ! –