如何比较数组内部的值

时间:2014-09-10 15:29:22

标签: c++ arrays

所以我写了一个程序,要求用户输入一个人(1-10)早餐吃的煎饼数量。该程序必须分析输入并确定哪个人吃最多的煎饼。该程序还必须按照所有10个人吃的煎饼数量顺序输出一个列表。到目前为止,我已经编写了代码来获取用户输入和显示数组的代码,但不是按顺序。在比较数组中的元素时,我完全迷失了:

int getPancakes();
void displayArray(int theArray[],int sizeOfArray);
void compareArray(int sizeOfArray);
int pancakes[10];
int z = 0;

int main()
{
}

int getPancakes(){
    int y;
    int x = 0;

    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){
    for(int x = 0 ;x < sizeOfArray ; x++){
        ++z;
        cout << "Person " << z << " ate " << pancakes[x] << " pancakes" << endl;
    }
}

那么如何指示我的程序比较数组中的元素呢?另外,我如何指示我的程序打印每个人按顺序吃的煎饼数量列表?

5 个答案:

答案 0 :(得分:1)

为了找到谁吃了最多的煎饼,你基本上需要找到数组中最大值的位置。

int findMaxPosition(int array[], int arraySize){
    int maxPosition = 0;     //assume the first element is maximum
    for(int i = 1; i < arraySize; i++)
        if(array[i] > array[maxPosition]) //compare the current element with the known max
            maxPosition = i;   //update maxPosition
    return maxPosition;
}

请注意,这会让您第一次出现最大值。如果元素是独一无二的,那就是这样。否则,您应该找到最大值array [maxPosition],并遍历数组并显示它出现的每个位置。

排序有点复杂。排序算法并不那么简单,我担心如果我给你写了一个实现,我就不会帮你。

最简单的排序算法之一是冒泡排序。维基百科(http://en.wikipedia.org/wiki/Bubble_sort)有一个关于它的详细页面,你应该能够使用那里给出的伪代码来实现它。

答案 1 :(得分:0)

如果所有数字都是唯一的

int max = 0;
for(int x = 0 ;x < 10 ; x++)
{
    if(pancakes[x] > max)
        max = pancakes[x];
}
for(int x = 0 ;x < 10 ; x++)
{
    if(pancakes[x] == max)
         cout << "Person " << x << " ate " << pancakes[x] << " pancakes - biggest number" << endl;
}

答案 2 :(得分:0)

简而言之,有两种方法可以将数组元素与另一个值(直接和通过副本)进行比较。

// Make a copy:
  int count = pancakes[x];
  if (count == limit)
  {
    //...
  }

// Direct access
  if (pancakes[x] == limit)
  {
    //...
  }

答案 3 :(得分:0)

无需遍历数组,查找max 然后排序以提供输出。相反,您可以跟踪在输入阶段找到的最大值:

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

请注意,我删除了y的冗余声明(您在for循环中声明它)和x(总是等于y) 。
我还添加了一个函数返回(最常吃的人的索引),因为你没有返回值。 (返回一些东西或使函数返回void(不返回)) 如果您只关心吃掉的最大数量,那么您甚至不需要跟踪最大值。相反,只需在排序步骤后读取数组中的最大值。

现在你需要做的就是实现void sortArray()并在调用display函数之前调用它:

int main()
{
  int max = getPancakes();
  sortArray();             //left to you to implement
  displayArray(pancakes,10);
}

您可能需要考虑在main本地制作煎饼并将其传递到您的功能中,方法与您displayArray相同。

答案 4 :(得分:0)

我完成了针对同一活动的解决方案,涉及“谁吃得最多”的部分,即使有多个人吃了最多的煎饼。它涉及数组和向量。经过测试并可以正常工作:

#include <iostream>
#include <array>
#include <vector>
using namespace std;

int main()
{
    array <int, 10> PancakeEater;
    vector<int> Winners;
    int max;


    cout << "Type number of pancakes eaten, separated by spaces, for each of the ten contestants:" << endl;
    cout << "#1 #2 #3 #4 #5 #6 #7 #8 #9 #10" << endl;

    for (int i = 0; i < PancakeEater.size(); i++)
    {
        cin >> PancakeEater[i];
    }

    max = PancakeEater[0];
    Winners.push_back(1);
    for (int i = 0; i < PancakeEater.size()-1; i++)
    {

        if (max == PancakeEater[i + 1]) 
        {
            Winners.push_back(i + 2);
        }
        if (max < PancakeEater[i + 1])
        {            
            max = PancakeEater[i + 1];
            Winners.clear();
            Winners.shrink_to_fit();
            Winners.push_back(i + 2);
        }
    }
    if (Winners.size() > 1) 
    {
        cout << endl << "The contestants that ate the most pancakes, on a tie for first place, are contestant numbers " << endl;
        for (auto item : Winners) 
        {
            cout << item << " ";
        }
    }
    else
    {
        cout << endl << "The contestant that ate the most pancakes is contestant number " << endl;
        for (auto item : Winners)
        {
            cout << item << " ";
        }
    }
}