小错误——Pancake Gluttony

Minor Bug -- Pancake Gluttony

我是C++新手,所以一直在练习。我在互联网上发现这个 activity 叫做 Pancake Glutton,所以我正在尝试通过应对这个挑战来提高我的技能。注意:这不是家庭作业。我这样做只是为了好玩并扩展我作为程序员的技能。

我运行遇到了问题。这是我的输出,如您所见,有两个人 2:

Please input number of people: 5
How many pancakes did person 1 have? 3
How many pancakes did person 2 have? 9
How many pancakes did person 3 have? 2
How many pancakes did person 4 have? 4
How many pancakes did person 5 have? 7

Person 2 had 9 pancakes.
Person 5 had 7 pancakes.
Person 4 had 4 pancakes.
Person 1 had 3 pancakes.
Person 2 had 3 pancakes.

这是我的代码:

#include <iostream>

int getAmountOfPeople();
void getAmountOfPancakes(int numPancakes[], int people);
void bubbleSwap(int pancakes[], int numPancakes[], int people);
void organizePancakeArray(int pancakes[], int num);
void display(int pancakes[], int numPancakes[], int people);

using namespace std;

int main()
{
    int people = 0;

    people = getAmountOfPeople();

    //dynamically set arrays
    int pancakes[people];
    int numPancakes[people];

    //calling appropriate functions
    initializePancakeArray(pancakes, people);
    getAmountOfPancakes(numPancakes, people);
    bubbleSwap(pancakes, numPancakes, people);
    display(pancakes, numPancakes, people);

    return 0;
 }

 /************************************************
 * Purpose: Get number of people from the user
 **********************************************/
int getAmountOfPeople()
{
    int people = 0;

    cout << "Please input number of people: ";
    cin >> people;

    //error checking
    while (people <= 0)
    {
       cout << "Invalid entry. Please input number of people: ";
       cin >> people;
    }
   return people;
 }

 /********************************************************
 * Purpose: Get amount of pancakes eaten from the user
 ******************************************************/
 void getAmountOfPancakes(int numPancakes[], int people)
 {
    int counter = 0;
    int temp = 0;

    for (int i = 0; i < people; i++)
    {
       cout << "How many pancakes did person "
            << i + 1
            << " have? ";

       cin >> numPancakes[i];

       //error checking
       while (numPancakes[i] < 0)
       {
          cout << "Invalid entry. Please re-enter a positive value.\n"
               << "How many pancakes did person "
               << i + 1
               << " have? ";

          cin >> numPancakes[i];
       }

       //for instrumentation
       counter++;
    }

    cout << endl;
    return;
 }

 /**************************************************************
 * Purpose: Organizes arrays into descending order using
 * bubble swap method.
 ***************************************************************/
 void bubbleSwap(int pancakes[], int numPancakes[], int people)
 {
    bool swapped = true;
    int temp;
    int tmp;
    int count;

    while (swapped)
   {
       swapped = false;
       for (int i = 0; i < people; i++)
       {
          if (numPancakes[i] < numPancakes[i + 1])
          {
             //temporarily sets original values
             //so original values will not be lost
             temp = numPancakes[i];
             tmp = pancakes[i];

             //sets the array to new variable
             numPancakes[i] = numPancakes[i + 1];
             pancakes[i] = pancakes[i + 1];

             //assigns next part of array lower value
             pancakes[i + 1] = temp;
             numPancakes[i + 1] = tmp;

             //allows loop to continue until no swap was made
             swapped = true;
          }
       }
    }
 }

 /***********************************************
 * Purpose: Gives each part of array a certain
 * value.
 **********************************************/
 void initializePancakeArray(int pancakes[], int num)
 {
    for (int i = 0; i < num; i++)
    {
       pancakes[i] = i + 1;
    }
 }

 /********************************************************
 * Purpose: Displays the arrays to the user in a descending
 * order.
 *******************************************************/
 void display(int pancakes[], int numPancakes[], int people)
 {
    for (int i = 0; i < people; i++)
    {
       cout << "Person "
            << pancakes[i]
            << " had "
            << numPancakes[i]
            << " pancakes."
            << endl;
    }
 }

您正在为超出最后一项的数组编制索引,这会在交换时导致未定义的行为:

   swapped = false;
   for (int i = 0; i < people; i++)
   {
      if (numPancakes[i] < numPancakes[i + 1])
      {

您正在测试 i < 人,而不是 i + 1 < 人。因为索引从0开始,所以最后一项的索引是people - 1.

既然你在练习 C++,我建议你使用 std::vector 而不是基本数组。例如,如果您的索引超出范围,std::vector::at() 将抛出异常。