我是C ++的新手,所以我一直在练习。我在互联网上发现了这个名为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;
}
}
答案 0 :(得分:1)
您正在将数组编入索引超出最后一项,并且在交换时会导致未定义的行为:
swapped = false;
for (int i = 0; i < people; i++)
{
if (numPancakes[i] < numPancakes[i + 1])
{
你正在测试我&lt;人,但不是i + 1&lt;人。因为索引从0开始,所以最后一项的索引是人 - 1。
由于您正在练习C ++,我建议您使用std :: vector而不是基本数组。例如,如果索引超出范围,则std :: vector :: at()会抛出异常。