值未在2d数组中正确分配

时间:2016-03-11 17:23:39

标签: c++

我的智慧结束了,我确信这是一个简单的错误。我已经google了这个,我已经看过很多次这个类似的问题了,但我的代码看起来和我看到的类似,所以我仍然无法弄清楚我做错了什么。

我正在为一个班级做一个家庭作业,要求用户在一周内让3只猴子吃掉食物的数量,然后将这些条目存储在一个二维阵列中。

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

当我运行代码时:         #include // for cin,cout,endl         #包括         #包括         使用namespace std;

    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 . . .

你可以看到第一行的星期三开始,数据没有镜像我输入的内容,它在第二行再次发生,但不是第三行。

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

您需要访问

foodEaten[day][monkey]

foodEaten[monkey][day]

答案 1 :(得分:0)

这是解决方案:您应该使用此double foodEaten[DAYS_WEEK][MONKEYS];替换此行:double foodEaten[MONKEYS][DAYS_WEEK];

这是输出:

enter image description here

答案 2 :(得分:0)

您面临的问题是: 实际上你的数组是:

    double foodEaten[7][3]

但是当您访问元素时,它按以下顺序进行:

    foodEaten[Monkey][DayOfTheWeek]

始终确保数组中的逻辑一致性,即如果行是Days并且列为Monkeys,请确保您的迭代器也属于同一类型。

正如其他答案所指定,使用

    foodEaten[DayOfTheWeek][Monkey]

可以解决问题。