Search code examples
c++bubble-sort

Minor Bug -- Pancake Gluttony


I am new to C++ so I have been practicing. I found this activity called Pancake Glutton on the internet so I am trying to improve my skills by tackling on this challenge. NOTE: THIS IS NOT HOMEWORK. I am just doing this for fun and to expand my skills as a programmer.

I ran into a problem. This is my output, as you can see, there are two People 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.

This is my code:

#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;
    }
 }

Solution

  • You are indexing your arrays beyond the last item and that leads to undefined behaviour when swapping:

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

    You are testing that i < people, but not that i + 1 < people. Because indexing starts from 0, the index of the last item is people - 1.

    Since you're practicing C++, I suggest you to use std::vector instead of basic arrays. For example, std::vector::at() throws an exception if your indexing is out of bounds.