Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何打印数组元素的索引号?_C++_Arrays_Sorting - Fatal编程技术网

C++ 如何打印数组元素的索引号?

C++ 如何打印数组元素的索引号?,c++,arrays,sorting,C++,Arrays,Sorting,所以我做了一个练习,要求用户输入10个人吃的煎饼数量,打印出谁吃的煎饼最多,然后从最大到最小排列列表,基本上我必须打印出: 第三个人:吃了10个煎饼,第五个人:吃了9个煎饼,第八个人:吃了8个煎饼 但是在使用冒泡排序后,我无法匹配具有正确值的人,因为冒泡排序会交换他们!有人知道解决这个问题的方法吗?例如,是否有其他方法可以在不使用气泡排序的情况下组织数组中的值 void getPancakes() { int x = 0; int temp; for(int y

所以我做了一个练习,要求用户输入10个人吃的煎饼数量,打印出谁吃的煎饼最多,然后从最大到最小排列列表,基本上我必须打印出:

第三个人:吃了10个煎饼,第五个人:吃了9个煎饼,第八个人:吃了8个煎饼

但是在使用冒泡排序后,我无法匹配具有正确值的人,因为冒泡排序会交换他们!有人知道解决这个问题的方法吗?例如,是否有其他方法可以在不使用气泡排序的情况下组织数组中的值

void getPancakes()
{    
    int x = 0;
    int temp;
    for(int y = 0; y < 10; y++)
    {
        ++x;
        cout << "How many pancakes did person " << x << " eat?" << endl;
        cin >> pancakes[y];
    }        
}

void displayArray(int theArray[],int sizeOfArray)
{
    int temp;
    int i,j;
    int q = 10;  
    for(i = 0; i <= sizeOfArray - 1 ; i++)
    {
        for(j = i+1 ; j < sizeOfArray ; j++)
        {
            if(theArray[i] < theArray[j])
            {
                temp = theArray[i];
                theArray[i] = theArray[j];
                theArray[j] = temp;
            }
        }
  }       
  cout << endl;
  for(i = 0; i < 10; i++)
      cout << "Person " << i+1 << " ate " << theArray[i] << " pancakes" << endl;
}
void getPancakes()
{    
int x=0;
内部温度;
对于(int y=0;y<10;y++)
{
++x;

cout正如Cody所说,您必须存储这两个值。正如@HgMs所指出的,您可以创建一个类或一个
struct
,它是一个只包含公共数据成员的类。下面是一个示例:

struct person{
    int id;
    int pancakes;
};

int main(){

    const int totalPeople = 10;
    person personArray[totalPeople];

    for (int i = 0; i < totalPeople; ++i){
        cout << "How many pancakes did person " << i << " eat? ";
        personArray[i].id = i;
        cin >> personArray[i].pancakes;
    }

    for (int i = 0; i < totalPeople; ++i){
        cout << "id: " << personArray[i].id << "\t" << "Pancakes eaten: " << personArray[i].pancakes << endl;
    }

    return 0;
}
struct-person{
int-id;
内特煎饼;
};
int main(){
const int totalPeople=10;
人-人[总体人];
for(int i=0;i您是否需要同时存储两个值:此人的标识符和他们吃的煎饼数量。然后,您将根据煎饼数量进行排序,并打印该项目的此人标识符。使用一个同时包含id和他吃的煎饼数量的类,您的数组就是此类的数组。如果不想创建新类中,您可以有一个与数组大小相同的额外数组,该数组将从
1
存储到
sizeOfArray
。排序时,还可以交换此数组。这将跟踪
人员N
的数量。